Jump to content

Tip how to change the button text of the add button inside the children tab


Juergen
 Share

Recommended Posts

With this little tutorial I want to show you how you can change the text of the add new button inside the children tab.

This is the default button text which will be rendered on every template where children are allowed:

screenshot-www.juergen-kern.at-2017-10-25-22-46-27.png.b34cca606d7c46280f9119fde313f7c1.png

And this is how it look likes after the manipulation: So it is a little bit customized.;)

screenshot-www.juergen-kern.at-2017-10-25-22-45-50.png.d56dd6e95fa8d67dd9df2f93a0e15551.png

Maybe you can make it also with JS but I show you a way to change it via a hook inside the ready.php.

Copy and adapt the following code into your ready.php.

 

$pages->addHook('ProcessPageEdit::buildForm', function($event) {
            $page = $this->pages->get($this->input->get->id);
            $template = $this->pages->get($this->input->get->id)->template;
            //run the code only on the following templates -> adapt it to your needs
            if(($template == "event_events") || ($template == "event_dates") || ($template == "event_businessvacations") || ($template == "event_specialbusinesshours")) {
             $form = $event->return;
             $field = $event->object;
             $href = $this->config->urls->admin.'page/add/?parent_id='.$page;
             $field = $this->modules->get('InputfieldButton');
             $field->attr('id+name', 'add_event');
             $field->attr('class', $field->class);
             $field->attr('value', 'Add new event');
             $field->attr('href',$href);
             $field->attr('icon','plus-circle');
             $form->insertAfter($field, $form->get("AddPageBtn"));
             $form->remove($form->get("AddPageBtn"));            
            }
});

You can use the code as it is, but you have to adapt the if conditions to run it only on the templates you want. In my case I run it on 4 different templates. You can also change the icon if you want.

Hope this will be helpful for some of you!

  • Like 4
Link to comment
Share on other sites

There is a little error in your hook code:

//...
$field = $event->object;
$href = $this->config->urls->admin.'page/add/?parent_id='.$page;
$field = $this->modules->get('InputfieldButton');
//...

In this hook, the event object is ProcessPageEdit, not a field. And you also overwrite $field two lines below.

Rather than removing the existing button and adding a new one, you can change the text of the existing button. Here is another way it could be done:

$wire->addHookAfter('ProcessPageEdit::buildFormChildren', function(HookEvent $event) {
    $form = $event->return; // The InputfieldWrapper on the "children" tab
    $ppe = $event->object; // ProcessPageEdit
    $page = $ppe->getPage(); // ProcessPageEdit contains a method to get the page being edited
    if(in_array($page->template->name, ['event_events', 'event_dates', 'event_businessvacations', 'event_specialbusinesshours'])) {
        $button = $form->getChildByName('AddPageBtn'); // Get the button
        if($button) $button->value = 'Add new event'; // Change the text
    }
});

 

  • Like 5
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...