Problem/Motivation
Many advanced modules (IEF, paragraphs, address) etc. use form elements to expand simple form structures to their complex internal elements. Once you have these elements using hook_form_alter no longer works and you need to switch to an alternative:
- Use #after_build
- Use #process (probably not what you want)
Proposed resolution
Add some documentation on hook_form_alter to allow people to point to the alternatives.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|
Issue fork drupal-2909574
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
th_tushar commentedComment #3
ivan berezhnov commentedComment #5
j. ayen green commentedTaking a look at this. If it's already in-hand, please let me know.https://www.drupal.org/project/drupal/issues/2909574#
Comment #6
msankhala commented@dawehner Can you please explain this bit further?
Comment #7
johnny_aroza commentedI have added the doc above the hook_form_alter api function and added the patch.
Comment #10
dksdev01 commentedWorking on updated patch with updated text. thanks
Comment #11
dksdev01 commentedMessage updated as below:
Comment #12
larowlanPlease don't RTBC your own patch, thanks
Comment #13
sumit-k commented@dawehner can you explain little bit more to replicate issue or give example cases ?
Comment #16
harpreet16 commentedComment #17
harpreet16 commentedThe patch applies successfully with Drupal 9.1.x as well
.
The patch was tested by applying it v.i.a composer using "composer install" after adding below lines within "extra" of the composer.json file
Comment #18
harpreet16 commentedComment #19
harpreet16 commentedComment #20
dagmarI think this issue is not enough complete to be RBTC.
I few question I would like to see documented are:
Do we have any better documentation to link? I was only able to find:
core/lib/Drupal/Core/Render/Element/FormElement.php
Which are basically the same thing without specifying any relevant difference.
Comment #21
dagmarRemoving Novice tag until we have the answers to the question I made.
Comment #22
dagmarI think most of my questions can be answered by writing in text what this diagram shows: https://www.drupal.org/docs/7/api/form-api/form-api-internal-workflow-il...
Comment #24
johnalbinGood find, Mariano!
Here's my comment from #1300290: In drupal_render(), element_info is not merged recursively, preventing #type from serving its purpose that is relevant:
Obviously, we shouldn't document this like I stated above. There are a lot of places we can alter a form. I think it makes sense to skip preprocess functions since you can technically do this, but it's more logically placed with theming/rendering. I think we should make an except for element info and pre-render, though.
Then for each element in the form:
Comment #25
johnalbinAh, man. I'm going through
Drupal\Core\Form\FormBuilder::doBuildForm()and I just discovered that, while Drupal's render does call\Drupal::service('element_info')->getInfo(), the FormBuilder calls this same method before render is called. doBuildForm also recursively calls itself for each form element. Which means the order I specified in my previous comment is completely wrong.I'll report back shortly.
Comment #26
johnalbinThis is maybe too much info, but here's what I figured out from the code in FormBuilder.php.
Comment #28
johnalbinI created a merge request for this issue.
Comment #29
johnalbinComment #30
jhodgdonInteresting how that shows up! I reviewed the merge request on the merge request page....
Comment #31
johnalbinI've made the changes Jennifer suggested.
Comment #32
jhodgdonAdded some more comments for the latest patch, which mostly looks good. Sorry if I missed these before...
Comment #33
johnalbinNo worries. Your comments have been very helpful!
Comment #34
jhodgdonThanks for all the iterations and attention to detail! Looks good to me. I'm going to change the component to Documentation at this point, as sometimes committers are looking for something quick to commit and the MR is not making any code changes.
Comment #35
catchLooks good but a couple of minor comments.
Comment #37
anmolgoyal74 commentedComment #38
jhodgdonThanks, it's getting closer! I think it needs some work though...
Comment #41
srilakshmier commentedFixed punctuation issue based on the previous comment.
Comment #45
joachim commentedReally useful addition to docs, just some nitpicks!