Jump to content
Juergen

Tip how to change add button value on pagetable fields

Recommended Posts

If you want to add your own button value at the add button of a pagetable field, you only have to put this little piece of code inside your ready.php file.

$wire->addHookBefore('InputfieldPageTable::render', function($event) {
   $table = $event->object;
   if(!in_array($table->name, array('datespagetable'))) return;
      $this->addHookBefore("InputfieldButton::render", null, function(HookEvent $event){
         $button = $event->object;
         if($button->name == 'button'){
          $button->attr('value', 'Test');
         }
      });
});

In this case I limit it to a certain pagetable field called "datespagetable". You have to change it to the name of your desired pagetable field.

If you want to change it for all pagetable fields (no restrictions) simply remove the following line from the code:

if(!in_array($table->name, array('datespagetable'))) return;

If you want to add it to more than 1 pagetable field, then write all the fieldnames into the array:

if(!in_array($table->name, array('datespagetable','pagetable1','pagetable2'))) return;

You can change the value of the button text to your needs in this line of code:

$button->attr('value', 'Test');

I simply called it "Test" in this case to show how it is working.

If you need it to be a translateable string simply use:

$button->attr('value', __('Test'));

And now this is the result:

 

screenshot-www.juergen-kern.at-2017-11-06-10-28-48.png.f5c93d656be57f313ba1d641d8b2ce7e.png

 

This let you customize your pagetable field a little bit. Fe you can use "Add News" or "Add new events" or something else.

If you want to change another attribute than the value please take a look at the following page: https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldButton.module

Best regards

  • Like 3

Share this post


Link to post
Share on other sites
6 minutes ago, Juergen said:

I know, this is the solution if you have only 1 template assigned.:)

Yeah - I got it :) and appreciate your tip!

Just thought others might like clarification on how it works with multiple templates.

Share this post


Link to post
Share on other sites

Thanks alot @Juergen!

If you wan't to name the button automatically based on the page-table label or the label/title of the allowed templates, use this hook:

$this->addHookBefore('InputfieldPageTable::render', function ($event) {
	$table = $event->object;

	// Make sure only for fields with a single template…
	if (count($table->hasField()->template_id) > 1) {
		return;
	}

	// Use the field label…
	$label = $table->hasField()->label;
	// or… use the template label or title…
	$label = $this->templates->get(['id=' => $table->hasField()->template_id])->get('label|title');

	// don't miss the use($label part)…
	$this->addHookBefore('InputfieldButton::render', null, function (HookEvent $event) use ($label) {
		$button = $event->object;
		if ('button' == $button->name) {
			// add label and translatable Add string…
			$button->attr('value', __('Add') . ' ' . $label);
		}
	});
});

 

  • Like 3

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...