Skip to content

Conversation

@rubengc
Copy link
Contributor

@rubengc rubengc commented Apr 4, 2017

Fixes #876

Added
cmb2_before_field_row
cmb2_before_{$field_type}field_row
cmb2_after_field_row
cmb2_after
{$field_type}_field_row

@rubengc
Copy link
Contributor Author

rubengc commented Apr 4, 2017

IMPORTANT!

Writing this PR I noticed a wrong hooks order that needs to be solved at CMB2.php

Hooks are called in this order:
cmb2_before_form
cmb2_before_{$object_type}_form_{$cmb_id}
cmb2_after_form
cmb2_after_{$object_type}_form_{$cmb_id}

And they should be called at this order:
cmb2_before_form
cmb2_before_{$object_type}_form_{$cmb_id}
cmb2_after_{$object_type}_form_{$cmb_id}
cmb2_after_form

Why? Because this means call this hooks is really dangerous because you could break your html if you expect a relative order, for example, if two plugins using this hooks to print something, the most common workflow is use same relative callbacks (cmb2_before_form and cmb2_after_form) or (cmb2_before_{$object_type}_form_{$cmb_id} and cmb2_after_{$object_type}_form_{$cmb_id})

A visual example with two plugins (Plugin 1 and Plugin 2):
Current order:
cmb2_before_form Plugin 1 opens a div
cmb2_before_{$object_type}_form_{$cmb_id} Plugin 2 prints a p
cmb2_after_form Plugin 1 closes the div
cmb2_after_{$object_type}_form_{$cmb_id} Plugin 2 closes the p

Result:

<div>
<p>
</div>
</p>

New order:
cmb2_before_form Plugin 1 opens a div
cmb2_before_{$object_type}_form_{$cmb_id} Plugin 2 prints a p
cmb2_after_{$object_type}_form_{$cmb_id} Plugin 2 closes the p
cmb2_after_form Plugin 1 closes the div

Result:

<div>
<p>
</p>
</div>

I keep the same order as before/after form to this PR because I do not know if this is the desired order, anyway if you confirm that you want to change this order, I will update this PR too

@jtsternberg
Copy link
Member

jtsternberg commented Apr 4, 2017

... wrong hooks order that needs to be solved at CMB2.php

This is an excellent point. Would appreciate a separate PR to resolve that order, if you are up for it. If so, please also add a changelog entry, similar to this: a03b471 (since it is a backwards-compatibility concern).

@rubengc
Copy link
Contributor Author

rubengc commented Apr 4, 2017

New hooks order to match coming CMB2 PR:
cmb2_before_field_row
cmb2_before_{$field_type}field_row
cmb2_after{$field_type}_field_row
cmb2_after_field_row

Ok @jtsternberg I will PR it in a separate PR with changelog notes

@jtsternberg
Copy link
Member

RE: this PR, i'm not super sure this is the way to handle this. Ideally, we could do something with CMB2_Field:peform_param_callback().

rubengc added 4 commits April 4, 2017 18:58
This is a backwards-compatibility break. If your theme or plugin is
using some of next action hooks and your are connecting the output
between them then take a look a this explanation

Hooks have been called in this order (until this PR):
`cmb2_before_form`
`cmb2_before_{$object_type}_form_{$cmb_id}`
`cmb2_after_form`
`cmb2_after_{$object_type}_form_{$cmb_id}`

And now they are called at this order:
`cmb2_before_form`
`cmb2_before_{$object_type}_form_{$cmb_id}`
`cmb2_after_{$object_type}_form_{$cmb_id}`
`cmb2_after_form`

Why? Because this means call this hooks is really dangerous because you
could break your html if you expect a relative order, for example, if
two plugins using this hooks to print something, the most common
workflow is use same relative callbacks (`cmb2_before_form` and
`cmb2_after_form`) or (`cmb2_before_{$object_type}_form_{$cmb_id}` and
`cmb2_after_{$object_type}_form_{$cmb_id}`)

A visual example with two plugins (Plugin 1 and Plugin 2):
Current order:
`cmb2_before_form` Plugin 1 opens a div
`cmb2_before_{$object_type}_form_{$cmb_id}` Plugin 2 prints a p
`cmb2_after_form`  Plugin 1 closes the div
`cmb2_after_{$object_type}_form_{$cmb_id}` Plugin 2 closes the p

Result:
``` html
<div>
<p>
</div>
</p>
```

New order:
`cmb2_before_form` Plugin 1 opens a div
`cmb2_before_{$object_type}_form_{$cmb_id}` Plugin 2 prints a p
`cmb2_after_{$object_type}_form_{$cmb_id}` Plugin 2 closes the p
`cmb2_after_form`  Plugin 1 closes the div

Result:
``` html
<div>
<p>
</p>
</div>
```
@rubengc
Copy link
Contributor Author

rubengc commented Apr 4, 2017

Dat github desktop :/

What do you mean @jtsternberg ?

@rubengc
Copy link
Contributor Author

rubengc commented Apr 4, 2017

I am not able to open a new PR without this field row hooks and the amount of reverts generated from github desktop app, so on get this pulled (or declined) I will folk again CMB2 repo

@rubengc
Copy link
Contributor Author

rubengc commented Apr 10, 2017

No news about this?

@rubengc rubengc closed this Apr 13, 2017
@rubengc rubengc mentioned this pull request Apr 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants