Problem/Motivation
When performing a form alter on a form extending ConfigureBlockFormBase
there currently does not appear to be a way to easily access the Section or or SectionComponent objects that the form will be modifying.
Proposed resolution
Perhaps adding a public method for getSectionStorage
to ConfigureBlockFormBase
would allow retrieving Sections via the object \Drupal\layout_builder\SectionStorageInterface
.
In addition, having a getComponent
would work for immediately returning the Section Component based on the form's Delta, and Component UUID.
Remaining tasks
User interface changes
None
API changes
Add public getSectionStorage()
and getComponent()
methods to ConfigureBlockFormBase
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#7 | 3003666-form_helpers-7-PASS.patch | 5.95 KB | tim.plunkett |
#7 | 3003666-form_helpers-7-interdiff.txt | 5.13 KB | tim.plunkett |
#7 | 3003666-form_helpers-7-FAIL.patch | 4.25 KB | tim.plunkett |
#6 | 3003666-form_helpers-6.patch | 2 KB | segovia94 |
Comments
Comment #2
tim.plunkettHere's a first attempt.
Comment #3
segovia94 CreditAttribution: segovia94 as a volunteer commentedHere is basically the same patch from #2 but with docs. I also made the parameters for
getComponent()
optional since the delta and uuid can come straight from the current object. Seems to be working well.Comment #4
segovia94 CreditAttribution: segovia94 as a volunteer commentedSame as above but with a docs fix.
Comment #5
tim.plunkettOhhh I don't know about this change. In this case, I think there should be a getCurrentSection() and getCurrentComponent() that use the internal $this->delta/$this->uuid, and take no parameters. And any other use case can use the getSectionStorage() method if they want another value. The way it is now feels confusing
Comment #6
segovia94 CreditAttribution: segovia94 as a volunteer commentedThis implements suggestions from #5
Comment #7
tim.plunkettHere's a test.
Also fixed some nits with the docs.
Comment #8
tim.plunkettI've included this to make it easier for alters, instead of having to check for layout_builder_add_block||layout_builder_update_block
Feels a bit odd putting these on a class and not an interface, but as is the case with all form classes they are always retrieved from $form_state->getFormObject() which will only ever guarantee a FormInterface, so any other methods will have to be assumed or checked for anyway.
Comment #10
bkosborneThis looks good, I was able to apply this to 8.7.x and tested that form alters have access to this from $form_state->getFormObject()->...
Comment #11
alexpottIs this going to be a common task? I'm wondering whether it would worth adding a layout_builder.api.php file with documentation for
hook_form_layout_builder_configure_block_alter()
- yes I know that we essentially re-documentinghook_form_BASE_FORM_ID_alter()
but it seems maybe worth it. Not sure - hence leaving at rtbc.Comment #12
tim.plunkettIs that a thing we do for any other form alters? I've not seen it.
And no, I don't think this will necessarily be *that* common, but common enough for anyone extending LB, or the functionality of something like block plugins.
Comment #14
catchWe've almost completely stopped referring to dynamic hooks separately, for example hook_node_load() docs use @mplements hook_ENTITY_TYPE_load(), so I don't think we should add something specific here.
Committed 9ca03e7 and pushed to 8.7.x. Thanks!
Comment #16
bkosborne@catch, should this have been committed to 8.8.x as well?
Comment #17
rodrigoaguileraIt is weird that it doesn't show in the interface but this is commited to 8.8.x