Jump to content

How to hook and change label on page save button?


JayGee
 Share

Recommended Posts

I'm trying to do something I thought would be simple, but maybe not!...

I'm trying to hook ProcessPageEdit to change the label/text displayed on the page save button in certain circumstances based on fields in that page. But can't figure if it's possible to target the button.

I can get to the button actions in the drop down with ProcessPageEdit::getSubmitActions but this array doesn't contain the top level label only the drop down values.

Can anyone point me in the right direction here please?

TIA ?

 

Link to comment
Share on other sites

Thank @wbmnfktr. I did see that thread from Ryan, but it didn't seem to cover modifying the default button, more about adding a new one.

However on second thoughts I'm going to use the drop down actions anyway as may need more than one option, but it would be good to know about editing the default label if anyone has any insight on this for future reference.

  • Like 2
Link to comment
Share on other sites

Check and try the following, I think the link provided all the infos.

/// get and mod inputfield submit
$wire->addHookBefore('InputfieldSubmit::render', function($event) {
  /// get page being edited from from query string
  $id = (int) $this->input->get->id; // ⚠️ sanitize it, do not trust your xss ninja admins 
  $page = $this->pages->get($id); 

  /// i.e. exit if page name is not 'hello'
  if($page->name !== 'hello') return;

  /// get the object the event occurred on
  $submit = $event->object;

  /// make sure to target the `submit save` button (it could be more than one button)
  $name = $submit->attr('name');
  if($name !== 'submit_save') return;

  /// change the button's label (attribute: value)
  $submit->attr("value", "Foobar");
}); 

/// mod the inputfield submit dropdown
$wire->addHookAfter('ProcessPageEdit::getSubmitActions', function($event) {
  $actions = $event->return; // array actions
 
  /// get page being edited from event's object
  $page = $event->object->getPage(); 

  /// i.e. exit if page name is not 'hello'
  if($page->name !== 'hello') return; 

  $actions['to_the_moon'] = [
    'value' => 'to_the_moon', 
    'icon' => 'moon-o', 
    'label' => '%s + Go Moon ?', // Save + Go Moon
    'class' => '', 
  ];

  $event->return = $actions;
}); 

/// process the new action dropdown
$wire->addHookAfter('ProcessPageEdit::processSubmitAction', function($event) {
  $action = $event->arguments(0); // action name, i.e. 'cache'
  if($action === 'to_the_moon') { 
      $event->message("Your page was rocketed to the moon ??", "nogroup");
  } 
});

/// remove all the dropdown action but keep our new one
$wire->addHookAfter('ProcessPageEdit::getSubmitActions', function($event) {
  $actions = $event->return; // array of actions, indexed by name 
  /* bd($actions) ?
  'exit' => array
  'view' => array
  'add' => array
  'next' => array
  */
  /// remove all action, let just keep our `to the moon` action
  foreach($actions as $action) {
    $name = $action['value'];
    if($name === 'to_the_moon') continue;
    unset($actions[$name]); 
  }
  $event->return = $actions;
}); 


/// bonus - add a second button on the current edit page
$this->addHook('ProcessPageEdit::buildForm', function($event) {
  /// get page being edited from from query string
  $id = (int) $this->input->get->id; // ⚠️ sanitize it, do not trust your xss ninja admins 
  $page = $this->pages->get($id); 

  /// i.e. exit if page name is not 'hello'
  if($page->name !== 'hello') return;

  /// quite self explanatory
  $href = $this->config->urls->admin."page/edit/?id=$id&to=the_moon";
  $field = $this->modules->get('InputfieldButton');
  $field->attr('id+name', 'input_moon');
  $field->attr('class', $field->class);
  $field->attr('value', 'To the moon ?');
  $field->attr('href', $href);

  $event->return = $event->return->append($field);
});

 

1242345341_Screenshot2022-08-23210116.png.b28880640cb66c8688a14964ef108c82.png

 

Edited by flydev ??
typo
  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Thanks @flydev ?? this is awesome. I've not even had a chance to try it out yet but I can see I had previously been doing something wrong...

/// change the button's label (attribute: value)
  $submit->attr("value", "Foobar");

Had been trying to target 'label' rather than button value... duh! Probably led me down a blind alley from the get go!

  • Like 1
  • Haha 1
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

×
×
  • Create New...