Update field settings after save post

Support General Update field settings after save post

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #40913
    Narcis NagyNarcis Nagy
    Participant

    Hi, i was wondering if the following Scenario would be possible:
    i have a group field like:

        'id'          => 'group_id',
        'type'        => 'group',
        'clone'       => true,
        'fields'      => [
          [
            'id'                => 'text_id',
            'type'              => 'text',
            'required'          => true,
          ],
        ]
    

    and i would like to update the "text_id" settings after the post has been saved, namely, i want to make that field readonly

    I tried the following:

    the group is wrapped in a field with id 'XXX' and i used rwmb_after_save_post to update some other field values but it doesnt seem to be possible to update field settings

    add_action('rwmb_XXX_after_save_post', function($post_id) {
      $group_settings = rwmb_get_field_settings('group_id');
      foreach ($group_settings['fields'] as $field) {
        if ($field['id'] === 'text_id') {
          ...
        }
      }
    }, 100);

    maybe a function named rwmb_set_field_settings

    #40923
    PeterPeter
    Moderator

    Hello,

    Currently, it is not possible to change the field settings after saving another field like that. If you want to modify the field settings, please use the filter hook rwmb_normalize_field, please read more on the documentation https://docs.metabox.io/filters/rwmb-normalize-field/

    #40928
    Narcis NagyNarcis Nagy
    Participant

    rwmb_normalize_field does not seem to work with a clonable group subfield
    i have tried it like this:

    add_filter( 'rwmb_normalize_group_id_field', function($field) {
      foreach($field['fields'] as $f) {
        if ($f['id'] === 'text_id') {
          $f['readonly'] = true;
        }
      }
      return $field;
    });

    rwmb_normalize_field works with other fields that are not in a group

    i hvae als tried rwmb_normalize_meta_box but no success

    #40945
    PeterPeter
    Moderator

    Hello,

    You should use the $field variable to set the field setting instead of mapping variable $f. The correct code should be like this

    add_filter( 'rwmb_normalize_group_id_field', function( $field ) {
        foreach($field['fields'] as $key => $f) {
            if ($f['id'] === 'text_id') {
              $field['fields'][$key]['readonly'] = true;
            }
        }
        return $field;
    } );
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.