Home > Net >  Modify admin configuration values on save
Modify admin configuration values on save

Time:12-13

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?

CodePudding user response:

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'];
    
      foreach ($topics as &$topic) {
        if ($topic['unique_id'] === 'generate_on_save') {
          $topic['unique_id'] = str_rot13($topic['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
  • Related