Jump to content

Field Inheritance


LostKobrakai
 Share

Recommended Posts

I just wanted to share a small module I just made, that can inherit values from pages, that are higher up in the page tree. This inheritance will only show for formatted output, so for example in listers or in the frontend. When using the unformatted value one will see, if a field is really set or not. 

<?php

class FieldtypeInheritInteger extends FieldtypeInteger {

	public static function getModuleInfo() {
		return array(
			'title' => 'Inherit Integer',
			'version' => 101,
			'summary' => 'If empty tries to get values of the same field from further 
				above the pagetree. This does only work for formatted values so empty values
				won\'t ne overwritten by editing a page.'
			);
	}

	public function ___formatValue(Page $page, Field $field, $value) {
		if(!$this->isEmptyValue($field, $value)) return $value;
		
		$parent = $page->parent("$field!=''");
		
		if($parent->id) return $parent->getFormatted($field);
		else return $value;
	}

	public function ___markupValue(Page $page, Field $field, $value = null, $property = '') {
		return $this->___formatValue($page, $field, $value);
	}

}
  • Like 11
Link to comment
Share on other sites

Imagine following page-tree with a field "speed" on all pages:

-  beachbuggies ( speed = 50 )
    - buggy #1 ( speed = '' )
    - buggy #2 ( speed = '' )
    - buggy #3 ( speed = 60 ) 

Now buggy #1 and #2 will show a (max) speed of 50, which is inherited from the beachbuggies group, while buggy #3 will show a speed of 60.

  • Like 1
Link to comment
Share on other sites

@LostKobrakai Very nice, thanks for sharing. I use similar logic in some cases but never made a module out of it...

@BernhardB Let's say you have an element in your page header that echoes a text field and that element is mandatory. Most of the times your element will show a text related to the current category, but you want to be able to overwrite that text in some cases on some child pages. With this module (well, with a Text version of it instead of Integer) you can do that easyly and even see it in admin.

  • Like 1
Link to comment
Share on other sites

  • 7 years later...
On 9/4/2015 at 12:57 PM, LostKobrakai said:

I just wanted to share a small module I just made, that can inherit values from pages, that are higher up in the page tree. This inheritance will only show for formatted output, so for example in listers or in the frontend. When using the unformatted value one will see, if a field is really set or not. 

<?php

class FieldtypeInheritInteger extends FieldtypeInteger {

	public static function getModuleInfo() {
		return array(
			'title' => 'Inherit Integer',
			'version' => 101,
			'summary' => 'If empty tries to get values of the same field from further 
				above the pagetree. This does only work for formatted values so empty values
				won\'t ne overwritten by editing a page.'
			);
	}

	public function ___formatValue(Page $page, Field $field, $value) {
		if(!$this->isEmptyValue($field, $value)) return $value;
		
		$parent = $page->parent("$field!=''");
		
		if($parent->id) return $parent->getFormatted($field);
		else return $value;
	}

	public function ___markupValue(Page $page, Field $field, $value = null, $property = '') {
		return $this->___formatValue($page, $field, $value);
	}

}

Hi Adrian,

I tried to get it running – without success. I guess some things have changed in the module requirements since 2015… e.g. I tried autoload = true, but no changes. As I put a bd() in the two functions I've got no output, so they aren't called at all. It would be a great help as a startingpoint for some inheritance needs - your approach to make it work in Admin-views is quite promising.

Thank you in advance. Sebastian

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...