There is a Drupal 7 tutorial on Drupal.org about: How to add a column with dynamically generated action buttons in a View.
Does anybody know how to do this for Drupal 9?
I have read Actions are now plugins and Actions are now plugins, configured actions are configuration entities, and have been able to create the plugin (It changes the value of a boolean field). The plugin shows up when I navigate to Manage » Configuration » System » Actions, and I am able to configure it and I am even able to use it if I navigate to Manage » Content and select it from "Actions" and click "Apply to selected items".
However, I want to be able to invoke this custom action for a node from an "Action" column that appears in a View. How can I invoke this actions in a View I create using the GUI?
Comments
You may look at Custom
You may look at the Custom actions in Drupal 8/9 article. Also, maybe the "dropbutton" filelds can be sutable.
What to put in the custom link field?
The method suggested by the "Dropbutton" answer looks promising. However, it just says: 'output as custom link'. It does not tell you what link to use to get the custom action invoked.
I guess that's the real question here. What to put in the custom link field?
I hoped to get some information from Route UI, but no joy so far, re: #3278661: Provide more documentation for Route UI
I am unable to see the relevance of the Alan Saunders article. He explains how to use the VBO apply to custom action. I want to do it directly in Views. There is nothing in it that reveals what the link to the action is. Maybe I'm missing something. If so please point to the thing I'm overlooking.
- gisle
If you provide more info
If you provide more info about your use case we'll try to figure something out.
More info about my use case
I am looking for a version of this Drupal 7 tutorial for Drupal 9: How to add a column with dynamically generated action buttons in a View.
The use case is to use an Action column in a View to add a user to another user's contact list. A "Contact" is a custom node with three fields: Entity relations to the two users involved and a boolean field named "Accepted" that defaults to FALSE. There is a View that shows a user all nodes where he/she is named where the field "Accepted" is FALSE. All this is already working. I also have a working Plugin with a custom Action that flips the field "Accepted" in a node instance to TRUE.
In the View that shows a user all the pending "Contact" nodes, I the view I want to have a column named "Action" that has a plain link (or a Dropbutton) to invoke this custom action when the user clicks on the link or Dropbutton.
I believe this is somehow related to routing, and that I need to create a route to this custom action by adding a
mymodule.links.action.ymland possibly also amymodule.route.yml, and then figure out the the local path from the route, and put that link in the custom link field as described in the "Dropbutton" answer you linked to.My guess is that this local path does not exist until properly configured YAML files for the action and route exists. What I am banging my head against is how to configure these YAML-files for my custom action. I think this is part of the Menu API is relevant: Providing module-defined local actions – but so far, I have been unable to convert that example to something that's working for my custom action.
- gisle
By following the guide I
By following the guide I shared, you can give the option to select multiple contacts and allow them. Also, the "Views Bulk Operations (VBO)" module is optional. For example, on the screenshots 1 and 2 below, I didn't use it:
1. https://www.drupal.org/files/custom_actions-1.png
2. https://www.drupal.org/files/custom_actions-2.png
But if you want just to have the links in separate columns, the one way is:
1. Create the controller that receive a node as argument.
2. Create the route with parameter.
3. Add to the view the nid field.
4. Open the "REWRITE RESULTS" section in the field's configuration.
5. Check the "Owerride the output of this field" option.
6. Create the link to your controller: enter the HTML code of the <a> tag using the Replacement pattern (the Twig syntax is using). The example:
The screenshots:
3. https://www.drupal.org/files/custom_actions-3.png
4. https://www.drupal.org/files/custom_actions-4.png
References
1. Using parameters in routes,
2. Introductory Drupal routes and controllers example.
Bad terminology: It is about a custom operation
I am afraid that my use of the phrase "custom action" created a red herring. It turns out that what I want to set up a columns with custom operations with Views. In this case, it turns out that the words "actions" and "operations" refer to very different things. My bad.
It turns out that you have already provided an excellent answer in this forum. And the answer is: Use
hook_entity_operation()to set up the custom operation for the node. Your sandbox (upgraded to Drupal 9) works and gives me working code to build on.Thank you so much for your patience and guidance, and apologies for the red herring.
- gisle