Skip to content

Commit

Permalink
Use PHP attribute for block plugin. (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
chesn0k authored May 20, 2024
1 parent 504d605 commit 0f1ed5e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
13 changes: 7 additions & 6 deletions templates/Plugin/_block/block.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace Drupal\{{ machine_name }}\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
{% endif %}
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
Expand All @@ -23,13 +25,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides {{ plugin_label|article|lower }} block.
*
* @Block(
* id = "{{ plugin_id }}",
* admin_label = @Translation("{{ plugin_label }}"),
* category = @Translation("{{ category }}"),
* )
*/
#[Block(
id: '{{ plugin_id }}',
admin_label: new TranslatableMarkup('{{ plugin_label }}'),
category: new TranslatableMarkup('{{ category }}'),
)]
final class {{ class }} extends BlockBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{

{% if services %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

namespace Drupal\foo\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
* Provides an example block.
*
* @Block(
* id = "foo_example",
* admin_label = @Translation("Example"),
* category = @Translation("Custom"),
* )
*/
#[Block(
id: 'foo_example',
admin_label: new TranslatableMarkup('Example'),
category: new TranslatableMarkup('Custom'),
)]
final class ExampleBlock extends BlockBase {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
namespace Drupal\foo\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\CronInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides an example block.
*
* @Block(
* id = "foo_example",
* admin_label = @Translation("Example"),
* category = @Translation("Custom"),
* )
*/
#[Block(
id: 'foo_example',
admin_label: new TranslatableMarkup('Example'),
category: new TranslatableMarkup('Custom'),
)]
final class ExampleBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
Expand Down

0 comments on commit 0f1ed5e

Please sign in to comment.