Pull merge.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / block.twig
1 {% import 'lib/di.twig' as di %}
2 <?php
3
4 namespace Drupal\{{ machine_name }}\Plugin\Block;
5
6 {% sort %}
7   {% if access %}
8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Session\AccountInterface;
10   {% endif %}
11 use Drupal\Core\Block\BlockBase;
12   {% if configurable %}
13 use Drupal\Core\Form\FormStateInterface;
14   {% endif %}
15   {% if services %}
16 {{ di.use(services) }}
17 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
19   {% endif %}
20 {% endsort %}
21
22 /**
23  * Provides {{ plugin_label|article|lower }} block.
24  *
25  * @Block(
26  *   id = "{{ plugin_id }}",
27  *   admin_label = @Translation("{{ plugin_label }}"),
28  *   category = @Translation("{{ category }}")
29  * )
30  */
31 class {{ class }} extends BlockBase {% if services %}implements ContainerFactoryPluginInterface {% endif %}{
32
33 {% if services %}
34 {{ di.properties(services) }}
35
36   /**
37    * Constructs a new {{ class }} instance.
38    *
39    * @param array $configuration
40    *   The plugin configuration, i.e. an array with configuration values keyed
41    *   by configuration option name. The special key 'context' may be used to
42    *   initialize the defined contexts by setting it to an array of context
43    *   values keyed by context names.
44    * @param string $plugin_id
45    *   The plugin_id for the plugin instance.
46    * @param mixed $plugin_definition
47    *   The plugin implementation definition.
48 {{ di.annotation(services) }}
49    */
50   public function __construct(array $configuration, $plugin_id, $plugin_definition, {{ di.signature(services) }}) {
51     parent::__construct($configuration, $plugin_id, $plugin_definition);
52 {{ di.assignment(services) }}
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59     return new static(
60       $configuration,
61       $plugin_id,
62       $plugin_definition,
63 {{ di.container(services) }}
64     );
65   }
66
67 {% endif %}
68 {% if configurable %}
69   /**
70    * {@inheritdoc}
71    */
72   public function defaultConfiguration() {
73     return [
74       'foo' => $this->t('Hello world!'),
75     ];
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function blockForm($form, FormStateInterface $form_state) {
82     $form['foo'] = [
83       '#type' => 'textarea',
84       '#title' => $this->t('Foo'),
85       '#default_value' => $this->configuration['foo'],
86     ];
87     return $form;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function blockSubmit($form, FormStateInterface $form_state) {
94     $this->configuration['foo'] = $form_state->getValue('foo');
95   }
96
97 {% endif %}
98 {% if access %}
99   /**
100    * {@inheritdoc}
101    */
102   protected function blockAccess(AccountInterface $account) {
103     // @DCG Evaluate the access condition here.
104     $condition = TRUE;
105     return AccessResult::allowedIf($condition);
106   }
107
108 {% endif %}
109   /**
110    * {@inheritdoc}
111    */
112   public function build() {
113     $build['content'] = [
114       '#markup' => $this->t('It works!'),
115     ];
116     return $build;
117   }
118
119 }