简体   繁体   中英

Modify admin configuration values on save

I have created a configuration form in Grav's admin panel, and I want to extend/modify some of it's values on save. More precisely, I have a list form element that looks like this in the blueprint:

topics:
  type: list
  fields:
    .name:
      type: text
    .unique_id:
      type: text
      readonly: true
      default: generate_on_save

On save, I want to replace all generate_on_save values with a unique id. I tried to hook into the onAdminSave Event, but the Event Object contained just an instance of \Grav\Common\Data\Blueprint and no actual form data. I then tried to modify the request object, but when I register the modified request in the grav container, I get an error Cannot override frozen service 'request' .

How can I accomplish this task?

I did the following which works fine:

  • In config file /user/themes/quark/blueprints.yaml , I copied your field definition.
  • In Admin I added some topics on the config page of theme Quark.
  • The 'Save' action was captured by the following eventhandler:
     public function onAdminSave(Event $event) { /** @var Data */ $form = $event['object']; $topics = $form['topics']; // Note: foreach won't work for($i = 0; $i < count($topics); $i++) { if ($topics[$i]['unique_id'] === 'generate_on_save') { $topics[$i]['unique_id'] = str_rot13($topics[$i]['name']); } } // Note: Updated $form['topics'] must be re-assigned $form['topics'] = $topics; }
  • The topics with there "unique" values have correctly been written to /user/config/themes/quark.yaml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM