Skip to content

Commit

Permalink
Merge pull request #144 from Chi-teck/action-plugin-attribute
Browse files Browse the repository at this point in the history
Use PHP attribute for action plugins
  • Loading branch information
Chi-teck committed Apr 14, 2024
2 parents 5dc84d5 + 6fef58a commit 0acf136
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
15 changes: 8 additions & 7 deletions templates/Plugin/_action/action.twig
Expand Up @@ -7,7 +7,9 @@ namespace Drupal\{{ machine_name }}\Plugin\Action;

{% sort %}
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if configurable %}
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Form\FormStateInterface;
Expand All @@ -26,13 +28,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides {{ plugin_label|article }} action.
*
* @Action(
* id = "{{ plugin_id }}",
* label = @Translation("{{ plugin_label }}"),
* type = "{{ entity_type }}",
* category = @Translation("{{ category }}"),
* )
*
* @DCG
* For updating entity fields consider extending FieldUpdateActionBase.
* @see \Drupal\Core\Field\FieldUpdateActionBase
Expand All @@ -47,6 +42,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* The whole action API is subject of change.
* @see https://www.drupal.org/project/drupal/issues/2011038
*/
#[Action(
id: '{{ plugin_id }}',
label: new TranslatableMarkup('{{ plugin_label }}'),
category: new TranslatableMarkup('{{ category }}'),
type: '{{ entity_type }}',
)]
final class {{ class }} extends {{ configurable ? 'ConfigurableActionBase' : 'ActionBase' }} {% if services %}implements ContainerFactoryPluginInterface {% endif %}{

{% if services %}
Expand Down
Expand Up @@ -5,21 +5,16 @@
namespace Drupal\example\Plugin\Action;

use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
* Provides a Foo action.
*
* @Action(
* id = "example_foo",
* label = @Translation("Foo"),
* type = "node",
* category = @Translation("Custom"),
* )
*
* @DCG
* For updating entity fields consider extending FieldUpdateActionBase.
* @see \Drupal\Core\Field\FieldUpdateActionBase
Expand All @@ -34,6 +29,12 @@
* The whole action API is subject of change.
* @see https://www.drupal.org/project/drupal/issues/2011038
*/
#[Action(
id: 'example_foo',
label: new TranslatableMarkup('Foo'),
category: new TranslatableMarkup('Custom'),
type: 'node',
)]
final class Foo extends ConfigurableActionBase {

/**
Expand Down
Expand Up @@ -6,22 +6,17 @@

use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Flood\FloodInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a Foo action.
*
* @Action(
* id = "example_foo",
* label = @Translation("Foo"),
* type = "user",
* category = @Translation("My Actions"),
* )
*
* @DCG
* For updating entity fields consider extending FieldUpdateActionBase.
* @see \Drupal\Core\Field\FieldUpdateActionBase
Expand All @@ -36,6 +31,12 @@
* The whole action API is subject of change.
* @see https://www.drupal.org/project/drupal/issues/2011038
*/
#[Action(
id: 'example_foo',
label: new TranslatableMarkup('Foo'),
category: new TranslatableMarkup('My Actions'),
type: 'user',
)]
final class Foo extends ActionBase implements ContainerFactoryPluginInterface {

/**
Expand Down

0 comments on commit 0acf136

Please sign in to comment.