Cmb2: Custom/Dynamic Field Group Title/Labels

Created on 23 Aug 2015  路  8Comments  路  Source: CMB2/CMB2

Hey

Devin Walker and Matt Cromwell are working on their new plugin google-maps-builder
They are using accordion kind of thing they call markers. One issue is the following:
https://github.com/WordImpress/google-maps-builder/issues/88

A list of markers are seen that can be clicked on that opens as accordions.
x Marker: 1
x Marker: 2
x Marker: 3
x Marker: 4
x Marker: 5

I asked if these could be renamed to whatever the user filled out the Marker title to become. But the response was that it is not possible yet with CMB2.

Have a great day!

<div class="cmb-td"><div id="gmb_markers_group_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group sortable repeatable" style="width:100%;"><div class="cmb-row cmb-group-description"><div class="cmb-th"><h2 class="cmb-group-name">Existing Markers</h2><p class="cmb2-metabox-description">Map marker data is contained within the repeatable fields below. You may add or update marker data here or directly on the map.<a href="#" class="button button-small toggle-repeater-groups">Toggle Marker Groups</a></p></div></div>
        <div class="postbox cmb-row cmb-repeatable-grouping" data-iterator="0"><button data-selector="gmb_markers_group_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row"></button>
            <div class="cmbhandle" title="Click to toggle"><br></div>
            <b><h3 class="cmb-group-title cmbhandle-title"><span>Marker: 1</span></h3></b>

            <div class="inside cmb-td cmb-nested cmb-field-list"><div class="cmb-row cmb-type-text cmb2-id-gmb-markers-group-0-title cmb-repeat-group-field table-layout">
<div class="cmb-th"><label for="gmb_markers_group_0_title">Marker Title</label></div>
Feature Request

Most helpful comment

Why not just refactor replace_hash into something more substantial? (either with an option or a CB to set/alter the output)

The CMB2_Field class is where you can handle the index. This is where you deal with the title specifically (or just change that part specifically so that replace_hash is the default action and a CB/option is an alternate so it doesn't break anything)

This isn't meant to be a real world example, but I just replaced the contents of the replace_hash function with:

return $this->value[$this->index]["shortcode_title"];

Where "shortcode_title" is the name of my property I want to replace the title with. It worked just fine. As mentioned earlier, updating the title before you hit "update" to save the page is trivial with Javascript.

I haven't touched sorting at all. (because I have no idea if it's even active/how it works tbh)

All 8 comments

This could be done with some custom javascript, but is not part of CMB2. That's a very specific implementation and it's unlikely it would be added any time soon.

Thanks for the feedback Justin!

I feel a LOT of users could find this type of functionality useful. Adding a more description toggle for repeatable groups sounds like it should be CMB2 core. Otherwise, everyone will be left with generic labels and increments. :(

The problem is that it's not a simple php callback or something like that because it will also have be generated on the JS side when adding, and removed when removing, and replacing when sorting, etc. There isn't a simple solution and with all the other bugs/feature requests, I remain unconvinced that this should somehow take priority. Also, the repeatable group JS is in need of a major refactor and I'm reluctant to bolt any more functionality onto it. Like I said, for custom use-cases, this could be done somewhat trivially with JS, but IMO there's not a simple, straight-forward and flexible way to add it to core.

I understand your apprehension there and having worked closely with the repeater JS know where you're coming from. Let me just cast my vote for this in the future once the repeater is stabilized.

Oh we're back in business! :+1:

Very interested in being able to use a variable entry instead of a variable number (like a group field value.) I'm thinking this probably could be done in the same way 'show_on' works - where it only goes in to effect AFTER the post(whatever) is saved.

Bear in mind I'm thinking of this only in an aesthetic sense - where we're not actually replacing keys or anything - just flipping out {#} for some other value.

Can you advise where I can find the bits of code which deal with group numbering? Maybe I can dive in and take some load off yer back.

Why not just refactor replace_hash into something more substantial? (either with an option or a CB to set/alter the output)

The CMB2_Field class is where you can handle the index. This is where you deal with the title specifically (or just change that part specifically so that replace_hash is the default action and a CB/option is an alternate so it doesn't break anything)

This isn't meant to be a real world example, but I just replaced the contents of the replace_hash function with:

return $this->value[$this->index]["shortcode_title"];

Where "shortcode_title" is the name of my property I want to replace the title with. It worked just fine. As mentioned earlier, updating the title before you hit "update" to save the page is trivial with Javascript.

I haven't touched sorting at all. (because I have no idea if it's even active/how it works tbh)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bomsn picture bomsn  路  5Comments

mathetos picture mathetos  路  8Comments

gorirrajoe picture gorirrajoe  路  4Comments

abuyoyo picture abuyoyo  路  7Comments

stabilimenta picture stabilimenta  路  8Comments