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