Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / action.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Action;
4
5 {% if configurable %}
6 use Drupal\Core\Action\ConfigurableActionBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Session\AccountInterface;
9 {% else %}
10 use Drupal\Core\Action\ActionBase;
11 use Drupal\Core\Session\AccountInterface;
12 {% endif %}
13
14 /**
15  * Provides a {{ plugin_label|article }} action.
16  *
17  * @Action(
18  *   id = "{{ plugin_id }}",
19  *   label = @Translation("{{ plugin_label }}"),
20  *   type = "node",
21  *   category = @Translation("{{ category }}")
22  * )
23  *
24  * @DCG
25  * For simple updating entity fields consider extending FieldUpdateActionBase.
26  */
27 class {{ class }} extends {{ configurable ? 'ConfigurableActionBase' : 'ActionBase' }} {
28
29 {% if configurable %}
30   /**
31    * {@inheritdoc}
32    */
33   public function defaultConfiguration() {
34     return ['title' => ''];
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
41     $form['title'] = [
42       '#title' => t('New title'),
43       '#type' => 'textfield',
44       '#required' => TRUE,
45       '#default_value' => $this->configuration['title'],
46     ];
47     return $form;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
54     $this->configuration['title'] = $form_state->getValue('title');
55   }
56
57 {% endif %}
58   /**
59    * {@inheritdoc}
60    */
61   public function access($node, AccountInterface $account = NULL, $return_as_object = FALSE) {
62     /** @var \Drupal\node\NodeInterface $node */
63     $access = $node->access('update', $account, TRUE)
64       ->andIf($node->title->access('edit', $account, TRUE));
65     return $return_as_object ? $access : $access->isAllowed();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function execute($node = NULL) {
72     /** @var \Drupal\node\NodeInterface $node */
73 {% if configurable %}
74     $node->setTitle($this->configuration['title'])->save();
75 {% else %}
76     $node->setTitle(t('New title'))->save();
77 {% endif %}
78   }
79
80 }