LostKobrakai Posted September 4, 2015 Share Posted September 4, 2015 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); } } 11 Link to comment Share on other sites More sharing options...
teppo Posted September 4, 2015 Share Posted September 4, 2015 That's a brilliant idea, thanks for sharing! Nearly every site I work with has something along those lines at template level. While that works just fine, this helps avoid some pointless if-mess 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 4, 2015 Author Share Posted September 4, 2015 And most importantly, it works in the admin as well. So users aren't confused to see empty fields in listers and so on. Link to comment Share on other sites More sharing options...
bernhard Posted September 4, 2015 Share Posted September 4, 2015 hm... i have no idea where/how i could use this. could you please give me an example use case? don't want to miss this "brilliant idea" thank you Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 4, 2015 Author Share Posted September 4, 2015 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. 1 Link to comment Share on other sites More sharing options...
tsdtsdtsd Posted September 4, 2015 Share Posted September 4, 2015 @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. 1 Link to comment Share on other sites More sharing options...
sz-ligatur Posted October 17, 2022 Share Posted October 17, 2022 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now