Juergen Posted November 13, 2017 Share Posted November 13, 2017 This little tutorial is about how to change the text of the table headers of a page table field: This is what it looks like before: The text for the table headers comes from the field labels. Now we want to change this a little bit: As you can see I have changed 3 values of the table header: Beginn (Datum) -> Start Beginn (Zeit) -> Beginn Ende (Zeit) -> Ende Here is the hook to make this working. I use a "InputfieldPageTable::render" hook. This code should be placed inside init.php not ready.php $this->addHookAfter('InputfieldPageTable::render', function($event) { $id = $this->input->get('id'); $page = wire('pages')->get('id='.$id); $table = $event->object; $content = $event->return;//put the output into a variable for manipulation later on if($table->name == 'mypagetablefield'){ //only on this pagetable field - rename it to the name of your field //create an array of all fields where you want to change the header text $fieldsarray = array( //use name of the field as key and the new header text as value ($key => $value) 'field1' => __('Text 1'),//rename field1 to your field name and Text 1 to your table header text of field1 'field2' => __('Text 2')//rename field2 to your field name and Text 2 to your table header text of field2 ); $templateID = $page->template->childTemplates[0];//array of children templates IDs - in this case key 0 indicates the first (and only) template; foreach($fieldsarray as $key=>$value) { $label = wire('templates')->get($templateID)->fields->getFieldContext($key)->label;//get labels from template context $content = str_replace($label, $value, $content);//manipulate the output } $event->return = $content;//output the manipulated content } }); I know using str_replace is not the most elegant solution, but I havent found another possibility to achive this. $templateID is used to get the labels from template context. In this example I manipulate 2 field labels (labels of field1 and field2). You have to adapt it to your needs The str_replace line is for the manipulation. If someone has better and more elegant solution please post it here. There is also a problem if the page table is updated via Ajax. If so they headers return to the default value: Maybe a hook with "InputfieldPageTableAjax::checkAjax" would be also necessary. So use with caution at the moment. Edit: After removing this line from the code and putting the code into init.php it works also after updating via Ajax: if($this->process != 'ProcessPageEdit') return; //this was not a good idea to use It seems that if the page table is updated via Ajax than the process is no longer in "ProcessPageEdit" mode, so it will force the hook to not run. Removing this line from the code solves the problem. Now its working after Ajax update without any problems. Also a big thanks to @Robin S for helping me to find a working solution. 4 Link to comment Share on other sites More sharing options...
Robin S Posted November 14, 2017 Share Posted November 14, 2017 20 hours ago, Juergen said: There is also a problem if the page table is updated via Ajax. The below works for me with AJAX updates. In /site/init.php: $wire->addHookAfter('InputfieldPageTable::renderTable', function(HookEvent $event) { $out = $event->return; // Add checks here for specific PageTable fields, specific templates, etc. // Your table header replacements $replacements = [ 'Title' => 'Foo', 'Headline' => 'Bar', ]; foreach($replacements as $find => $replace) { $out = str_replace("<th>$find</th>", "<th>$replace</th>", $out); } $event->return = $out; }); 5 Link to comment Share on other sites More sharing options...
Juergen Posted November 14, 2017 Author Share Posted November 14, 2017 Yep that works!!! Awesome! I will rewrite it to adapt it to my code! Thousand thanks. 1 Link to comment Share on other sites More sharing options...
PWaddict Posted November 21, 2017 Share Posted November 21, 2017 I used your code and it does what I want BUT after editing a field value from the pagetable when the modal closes the pagetable doesn't ajax update the new values. I have to refresh the page see them. This happens on all pagetables even though I'm only targeting a specific one with a specific template. If I remove the code from the site/init.php the ajax update on all pagetables is working properly. $this->addHookAfter('InputfieldPageTable::render', function(HookEvent $event) { $page = $this->process->getPage(); $templateID = $page->template->childTemplates[0]; $table = $event->object; $content = $event->return; if($table->name == 'mypagetable' && $page->template->name == "mytemplate") { $fieldsarray = array ( 'myfield' => __('My New Header') ); foreach($fieldsarray as $key=>$value) { $label = wire('templates')->get($templateID)->fields->getFieldContext($key)->label; $content = str_replace($label, $value, $content); } $event->return = $content; } }); Link to comment Share on other sites More sharing options...
Juergen Posted November 21, 2017 Author Share Posted November 21, 2017 Yes you are right. Do you use the latest DEV version of PW? Link to comment Share on other sites More sharing options...
PWaddict Posted November 21, 2017 Share Posted November 21, 2017 2 minutes ago, Juergen said: Yes you are right. Do you use the latest DEV version of PW? Yes, 3.0.84. Link to comment Share on other sites More sharing options...
Juergen Posted November 21, 2017 Author Share Posted November 21, 2017 I am not 100 percent sure, but I guess this issue was not present in previous versions. For the moment I am a little bit helpless, cause there is not so much code that could be changed to make it work properly. Its a simple string replace function and nothing more. I cannot say what causing this issue now????? Maybe you could try to use the code from @Robin S to see if that works. Link to comment Share on other sites More sharing options...
PWaddict Posted November 21, 2017 Share Posted November 21, 2017 7 minutes ago, Juergen said: Maybe you could try to use the code from @Robin S to see if that works. Already tried that and the issue still happens. Link to comment Share on other sites More sharing options...
Juergen Posted November 21, 2017 Author Share Posted November 21, 2017 Ok, there must be something in the code that stops the Ajax update of page table fields. For the moment I have no solution, but I hope that I will find out what causes the problem. Link to comment Share on other sites More sharing options...
Juergen Posted November 21, 2017 Author Share Posted November 21, 2017 Ok I found the issue, but not a solution. This line of code is responsible: $page = $this->process->getPage(); I will try to find a working way to grab the page in another way. Link to comment Share on other sites More sharing options...
Juergen Posted November 21, 2017 Author Share Posted November 21, 2017 Ok, I have tried another approach that seems to work: Replace $page = $this->process->getPage(); with $id = $this->input->get('id'); $page = wire('pages')->get('id='.$id); Let me know if it works Best regards 1 1 Link to comment Share on other sites More sharing options...
PWaddict Posted November 21, 2017 Share Posted November 21, 2017 Yep that works perfect. Problem is gone Thank you very much. 1 Link to comment Share on other sites More sharing options...
Juergen Posted November 21, 2017 Author Share Posted November 21, 2017 Nevermind. Thanks for your info. I will change the code at my first post. 1 Link to comment Share on other sites More sharing options...
Robin S Posted November 21, 2017 Share Posted November 21, 2017 1 hour ago, Juergen said: Ok I found the issue, but not a solution. This line of code is responsible: $page = $this->process->getPage(); getPage() is a method of ProcessPageEdit. It's fine to use it, but you first need to check that the current process is ProcessPageEdit. if($this->process != 'ProcessPageEdit') return; $page = $this->process->getPage(); // ... Link to comment Share on other sites More sharing options...
Juergen Posted November 21, 2017 Author Share Posted November 21, 2017 On 13.11.2017 at 4:06 PM, Juergen said: Edit: After removing this line from the code and putting the code into init.php it works also after updating via Ajax: if($this->process != 'ProcessPageEdit') return; //this was not a good idea to use This causes a problem in my case with Ajax update, so I removed it. 1 Link to comment Share on other sites More sharing options...
PWaddict Posted November 21, 2017 Share Posted November 21, 2017 9 minutes ago, Robin S said: getPage() is a method of ProcessPageEdit. It's fine to use it, but you first need to check that the current process is ProcessPageEdit. if($this->process != 'ProcessPageEdit') return; $page = $this->process->getPage(); // ... This is not good for the specific hook cause on the ajax update of pagetable it displays the original label header. 1 Link to comment Share on other sites More sharing options...
Robin S Posted November 21, 2017 Share Posted November 21, 2017 13 minutes ago, PWaddict said: This is not good for the specific hook cause on the ajax update of pagetable it displays the original label header. You are right. It seems that when the hook fires for the AJAX reload the process is ProcessPageView and not ProcessPageEdit. So @Juergen's way of getting the page from the id GET variable would be the way to go. 1 Link to comment Share on other sites More sharing options...
PWaddict Posted March 30, 2018 Share Posted March 30, 2018 I moved $templateID inside "if" to prevent "Undefined offset" error on a module page. $this->addHookAfter('InputfieldPageTable::render', function(HookEvent $event) { $id = $this->input->get('id'); $page = wire('pages')->get('id='.$id); $table = $event->object; $content = $event->return; if($table->name == 'mypagetable' && $page->template->name == "mytemplate") { $fieldsarray = array ( 'myfield' => __('My New Header') ); $templateID = $page->template->childTemplates[0]; foreach($fieldsarray as $key=>$value) { $label = wire('templates')->get($templateID)->fields->getFieldContext($key)->label; $content = str_replace($label, $value, $content); } $event->return = $content; } }); 2 Link to comment Share on other sites More sharing options...
Juergen Posted March 31, 2018 Author Share Posted March 31, 2018 Thanks for the hint @PWaddict, I have updated the code on the first post ! 1 Link to comment Share on other sites More sharing options...
elabx Posted January 13, 2020 Share Posted January 13, 2020 On 11/13/2017 at 9:06 AM, Juergen said: Edit: After removing this line from the code and putting the code into init.php it works also after updating via Ajax: Does anyone know why if placed on init.php it works and not on ready?? 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