- Issue created by @Rajab Natshah
- Merge request !90Issue #3431330: Add Action Method to Add a button plugin and settings into the Active toolbar in editor β (Open) created by Rajab Natshah
- Issue was unassigned.
- Status changed to Needs review
8 months ago 2:06pm 18 March 2024 - π―π΄Jordan Rajab Natshah Jordan
First of all, thank you, for all your work on the Drupal Recipes!
Learning how to use, and how to manage action methods.
So many ideas, it feels that a recipe can so many things. ( when extended )
This is my first suggestion (playground case), which I needed recipes to target the Editor entity. not only with simple_config_update
- πΊπΈUnited States phenaproxima Massachusetts
Hiding patch in favor of MR.
- Status changed to Needs work
8 months ago 3:54pm 18 March 2024 - πΊπΈUnited States phenaproxima Massachusetts
This will need automated test coverage...
- πΊπΈUnited States phenaproxima Massachusetts
I think this is a good idea but I don't think it should be done with an ActionMethod, which is a very specialized attribute specifically targeting pre-existing entity methods. For something this specialized, we should use a dedicated, specialized config action.
- π―π΄Jordan Rajab Natshah Jordan
Thanks, Adam, for having a look
Noted;
1- No more patch files.
2- Use a dedicated config action.Exploring how to manage a config action for the editor config entity.
- π―π΄Jordan Rajab Natshah Jordan
Hetting the following error when converting to config action ( entity_method, Entity Method Deriver )
add_button_to_toolbar
(addButtonToToolbar)In DiscoveryTrait.php line 53: The "add_button_to_toolbar" plugin does not exist. Valid plugin IDs for Drupal\Core\Config\Action\ConfigActionManager are: entity_create:ensure_exists, entity_create:create, simple_config_update, entity_method:field.field:setLabel, entity_method:field.field:setLabels, entity_method:filter.format:setFilterConfig, entity_method:filter.format:setF ilterConfigs, entity_method:user.role:grantPermission, entity_method:user.role:grantPermissions, entity_method:core.base_field_override:setLabel, entity_method:core.base_fie ld_override:setLabels, entity_method:core.entity_form_display:setComponent, entity_method:core.entity_form_display:setComponents, entity_method:core.entity_view_display:setC omponent, entity_method:core.entity_view_display:setComponents recipe <path>
for
<code> name: Add OpenAI to Basic HTMl text format description: A recipe to manage default OpenAI button and plugin settings for the Basic HTMl with CKEditor 5 type: site config: actions: editor.editor.basic_html: add_button_to_toolbar: button_name: openai button_index: 1 plugin_name: openai_ckeditor_openai plugin_settings: completion: enabled: true model: gpt-4 temperature: 0.2 max_tokens: 512
- π―π΄Jordan Rajab Natshah Jordan
Trying with
addButtonToToolbar.php
in core/modules/editor/src/Plugin/ConfigActionβ/addButtonToToolbar.php<?php namespace Drupal\editor\Plugin\ConfigAction; use Drupal\Core\Config\Action\ConfigActionException; use Drupal\Core\Config\Action\ConfigActionPluginInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * @ConfigAction( * id = "editor:add_button_to_toolbar", * label = @Translation("Add button to toolbar"), * entity_types = {"editor"}, * description = @Translation("Add a button plugin and settings into the Active toolbar for a CKEditor 5 editor") * ) * * @internal * This API is experimental. */ final class addButtonToToolbar implements ConfigActionPluginInterface, ContainerFactoryPluginInterface { /** * Constructs a SimpleConfigUpdate object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * The config factory. */ public function __construct( protected readonly ConfigFactoryInterface $configFactory, ) { } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { return new static($container->get('config.factory')); } /** * {@inheritdoc} */ public function apply(string $configName, mixed $value): void { $config = $this->configFactory->getEditable($configName); if ($config->isNew()) { throw new ConfigActionException(sprintf('Config %s does not exist so can not be updated', $configName)); } $editor = $config->get(); // Check for editor: ckeditor5 . if (isset($editor['editor']) && $editor['editor'] != 'ckeditor5') { throw new ConfigActionException(sprintf('This %s editor is not a CKEditor 5 editor', $configName)); } if (empty($value['button_name'])) { throw new ConfigActionException(sprintf('Plugin name is not provided')); } $button_name = $value['button_name']; $button_index = -1; if (!empty($value['button_index'])) { $button_index = $value['button_index']; } $plugin_name = ''; if (!empty($value['plugin_name'])) { $plugin_name = $value['plugin_name']; } $plugin_settings = []; if (!empty($value['plugin_settings'])) { $plugin_settings = $value['plugin_settings']; } if (!in_array($button_name, $editor['toolbar']['items'])) { if ($button_index == -1) { $editor['toolbar']['items'][] = $button_name; } elseif ($button_index == 0) { array_unshift($editor['toolbar']['items'], $button_name); } else { array_splice($editor['toolbar']['items'], $button_index, 0, $button_name); } if ($plugin_name != '') { $editor['plugins'][$plugin_name] = $plugin_settings; } } $config->setData($editor)->save(); } }
It feels that the
\Drupal\Core\Config\Action\Plugin\ConfigAction\Deriver\EntityMethodDeriver
is not hooking the scannner for the plugin.
Or it is must have customaddButtonToToolbarDeriver.php
Still learning, and exploring Entity Method Deriver, and Config Method Deriver
Thinking of having a custom repo packages to add custom Config Actions, Config Methods, Entity Methods, or they must be located in the same config or data entity classes.Maybe if the ConfigActionManager support to scan for plugins in other module packages or vender packages
// Enable this namespace to be searched for plugins. $namespaces[__NAMESPACE__] = 'core/lib/Drupal/Core/Config/Action'; parent::__construct('Plugin/ConfigAction', $namespaces, $module_handler, 'Drupal\Core\Config\Action\ConfigActionPluginInterface', 'Drupal\Core\Config\Action\Annotation\ConfigAction');
Looked at
Information about the classes and interfaces that make up the Config Action
API.Configuration actions are plugins that manipulate simple configuration or
configuration entities. The configuration action plugin manager can apply
configuration actions. For example, the API is leveraged by recipes to create
roles if they do not exist already and grant permissions to those roles.To define a configuration action in a module you need to:
- Define a Config Action plugin by creating a new class that implements the
\Drupal\Core\Config\Action\ConfigActionPluginInterface, in namespace
Plugin\ConfigAction under your module namespace. For more information about
creating plugins, see the @link plugin_api Plugin API topic. @endlink
- Config action plugins use the annotations defined by
\Drupal\Core\Config\Action\Annotation\ConfigAction. See the
@link annotation Annotations topic @endlink for more information about
annotations.Further information and examples:
- \Drupal\Core\Config\Action\Plugin\ConfigAction\EntityMethod derives
configuration actions from config entity methods which have the
\Drupal\Core\Config\Action\Attribute\ActionMethod attribute.
- \Drupal\Core\Config\Action\Plugin\ConfigAction\EntityCreate allows you to
create configuration entities if they do not exist.
- \Drupal\Core\Config\Action\Plugin\ConfigAction\SimpleConfigUpdate allows
you to update simple configuration using a config action. - πΊπΈUnited States phenaproxima Massachusetts
Part of the problem here might be that you're using an annotation. As of #3427874: Move all ConfigAction annotations to attributes β , committed a few days ago, config action plugins now use PHP attributes for discovery.
Forget about EntityMethodDeriver - it's not relevant. :) It doesn't do the plugin scanning. The plugin system already scans installed modules for plugins.
Other than that, the code looks right to me...
- π―π΄Jordan Rajab Natshah Jordan
Got it, I see it now.
To continue playing with PHP attributes using the10.3.x
branch
Thanks, for following up and the hint. - π―π΄Jordan Rajab Natshah Jordan
Ready for any of the following:
- Better naming conventions.
- Better ways on (logic, Config Action Exception lookup messages)
- More needed config actions (remove_button_from_toolbar
,update_editor_plugin
WIP on Testing coverage
- πΊπΈUnited States phenaproxima Massachusetts
I have some feedback - this is a good start but should be using the entity API, not the simple config system.
Additionally, the MR should be filed against 11.x, not 10.3.x.
- Assigned to phenaproxima
- πΊπΈUnited States phenaproxima Massachusetts
Looks like we're actually going to need this action for a project we're working on, so self-assigning to push this forward. :)
- π―π΄Jordan Rajab Natshah Jordan
Thanks, Adam!
I agree with all your feedback points.Only drafting a needed case, and exploring what can do.
Happy with all changes.Noted;
It is needed for number of our projects/products too. - Issue was unassigned.
- Status changed to Needs review
8 months ago 4:39pm 26 March 2024 - πΊπΈUnited States phenaproxima Massachusetts
Added test coverage. :) I think this is ready for an initial review.
- Assigned to wim leers
- Issue was unassigned.
- Status changed to Needs work
8 months ago 12:18pm 29 March 2024 - π§πͺBelgium wim leers Ghent π§πͺπͺπΊ
I feel strongly about the naming "nit", because it introduces inconsistent terminology that will be confusing when creators of recipes using this config action look at
*.ckeditor5.yml
files.All my feedback is trivial to address though! π And once addressed, I'll happily RTBC π
- Status changed to Needs review
8 months ago 12:56pm 29 March 2024 - πΊπΈUnited States phenaproxima Massachusetts
Thanks for reviewing, Wim! Your feedback makes sense to me. I think I've addressed it all.
- Status changed to Needs work
8 months ago 9:54am 2 April 2024 - π§πͺBelgium wim leers Ghent π§πͺπͺπΊ
Re-read the entire MR + existing review by @phenaproxima, and that's how I discovered we're missing one bit of test coverage that ensures a good recipe authoring experience: https://git.drupalcode.org/project/distributions_recipes/-/merge_request...
- Status changed to Needs review
8 months ago 2:01pm 2 April 2024 - Status changed to RTBC
8 months ago 9:06am 3 April 2024 - π¬π§United Kingdom alexpott πͺπΊπ
alexpott β changed the visibility of the branch 3431330-add-action-method to hidden.
- Status changed to Fixed
8 months ago 11:32pm 4 April 2024 - π¬π§United Kingdom alexpott πͺπΊπ
Committed and pushed 448d98a0df9 to 11.x and 9da91eb5e17 to 10.3.x. Thanks!
-
alexpott β
committed 448d98a0 on 11.x
Issue #3431330 by phenaproxima, Rajab Natshah, Wim Leers: Add a...
-
alexpott β
committed 448d98a0 on 11.x
-
alexpott β
committed 9da91eb5 on 10.3.x
Issue #3431330 by phenaproxima, Rajab Natshah, Wim Leers: Add a...
-
alexpott β
committed 9da91eb5 on 10.3.x
- πΊπΈUnited States thejimbirch Cape Cod, Massachusetts
It doesn't look like this had a documentation follow-up ticket. Is the config action in the issue summary the final solution?
Automatically closed - issue fixed for 2 weeks with no activity.
- π―π΄Jordan n.ghunaim Amman - Jordan
Currently, I'm not able to add more than one item to the toolbar, any suggestions regarding that? do I need to have a new recipe file for each item?
[error] Duplicate key "addItemToToolbar" detected at line 16 (near " item_name: fullScreen").