Problem/Motivation
In #3284025: Add configuration actions API we added the ability to add an attribute to config entity methods to make them available to the config actions APIs and, thereby, recipes. We added the attribute to \Drupal\user\Entity\Role::grantPermission() like so:
/**
* {@inheritdoc}
*/
#[ActionMethod(adminLabel: new TranslatableMarkup('Add permission to role'))]
public function grantPermission($permission) {
The question is what other core provided config entity methods should be available to actions?
Proposed resolution
- Determine the methods to make into actions
- Make it so
Here are the actions this MR adds, with examples for documentation:
set: Changes a property of a config entity. This is a pretty low-level method and should generally only be used if no dedicated method exists. Works on all config entities.
user.role.authenticated:
set:
property_name: label
value: Logged-in user
setMultiple: Same as set, but accepts multiple property/value pairs.
user.role.authenticated:
setMultiple:
- property_name: label
value: Logged-in user
- property_name: is_admin
value: false
enable: Marks any config entity as "enabled". The effect of this varies by the config entity type. Works on all config entities.
views.view.files:
enable: []
disable: Marks any config entity as "disabled". THe effect of this varies by config entity type. For example, disabling a view keeps it editable in the administrative UI, but makes it unavailable everywhere else. Works on all config entities.
views.view.files:
disable: []
hideComponent: Hides a component from an entity view display or entity form display.
core.entity_view_display.node.page.full:
hideComponent: uid
hideComponents: Same as hideComponent, but hides more than one component.
core.entity_form_display.media.image.default:
hideComponents:
- uid
- path
setLabel: Changes the human-readable label of a field. Works on fields and base field overrides.
field.field.node.page.field_byline:
setLabel: 'Byline for this page'
setDescription: Changes the user-facing description of a field. Works on fields and base field overrides.
field.field.node.page.field_byline:
setDescription: 'Enter the name or credit of whoever created this magnificent page.'
setTranslatable: Sets whether a field should be translatable in the UI, or not. Works on fields and base field overrides. (Note that most fields are translatable by default.)
field.field.node.page.field_byline:
setTranslatable: true
setSettings: Changes field settings. Exactly which settings are available, and what they mean, varies by the field type. Any preexisting settings are added automatically, with the incoming settings taking precedence. Works on fields and base field overrides.
field.field.node.page.field_byline:
setSettings:
display_summary: true
required_summary: true
setRequired: Sets whether users must enter a value for a field. Works on fields and base field overrides.
field.field.node.page.field_byline:
setRequired: true
setDefaultValue: Sets the default value of a field, which can be changed by users when editing content. Exactly what the default value should look like, varies by field type. Works on fields and base field overrides.
field.field.node.page.field_byline:
setDefaultValue:
value: "Joe Bag o'Donuts"
setRegion: Sets the region in which a block should be. Which regions are available depends on which theme the block is in. Only works on blocks.
block.block.olivero_powered:
setRegion: page_bottom
setWeight: Sets the weight (position relative to other blocks in the same region of the same theme) of a block. Accepts any number. Only works on blocks.
block.block.olivero_powered:
setWeight: 39
setMessage: Sets the message that a contact form should display to users when they submit the form. Only works on contact forms.
contact.form.feedback:
setMessage: 'Thanks for telling us how you feel.'
setRecipients: Sets the email addresses that should be notified when a user submits a contact form. Accepts an array of email addresses. Only works on contact forms.
contact.form.feedback:
setRecipients:
- king@monarchy.uk
- chief@example.com
setRedirectPath: Sets the path (URL) where users should be redirected when they submit a contact form. Must start with a slash. Only works on contact forms.
contact.form.feedback:
setRedirectPath: '/thank-you'
setReply: Sets a message to be emailed to the person who submitted a contact form. Only works on contact forms.
contact.form.feedback:
setReply: 'We have received your feedback and will get back to you at some point when the planets align properly.'
setWeight: Sets the weight of the contact form, relative to other contact forms, in the administrative UI. Accepts a number. Only works on contact forms.
contact.form.feedback:
setWeight: -50
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #45 | 3303127-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #43 | 3303127-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #40 | 3303127-nr-bot.txt | 5.62 KB | needs-review-queue-bot |
| #37 | 3303127-nr-bot.txt | 90 bytes | needs-review-queue-bot |
Issue fork drupal-3303127
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:
- 3303127-add-config-actions
changes, plain diff MR !7940
1 hidden branch
Issue fork distributions_recipes-3303127
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:
- 3303127-determine-which-core
changes, plain diff MR !115
Comments
Comment #2
nedjoAdded a child issue for
Config::set().Comment #3
thejimbirch commentedWhat are the "core provided config entity methods"?
Would someone be able to list, or link to a list?
Comment #4
nedjo@thejimbirch
Here are some quick pointers that I hope may be enough to get you started.
Config entity types are defined via the
ConfigEntityTypeannotation, see the relevant documentation page. To list core-provided config entity types, search api.drupal.org forConfigEntityTypeand click on the link for the annotation class with the description "Defines a config entity type annotation object." On the resulting page, click to expand "33 classes are annotated with ConfigEntityType" and then click "See full list". Then click on each of the listed types: Action, Block, and so on--though we can presumably skip anything with "Test" in its name.How do we start to decide which methods should be config actions? Here are some thoughts and suggestions.
For starters, it has to be something that modifies the config entity. Often, that's reflected in a verb that starts the method name. The most common such verb is "set", but there are others.
Once you bring up the class for a config entity type - let's take Block as an example - scroll down to the "Members" section. For "Type" enter "Function" and click "Filter". In the resulting list, you can ignore any that say "protected" under "Modifiers", and also any function starting with "get" ("getPlugin" and so on), since by definition those methods won't alter anything. Conversely, anything starting with "set" is probably a strong candidate for a config action. For Block, that starts with "Block::setRegion".
Comment #5
phenaproximaIn doing some research, I discovered another thing we'd want to grant in some situations - the ability to change the configuration options of a plugin that powers a config entity.
A good example here is adding an additional entity type or bundle to a Content Moderation workflow. In this case, the workflow itself is not really the thing to update - we need to get the workflow's type plugin, and tell it to make the change as needed. How could this be represented in config actions?
Media types are another example of a plugin-backed config entity that would need similar handling.
Comment #6
wim leersAnother example: changing filter plugin settings for an enabled filter plugin in a text format.
And AFAIK the most complex example possible: changing the CKEditor 5 plugin settings for a text editor that happens to be using the CKEditor 5 plugin — this is AFAIK the only config entity with multiple layers of indirection.
If we can make config actions work there, then we can make them work everywhere AFAICT.
Comment #7
phenaproximaFrom #3417835-14: Convert the Standard install profile into a set of recipes - it would make sense to have the setter methods of
\Drupal\contact\ContactFormInterfacebe config actions:\Drupal\contact\ContactFormInterface::setMessage()\Drupal\contact\ContactFormInterface::setRecipients()-- there should also be anaddRecipientaction (and a pluralized version of that), which adds a single recipient to an existing list.\Drupal\contact\ContactFormInterface::setRedirectPath()\Drupal\contact\ContactFormInterface::setReply()\Drupal\contact\ContactFormInterface::setWeight()Comment #8
wim leers#7: excellent info — that's exactly the kind of real-world research we need to move this forward IMO 🤩
Comment #9
phenaproximaComment #11
phenaproximaOK, I ended up taking a whirlwind tour through core and marking a lot of entity methods as action methods.
Curiously, most of them don't make sense in pluralized form (things like setting the weight, or changing the name, of an entity -- you only do that once in any given receipe, they're not really methods that take multiple values). So these all got
pluralize: FALSEin their annotation.Comment #12
phenaproximaComment #15
phenaproximaOkay, I think this is ready for a look.
I took a pretty broad approach to exposing config entity methods as actions, and added test coverage for every one of them. Some methods I omitted on purpose because they couldn't work from an API perspective, despite being useful (like
\Drupal\image\Entity\ImageStyle::deleteImageEffect); others I skipped because their usefulness is dubious (like\Drupal\search\Entity\SearchPage::setPlugin).Would be curious what people think.
Comment #16
ltrainThis looks good to me, except that I find it a little odd that in some cases we label it in agreement with the method verb:
setRequired() label = 'Set whether field is required'
...but other times instead of 'set' it's 'change'
setSettings() label = 'Change field settings'
I think I would always go with 'Set' (or whatever the verb of the method is).
I'll test all of these in a recipe at contrib day tomorrow.
Comment #17
thejimbirch commentedAm I thinking of this correctly? Based on these changes we can update following:
## Field instances:
### And we can't update:
Comment #18
phenaproximaCorrect.
Comment #19
thejimbirch commented## Block
### And we can't update:
Comment #20
phenaproximaAll correct.
It's pretty much a subjective judgment call, but I only exposed methods that I think are likely to be useful, but not cause additional complications. Complicated things need their own issues and/or dedicated config actions, so we can think through and address their implications.
Comment #21
thejimbirch commentedThat makes perfect sense. Thank you for your patience as I work through this.
It is helping my brain understand better making a list of what we can and cannot do per config type. I feel like it will make a good documentation resource also.
Comment #22
thejimbirch commented## Contact form
### And we can't update:
Comment #23
phenaproximaSo about that...
It's not really true that you can't update
label...it's just that there's no dedicated method for it on the ContactForm class. But this MR adds generalized access toConfigEntityBase::set(), so you could do this:...but that's sort of a lower-level action, so maybe a bit of an advanced use case.
Comment #24
thejimbirch commentedThat's great! Updating comment #22, thanks!
Comment #25
thejimbirch commentedUpdated the labels based on the comment in #16 and phenaproxima's thumbs up. I can't resolve the comments, but they should be closed.
Comment #26
thejimbirch commentedI believe this is everything we can do with these additions.
Block
And we can't update
Contact form
And we can't update
Field instances
And we can't update
Image
And we can't update
Language
And we can't update
Node
And we can't update
Media
And we can't update
Comment #27
thejimbirch commentedThis MR is awesome. It will really help push config actions forward. I have some questions and comments.
1. The tests are testing the additions here and helped me understand the options. However, they are far from complete. Could we update these test to cover all of the possible changes you can do per config type? Or would that be better suited for a new issue?
2. If we can do things with just the
setconfig action, do we need to have all the specificity?For example, on Contact form we can set the label name with:
Yet on Language we have
setName: foo.And on Field instances we have
setLabel: 'words'3. In the example above, we have
setNameandsetLabel. Can we decide on one for consistency?4. I made some assumptions in the documentation I put in the comment above this that we could use
set:on things. Could someone validate those will work?Comment #28
wim leers#27.3: I think the current MR is consistent with the naming of each config entity type. Sadly, not all config entity types are consistent.
I don't think it makes sense to change that here — if you want that consistency, we should be enforcing it at the config entity type level instead, and provide update paths for those config entities. That'd be a separate issue IMHO.
Comment #29
thejimbirch commentedThanks @Wim Leers. That makes sense.
Also need to document:
Comment #30
thejimbirch commentedComment #31
phenaproxima#27.2:
Yes, yes we do.
set()is an extremely dumb method; it just updates a value on the object, regardless of whether it makes any sense as passed, or needs additional massaging or processing to be valid/useful. It's handy for filling in incomplete entity APIs, but it's fundamentally a shim. The more specific methods are the ones that should be used wherever possible.#27.3:
Not with the ActionMethod annotation, we can't. Using that annotation, the config action is named for the entity method. Renaming it would (I think) require a whole separate config action, or at least some kind of alter hook.
#27.4:
Yes, we have
set:andsetMultiple:. They are both explicitly tested inEntityMethodConfigActionsTest::testSet(). However, in your comment, the syntax is wrong. To call set() a single time, it's like this:To call it multiple times:
Comment #32
thejimbirch commentedThanks for answering my questions! I updated my examples in #26 to use setMultiple.
This is a great step forward for Actions. I am marking as RTBC.
Comment #33
alexpottI finally managed to post my review of this issue - thought I did 2 weeks ago. Sorry.
Comment #34
thejimbirch commentedBased on @alexpott's review, I think the next steps are:
Move the following to their own issues/MRs
* core/lib/Drupal/Core/Field/FieldConfigBase.php - Remove component
* core/lib/Drupal/Core/Field/FieldConfigBase.php - Set default value
"This needs validation" - does that mean in this MR, or break this one out also?
* core/modules/block/src/Entity/Block.php - Set block region
Comment #35
thejimbirch commentedSlack convo:
https://drupal.slack.com/archives/C2THUBAVA/p1719847206878749
TLDR: Keep, but move into its own Issue/MR
phenaproxima
1 day ago
I don’t think this can be considered a truly “destructive” operation in the sense that it can break the site.
alexpott
1 day ago
Well it could break a site - what happens if you remove a required field’s widget?
alexpott
1 day ago
But I do get the point. What component are you wanting to remove?
phenaproxima
1 day ago
Well, one example in Starshot’s prototype is, say, a geofield that stores latitude and longitude.
phenaproxima
1 day ago
If I’m geocoding from an address field, then I want to hide the geofield.
alexpott
1 day ago
So you want to add the field and remove the components straight away?
phenaproxima
1 day ago
Well, in my case, I don’t want the field to show up at all, it’s just a data store.
phenaproxima
1 day ago
So, yes.
phenaproxima
1 day ago
I am, to be clear, totally fine with deferring removeComponent to a follow-up.
phenaproxima
1 day ago
But I’m about 99% sure we are going to want it.
alexpott
1 day ago
Sure let’s leave it in. Maybe add some docs about trying to only use it in recipes that add the field that is being removed. At the end of the day we’re going to hit things like this where the pragmatic solution is to allow and indicate how it should be used.
phenaproxima
1 day ago
Agreed.
phenaproxima
1 day ago
I have overall been totally on board with not adding destructive things, but I favor usefulness. Removing components is something I have to do quite often (edited)
thejimbirch
1 day ago
Would a better word be unSetComponent(s)? (edited)
mikelutz (he/him)
:4k: 23 hours ago
Just call it iDontKnowWhatYouAreTalkingAboutINeverSawAComponentThere()
:stuck_out_tongue_closed_eyes:
1
mandclu
21 hours ago
In the geofield example, wouldn't you be hiding the field instead of removing it?
mandclu
21 hours ago
That looks like it's exactly what the code does. Maybe calling it hideComponent instead would make it more clear that it's nondestructive?
thejimbirch
20 hours ago
Thats better than my suggestion.
phenaproxima
20 hours ago
@mandclu
removeComponent is the name of the method. ActionMethod actions use the name of the method AFAIK. Not sure if there’s a way to override that
:+1:
1
alexpott
8 hours ago
There is no way to override it (yet). We could add that but it decouples us from the API which could get confusing. Tricky.
mandclu
6 hours ago
I mean, we did recently rename ensure_exists to createIfNotExists so it's not like renaming things is impossible
alexpott
6 hours ago
@mandclu
totally. But that is it’s own action so less tied to the config entity’s existing API.
Comment #36
b_sharpe commentedCommented on the EntityDisplay. Re: the "needs validation", I'm wondering what the approach should be here. For example, there's technically nothing stopping someone from just doing the following on their own:
Why should recipes be any different? It's up to the recipe author IMO to ensure validity of the data they pass to a config action.
Comment #37
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #38
phenaproximaComment #39
phenaproximaAssigning to myself to do some cleanup here.
Comment #40
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #41
phenaproximaComment #42
phenaproximaUpdating the issue summary with an partial list of every action added by this merge request, with examples for documentation.
Comment #43
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #44
phenaproximaComment #45
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #46
phenaproximaComment #47
b_sharpe commentedTests are passing, Confirmed aliasing works as expected, noted other concerns, all others addressed. Looks great!
Comment #48
alexpottAdded a comment to the MR.
Comment #49
phenaproximaFixed @alexpott's feedback. Since it was literally just renaming a parameter, I don't think we need to shuffle through the whole needs review process for this. Restoring RTBC.
Comment #50
alexpottI think we need to apply some validation to the name parameter... let's make it a valid PHP function name. Excludes things like unicode and means it has to start with a letter which will be good for yaml keying.
We should add test coverage for the attribute name validation to core/tests/Drupal/Tests/Core/Config/Action
Comment #51
alexpottComment #52
thejimbirch commentedTests pass and feedback addressed. Marking as RTBC.
Comment #53
alexpottCommitted d9a1ec2 and pushed to 11.x. Thanks!
Comment #56
alexpottCherry-picked back to 10.4.x so recipes on 10.4 and 11.1 are mostly compatible,