These are chat archives for WebDevStudios/CMB2

5th
Jul 2016
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:37
Hey everyone, how can I name my group field based on a child field? I have a title inside a group field, and I want the group's title to be the value of the title field within the group. Make sense?
Justin Sternberg
@jtsternberg
Jul 05 2016 15:39
I think I understand what you're saying. Is it a repeatable group? if so, how do you decide which title in the repeatable group to use? And either way, You'd have to both change it in PHP, but also listen in JS when new groups are added or the value is changed.
(unless it's not a repeatable group)
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:44
@jtsternberg Thank you for your help, it is a repeatable group, but I also will have a default title for the group, so it will only have to update the title when the page is saved. If the title (the title field within the group) is changed, the group title should be the same when the page is updated. If there is no specified title, the group will just have a default name.
Justin Sternberg
@jtsternberg
Jul 05 2016 15:45
what's your code?
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:47
This message was deleted
$group_field_id = $box->add_field( array( 'id' => $prefix . 'group', 'type' => 'group', 'options' => array( 'group_title' => __( 'Snippet {#}', 'cmb2' ), // {#} gets replaced by row number 'add_button' => __( 'Add Another Snippet', 'cmb2' ), 'remove_button' => __( 'Remove Snippet', 'cmb2' ), // 'closed' => true, ), ) );
Justin Sternberg
@jtsternberg
Jul 05 2016 15:47
can you put it in a gist?
or do whatever @DevNIX did to get the pretty formatted output :P
Dev_NIX
@DevNIX
Jul 05 2016 15:48
just two ```
:)
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:48
This message was deleted
This message was deleted
Justin Sternberg
@jtsternberg
Jul 05 2016 15:49
that's not all your code. where's the sub-fields?
@jtsternberg This is what I tried, see when I added the group, I tried using ternary operator to use the title of the group field.
Justin Sternberg
@jtsternberg
Jul 05 2016 15:55
looking
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:56
@jtsternberg thanks so much
Justin Sternberg
@jtsternberg
Jul 05 2016 15:56
where are you setting $box->title?
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:57
I'm not, I just put that in there to explain to you what I'm trying to do.
Justin Sternberg
@jtsternberg
Jul 05 2016 15:57
oh
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:57
sorry
Justin Sternberg
@jtsternberg
Jul 05 2016 15:57
which group title are you expecting to use?
the first group's?
the last?
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 15:58
I want to use the value of the "title" group field as the "group title".
Justin Sternberg
@jtsternberg
Jul 05 2016 15:59
right, but it's a repeatable group
so there could potentially be X number of "title" group fields
X is the number of groups you've created
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:00
because the "title" field is a child of the group, EVERY group would have it's title set by it's "title" field.
Does that make sense?
Justin Sternberg
@jtsternberg
Jul 05 2016 16:01
Yes, I know how groups work :P
again, you'd have a "title" field for each iteration of the group
which can have a separate value
and from what I understand, you want the 'title' parameter in the group field registration to be one of those title values
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:04
Yes.
See how the group name is "Snippet 1"? It should be "Heading 1".
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:10
Thanks sooo much, @jtsternberg
Justin Sternberg
@jtsternberg
Jul 05 2016 16:11
Your screenshot is cut off, but there should be a "Add another" button below there: af68515 http://b.ustin.co/YALw
ah, hang on
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:13
Yes, there is. Want me to get a better screenshot?
Justin Sternberg
@jtsternberg
Jul 05 2016 16:13
no, I think I'm following now
(I was confused)
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:13
Code is hard to explain!
Justin Sternberg
@jtsternberg
Jul 05 2016 16:14
I forgot the repeatable group UI 😳
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:16
What if I used a simple if block right before I got to this section in the code. I could do something like: $group_title = get_post_meta( get_the_ID(), $prefix . 'group_title', true );
Justin Sternberg
@jtsternberg
Jul 05 2016 16:17
won't work.. It's only called once, and you want it called for each block
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:18
Oh, I see.
How will I even access the value of the grouped fields?
get_post_meta( get_the_ID(), $prefix . 'group_title', true );
isn't working.
Justin Sternberg
@jtsternberg
Jul 05 2016 16:44
get_post_meta( get_the_ID(), $prefix . 'group', true );
but you're going to want to use JS
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:46
Why? Is that not possible with the plugin as it already is?
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 16:57
Anyway, thanks for your help @jtsternberg!
Justin Sternberg
@jtsternberg
Jul 05 2016 16:58
no, because the group title only gets called once per group. I'll put a snippet in the snippet library in a bit.
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 17:24
This message was deleted
Thanks!
Unfortunately, I spent too much time on this already, so hopefully it's self-explanatory
Godisgoodtoyou
@Godisgoodtoyou
Jul 05 2016 17:57
@jtsternberg You are a life-saver. Thanks a million!