Jump to content

Tip how to change the table headers in page tables


Juergen
 Share

Recommended Posts

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:

screenshot-www.juergen-kern.at-2017-11-13-15-51-50.thumb.png.dd7f2c785385d68d72f4346605a4b011.png

The text for the table headers comes from the field labels. Now we want to change this a little bit:

screenshot-www.juergen-kern.at-2017-11-13-15-52-29.thumb.png.e06f9f890d31a053b0d95347b10789a1.png

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.

  • Like 4
Link to comment
Share on other sites

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;
});

 

  • Like 5
Link to comment
Share on other sites

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

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

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

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.

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

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;

  }

});

 

  • Like 2
Link to comment
Share on other sites

  • 1 year later...
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

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...