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

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

  • Like 1

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 4

Share this post


Link to post
Share on other sites

this is working great for a pagetable with a single item/button. I would like to adapt this code to set icons to multiple buttons, based on the icon that is set in the template settings of the item template (in backend GUI).  Here is what I have so far, wich sets the icon markup as an attribute for the first item:

$this->addHookBefore('InputfieldPageTable::render', function ($event) {
	    $table = $event->object;
        $templateIconName = $this->templates->get(['id=' => $table->hasField()->template_id])->getIcon();
        $templateIcon = wireIconMarkup($templateIconName);

	$this->addHookBefore('InputfieldButton::render', null, function (HookEvent $event) use ($templateIcon) {
		$button = $event->object;
    	$button->attr('icon', $templateIcon);
	});
});

Any Idea how I can adapt that to work with multiple Items? And is it also possible to set the Icon markup inside a span tag or something?
If its easier to set the attribute, I think I could set the markup using javascript after page load...

Share this post


Link to post
Share on other sites
10 hours ago, jploch said:

I would like to adapt this code to set icons to multiple buttons, based on the icon that is set in the template settings of the item template

For /site/ready.php, but you can adapt for use in a module if needed:

$wire->addHookBefore('InputfieldPageTable::render', function (HookEvent $event) {
	/** @var InputfieldPageTable $table */
	$table = $event->object;
	$field = $table->hasField;
	// Get array of table template data in the form 'label' => 'icon'
	$table_template_ids = $field->template_id;
	$table_templates = $event->wire('templates')->find(['id' => $table_template_ids]);
	$table_template_data = [];
	foreach($table_templates as $table_template) {
		$table_template_data[$table_template->get('label|name')] = $table_template->icon;
	}
	
	$event->wire()->addHookBefore('InputfieldButton::render', function (HookEvent $event) use ($table_template_data) {
		/** @var InputfieldButton $button */
		$button = $event->object;
		// Return early if this is not a button we want to modify
		if(!isset($table_template_data[$button->value])) return;
		// Set button icon
		$button->icon = $table_template_data[$button->value];
	});
});

2020-08-16_120655.png.3345d03210b365728e76c944e96df305.png

  • Like 1

Share this post


Link to post
Share on other sites

@Robin S this is perfect! Works like a charm. Thank you!!

EDIT: After some testing I realized that the icons disappear once a new item is added to the pagetable.
So I guess the ajax call and update of the pagetable removes any changes to the buttons?

Do you know how to hook into the add item action and reset those icons after the ajax update is finished?

EDIT2: Got it. I had to put the hook inside the init instead of ready function. Now it works!

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...