Juergen Posted April 18, 2016 Share Posted April 18, 2016 Hello @ all, I have tried to create the following scenario: The page table field should be hidden in the (parent) template, if there are no items in it. If items (child pages) were created, the page table field should be visible in the (parent) template. Inputfield dependencies seem to be the wrong way because the inputfield depency is depending on the value of another field. As far as I know it is not possible in this case to check if the inputfield (page table) itself is empty or not. Another possible way would be to hook into page render: $this->addHookBefore('PageRender::renderPage', $this, 'rendereventpagetable'); public function rendereventpagetable($event) { $page = $event->arguments[0]; //hide page table if there are no events in it if (($page->template == "events") AND ($page->children == "0")) { //SET the status of the pagetablefield to hidden but how? } } Does anyone know how to set the status of a field to hidden or maybe how to add a css class to hide the field? Best regards Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 18, 2016 Share Posted April 18, 2016 Hook before InputfieldPageTable::render and set the "collapse" mode to hidden if the table is empty and it's your specified field. 1 Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 Aah, you mean something like this: https://gist.github.com/somatonic/c9db5c43f10d050bb14d I will try this way out. Thanks Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 Sorry but I cannot get it to work. I have tried it with this hook function. public function rendereventpagetable(HookEvent $event) { // get the table field $table = $event->object; // make sure this is our field if ($table->name !== "singleeventtable") return; if (count($table->attr("value")) == 0) { $this->message('Noch keine Termine erstellt'); $table->collapse = "hidden"; } } It shows me the message "Noch keine Termine erstellt" (in English: No events created), but the page table is still visible. Can you point me into the right direction? Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 18, 2016 Share Posted April 18, 2016 Try Inputfield::collapsedHidden instead of "hidden". Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 No this doesnt work either. I have also tried to add a specific css class to the page table field in this way: $table->addClass('myClassName'); But this will also be ignored. It seems that the page table field is a little bit different like other inputfields. Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 I have tried it in these ways: $table->collapsed = Inputfield::collapsedHidden; $table->collapse = Inputfield::collapsedHidden; $table->collapsed = "hidden"; $table->collapse = "hidden"; For example this works: $table->label = "test"; The label will be replaced by the word "test". Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 18, 2016 Share Posted April 18, 2016 If you can set the visibility to hidden in the field's settings then the above change should work as well, but maybe you need to hook somewhere else, so that the render() method won't actually be called for that field (because it's hidden). Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 I can set it to hidden in the settings - but why should I do that? I dont understand what you mean in this case??? Link to comment Share on other sites More sharing options...
netcarver Posted April 18, 2016 Share Posted April 18, 2016 Have you tried hooking ProcessPageEdit::buildFormContent? Like this; $pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) { $wrapper = $event->return; $wrapper->YOUR_FIELD->collapsed = Inputfield::collapsedHidden; }); If that works, you can add conditional logic to only apply it as needed. I usually apply this kind of hook in site/ready.php. 2 Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 Hello netcarver, your code as standalone works!! I have tried to combine it: $this->addHookAfter("InputfieldPageTable::render", $this, "rendereventpagetable"); public function rendereventpagetable(HookEvent $event) { // get the table field $table = $event->object; // make sure this is our field if ($table->name !== "singleeventtable") return; if (count($table->attr("value")) == 0) { $this->error('Achtung! Sie haben noch keine Termine erstellt'); //START $this->addHookAfter("ProcessPageEdit::buildFormContent", function($event) { $wrapper = $event->return; $wrapper->singleeventtable->collapsed = Inputfield::collapsedHidden; }); //END } } But in this case it shows me the message, but the page table field is still visible Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 18, 2016 Share Posted April 18, 2016 Now that's some hookception $pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) { $wrapper = $event->return; if(!$wrapper->has('singleeventtable')) return; if (count($wrapper->singleeventtable->attr("value")) == 0) { $this->error('Achtung! Sie haben noch keine Termine erstellt'); $wrapper->singleeventtable->collapsed = Inputfield::collapsedHidden; } }); 4 Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 That code makes exactly what I want! Great! Thank you so much!!!!!!! Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 Uuhh! Unfortunately it hides the page table if there is at least one item Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 I have output the number of items in the message: If there is one item it outputs 0. If there are more than one item it outputs the correct number. Very strange! Link to comment Share on other sites More sharing options...
netcarver Posted April 18, 2016 Share Posted April 18, 2016 Does using this test work? if (count($wrapper->singleeventtable->attr("value")) === 0) { It may not, just guessing at the moment. Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 18, 2016 Share Posted April 18, 2016 Otherwise try: $event->object->getPage()->singleeventtable->count() 2 Link to comment Share on other sites More sharing options...
Juergen Posted April 18, 2016 Author Share Posted April 18, 2016 This works!!! Here is the complete code for others who are interested in: //initialize the function in the module public function init(){ $this->addHookAfter("ProcessPageEdit::buildFormContent", $this, "renderpagetable"); } public function rendepagetable($event){ $wrapper = $event->return; $page = $event->object->getPage(); if(!$wrapper->has('singleeventtable')) return; $itemnumber = count($page->children); if ($itemnumber == 0) { $this->message('There are no items in the page table field'); $wrapper->singleeventtable->collapsed = Inputfield::collapsedHidden; } else { $this->message("Number of items:{$itemnumber}"); } } } "singleeventtable" is the name of my page table field - replace it with your own page table field name. Special thanks to netcarver and LostKobraKai for helping to find a working solution. 3 Link to comment Share on other sites More sharing options...
Alfred Posted November 26, 2016 Share Posted November 26, 2016 For everyone using/copying Juergens last mentioned code; there is a little typo in: public function rendepagetable($event){ The 'r' is missing in renderpagetable. 2 Link to comment Share on other sites More sharing options...
Juergen Posted November 27, 2016 Author Share Posted November 27, 2016 Thanx @Alfred I have corrected this. 1 Link to comment Share on other sites More sharing options...
Juergen Posted January 6, 2017 Author Share Posted January 6, 2017 If you use the init.php introduced in PW 2.6.7 you can add this piece of code to the init. php: // show pagetable field only if there are items in it $pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) { $wrapper = $event->return; if($wrapper->has('NAMEOFYOURPAGETABLEFIELD')){ if ($event->object->getPage()->numChildren > 0) return; $wrapper->NAMEOFYOURPAGETABLEFIELD->collapsed = Inputfield::collapsedHidden; } }); 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 6, 2017 Share Posted January 6, 2017 // show pagetable field only if there are items in it $pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) { $wrapper = $event->return; if (!$wrapper->has('NAMEOFYOURPAGETABLEFIELD')) return; if ($event->object->getPage()->NAMEOFYOURPAGETABLEFIELD->count) return; $wrapper->NAMEOFYOURPAGETABLEFIELD->collapsed = Inputfield::collapsedHidden; }); This is more reliable as it does also work if page table entries are not stored as children or if there are other children for the page besides the pagetable ones. 2 Link to comment Share on other sites More sharing options...
Juergen Posted January 6, 2017 Author Share Posted January 6, 2017 Ok, thanks for the hint. In my case I have only direct children in the page table field, but I have changed it to your snippet. 1 Link to comment Share on other sites More sharing options...
Juergen Posted January 6, 2017 Author Share Posted January 6, 2017 Mmhh! Your version seems not to work: $itemnumber = $event->object->getPage()->NAMEOFYOURPAGETABLEFIELD->count(); $this->message("Number of items:{$itemnumber}"); Returns always 0 in the message in my case. 1 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