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: content

Plural

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: {  }
CommentFileSizeAuthor
#12 test-recipes.zip9.91 KBjoegraduate
Command icon 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

nedjo created an issue. See original summary.

nedjo’s picture

Issue summary: View changes
nedjo’s picture

Issue summary: View changes

alexpott made their first commit to this issue’s fork.

alexpott’s picture

Status: Active » Needs review

Added 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...

Rajeshreeputra made their first commit to this issue’s fork.

markie’s picture

Status: Needs review » Needs work

on 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.

thejimbirch’s picture

Status: Needs work » Needs review

As 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

  1. Create a "Tags" taxonomy
  2. Create a recipe that creates/installs an Article Content type and uses the setComponent config action to add a field_tags reference field from the content type's entity_view_display and entity_view_form configs to the taxonomy term
  3. Apply the recipe and validate that it installs successfully and creates the field, and adds it to the content type's display and form
joegraduate’s picture

Several of us a DrupalCon Pittsburgh are looking into this one (and trying to figure out how to review it).

joegraduate’s picture

joegraduate’s picture

StatusFileSize
new9.91 KB

I was able to verify that this config action works by doing the following:

  • Cloned the issue branch/fork locally
  • Adapted some of the work-in-progress recipes from #3301370: Model core's standard install profile as recipes (modified versions attached to this comment)
  • Installed Drupal minimal install profile
  • Ran php core/scripts/drupal recipe <recipe-name> for the article_content_type and tags_taxonomy recipes
  • Ran php core/scripts/drupal recipe <recipe-name> for the article_tags recipe
  • Verified that field_tags had been added to the displays for the article content type

I 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.

joegraduate’s picture

Status: Needs review » Reviewed & tested by the community
thejimbirch’s picture

Thanks Joe! Will try to get this reviewed and merged this week.

thejimbirch’s picture

thejimbirch’s picture

Status: Reviewed & tested by the community » Fixed

Config Action setComponent added! Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ltrain’s picture

I'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:

In YamlSymfony.php line 40:

  Mapping values are not allowed in multi-line blocks
ltrain’s picture

My bad! Pro tip: use spaces rather than tabs in yaml.

ltrain’s picture

Also, the machine name of the field has to be in quotations.

    core.entity_view_display.node.page.default:
      setComponent:
        'field_content'
        -
          type: entity_reference_label
          label: above
          settings:
            link: true
          third_party_settings: {  }
          weight: 10
          region: content
ltrain’s picture

This 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...

thejimbirch’s picture

I 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?

ltrain’s picture

This works:

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: content

This works with setComponents(): from here

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: {  }
thejimbirch’s picture

Issue summary: View changes

Updating issue summary with the final code.