Problem/Motivation
A common configuration action requirement is to add a field to an existing entity form or view display.
For example, in #3301370: Model core's standard install profile as recipes, to make the tags field an optional enhancement to the article content type, an article_tags recipe would add a tags field to entity displays provided by an article recipe.
Proposed resolution
Expose EntityDisplayBase::setComponent() as a config action.
Sample action:
actions:
core.entity_view_display.node.article.default:
setComponent:
field_tags
-
type: entity_reference_label
label: above
settings:
link: true
third_party_settings: { }
weight: 10
region: content
Singular:
core.entity_view_display.node.page.default:
setComponent:
name: field_content
options:
type: entity_reference_revisions_entity_view
label: hidden
settings:
view_mode: default
link: ''
third_party_settings: { }
weight: 10
region: contentPlural
core.entity_form_display.user.user.default:
setComponents:
-
name: field_last_password_reset
options:
type: datetime_default
weight: 4
region: content
settings: { }
third_party_settings: { }
-
name: field_password_expiration
options:
type: boolean_checkbox
weight: 3
region: content
settings:
display_label: true
third_party_settings: { }| Comment | File | Size | Author |
|---|---|---|---|
| #12 | test-recipes.zip | 9.91 KB | joegraduate |
Issue fork distributions_recipes-3304324
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
nedjoComment #3
nedjoComment #6
alexpottAdded an attribute for this. Doing that opened up an interesting discussion. What should we do if a class overrides an implementation which is annotated. We have exactly this case with \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent() and \Drupal\Core\Entity\EntityDisplayBase::setComponent(). I'm currently checking the parent class. This however will be interesting if LayoutBuilderEntityViewDisplay::setComponent() has already added the attribute to its override - because then with this would fall foul of the duplicate check in \Drupal\Core\Config\Action\Plugin\ConfigAction\Deriver\EntityMethodDeriver::addDerivative()....
Hmmm... I think either we need to remove the duplicate check or we should revert the parent-based discovery and just say that all methods including overrides must have the attribute.
I'm not sure what the right answer is...
Comment #8
markie commentedon reviewing, I am not sure what needs to be done to validate the code changes. Can we get some steps to reproduce or test instructions please.
Comment #9
thejimbirch commentedAs a developer of a site, I need to be able to add an entity reference field to a tags taxonomy from an article content type.
Validations steps/Acceptance criteria
Comment #10
joegraduateSeveral of us a DrupalCon Pittsburgh are looking into this one (and trying to figure out how to review it).
Comment #11
joegraduateComment #12
joegraduateI was able to verify that this config action works by doing the following:
php core/scripts/drupal recipe <recipe-name>for thearticle_content_typeandtags_taxonomyrecipesphp core/scripts/drupal recipe <recipe-name>for thearticle_tagsrecipeI also pushed the important changes related to this action that I had to make to the WIP recipes copied from the #3301370: Model core's standard install profile as recipes issue branch up to that branch. The diffs can be seen here:
https://git.drupalcode.org/issue/distributions_recipes-3301370/-/commit/...
https://git.drupalcode.org/issue/distributions_recipes-3301370/-/commit/...
For testing this MR, I also removed the image field from the article recipe (see the zip file) to simplify things.
Comment #13
joegraduateComment #14
thejimbirch commentedThanks Joe! Will try to get this reviewed and merged this week.
Comment #16
thejimbirch commentedComment #17
thejimbirch commentedConfig Action
setComponentadded! Thanks all!Comment #19
ltrainI'm trying to use this to add a field to existing form and view displays using the sample action here, but I get this error:
Comment #20
ltrainMy bad! Pro tip: use spaces rather than tabs in yaml.
Comment #21
ltrainAlso, the machine name of the field has to be in quotations.
Comment #22
ltrainThis adds the field, but it strips out all of the settings and third party settings. I'm wondering if there's another function I can use to add those...
https://git.drupalcode.org/project/distributions_recipes/-/blob/11.x/cor...
Comment #23
thejimbirch commentedI have an open issue to update the docs:
https://www.drupal.org/project/distributions_recipes/issues/3395840
But yours is a little different.
Does this work plural also?
Comment #24
ltrainThis works:
This works with setComponents(): from here
Comment #25
thejimbirch commentedUpdating issue summary with the final code.