Skip to content

Commit 0f1ed5e

Browse files
authored
Use PHP attribute for block plugin. (#145)
1 parent 504d605 commit 0f1ed5e

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

templates/Plugin/_block/block.twig

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ namespace Drupal\{{ machine_name }}\Plugin\Block;
1010
use Drupal\Core\Access\AccessResult;
1111
use Drupal\Core\Session\AccountInterface;
1212
{% endif %}
13+
use Drupal\Core\Block\Attribute\Block;
1314
use Drupal\Core\Block\BlockBase;
15+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1416
{% if configurable %}
1517
use Drupal\Core\Form\FormStateInterface;
1618
{% endif %}
@@ -23,13 +25,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
2325
2426
/**
2527
* Provides {{ plugin_label|article|lower }} block.
26-
*
27-
* @Block(
28-
* id = "{{ plugin_id }}",
29-
* admin_label = @Translation("{{ plugin_label }}"),
30-
* category = @Translation("{{ category }}"),
31-
* )
3228
*/
29+
#[Block(
30+
id: '{{ plugin_id }}',
31+
admin_label: new TranslatableMarkup('{{ plugin_label }}'),
32+
category: new TranslatableMarkup('{{ category }}'),
33+
)]
3334
final class {{ class }} extends BlockBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
3435
3536
{% if services %}

tests/functional/Generator/Plugin/_block/_n_deps/_n_config/src/Plugin/Block/ExampleBlock.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
namespace Drupal\foo\Plugin\Block;
66

7+
use Drupal\Core\Block\Attribute\Block;
78
use Drupal\Core\Block\BlockBase;
9+
use Drupal\Core\StringTranslation\TranslatableMarkup;
810

911
/**
1012
* Provides an example block.
11-
*
12-
* @Block(
13-
* id = "foo_example",
14-
* admin_label = @Translation("Example"),
15-
* category = @Translation("Custom"),
16-
* )
1713
*/
14+
#[Block(
15+
id: 'foo_example',
16+
admin_label: new TranslatableMarkup('Example'),
17+
category: new TranslatableMarkup('Custom'),
18+
)]
1819
final class ExampleBlock extends BlockBase {
1920

2021
/**

tests/functional/Generator/Plugin/_block/_w_deps/_w_config/src/Plugin/Block/ExampleBlock.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
namespace Drupal\foo\Plugin\Block;
66

77
use Drupal\Core\Access\AccessResult;
8+
use Drupal\Core\Block\Attribute\Block;
89
use Drupal\Core\Block\BlockBase;
910
use Drupal\Core\CronInterface;
1011
use Drupal\Core\Form\FormStateInterface;
1112
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
1213
use Drupal\Core\Session\AccountInterface;
14+
use Drupal\Core\StringTranslation\TranslatableMarkup;
1315
use Symfony\Component\DependencyInjection\ContainerInterface;
1416

1517
/**
1618
* Provides an example block.
17-
*
18-
* @Block(
19-
* id = "foo_example",
20-
* admin_label = @Translation("Example"),
21-
* category = @Translation("Custom"),
22-
* )
2319
*/
20+
#[Block(
21+
id: 'foo_example',
22+
admin_label: new TranslatableMarkup('Example'),
23+
category: new TranslatableMarkup('Custom'),
24+
)]
2425
final class ExampleBlock extends BlockBase implements ContainerFactoryPluginInterface {
2526

2627
/**

0 commit comments

Comments
 (0)