• Resolved ramblurr

    (@ramblurr)


    Hi,

    I’d like to customize the group_title field to display something asides from a static string with a number.

    Ideally, I’d like to specify a callback that gets the group as an argument and has the chance to return a title based on the contents of the group field’s values.

    If it was empty, then a default title would suffice.

    Any tips on how to do this?

    https://www.ads-software.com/plugins/cmb2/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter ramblurr

    (@ramblurr)

    Alright, I’ve hacked it up myself.

    I changed the
    In CMB2_Field.php, I changed the replace_hash function to be:

    public function replace_hash($value)
    {
        if (!$this->options('group_title_callback')) {
    
            // Replace hash with 1 based count
    
            return str_ireplace('{#}', ($this->count() + 1) , $value);
        }
        else {
            $fields = array();
            foreach(array_values($this->args('fields')) as $field_args) {
                $field = new CMB2_Field(array(
                    'field_args' => $field_args,
                    'group_field' => $this,
                ));
                $fields[$field_args['id']] = $field->value;
            }
    
            return call_user_func($this->options('group_title_callback') , $fields);
        }
    }

    Then in my metabox setup, I set:

    'group_title' => 'New Widget',
    'group_title_callback' => array($this, 'format_group_title'),

    and finally my formatting callback that sets the title based off the field values:

    public function format_group_title($fields) {
            return $fields['time'] . ' ' . $fields['name'];
    }

    This results in new items having the title “New Widget”, and existing groups getting an informative title based off two fields.

    It’s a bit of a hack, is there a way to do this without editing CMB2? I’d rather not break automatic updates and maintain my own fork.

    Edit: improved slightly

    Plugin Author Justin Sternberg

    (@jtsternberg)

    As I mentioned in the subsequent pull request, this desired functionality will need to be a custom Javascript solution.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    My only idea at this point involves filtering the gettext for the 3 spots, but I’m not sure that’s going to provide you enough to affect all the text you need, especially any that trickle down to other parts.

    Have you ever done any work with WP Nav Walker extending? I’m curious if you can do a similar thing for the callback here.

    Would love to see this implemented +1

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Customize group_title with callback’ is closed to new replies.