diff vendor/chi-teck/drupal-code-generator/templates/d8/plugin/action.twig @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children 12f9dff5fda9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vendor/chi-teck/drupal-code-generator/templates/d8/plugin/action.twig	Thu Jul 05 14:24:15 2018 +0000
@@ -0,0 +1,80 @@
+<?php
+
+namespace Drupal\{{ machine_name }}\Plugin\Action;
+
+{% if configurable %}
+use Drupal\Core\Action\ConfigurableActionBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Session\AccountInterface;
+{% else %}
+use Drupal\Core\Action\ActionBase;
+use Drupal\Core\Session\AccountInterface;
+{% endif %}
+
+/**
+ * Provides a {{ plugin_label|article }} action.
+ *
+ * @Action(
+ *   id = "{{ plugin_id }}",
+ *   label = @Translation("{{ plugin_label }}"),
+ *   type = "node",
+ *   category = @Translation("{{ category }}")
+ * )
+ *
+ * @DCG
+ * For simple updating entity fields consider extending FieldUpdateActionBase.
+ */
+class {{ class }} extends {{ configurable ? 'ConfigurableActionBase' : 'ActionBase' }} {
+
+{% if configurable %}
+  /**
+   * {@inheritdoc}
+   */
+  public function defaultConfiguration() {
+    return ['title' => ''];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+    $form['title'] = [
+      '#title' => t('New title'),
+      '#type' => 'textfield',
+      '#required' => TRUE,
+      '#default_value' => $this->configuration['title'],
+    ];
+    return $form;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
+    $this->configuration['title'] = $form_state->getValue('title');
+  }
+
+{% endif %}
+  /**
+   * {@inheritdoc}
+   */
+  public function access($node, AccountInterface $account = NULL, $return_as_object = FALSE) {
+    /** @var \Drupal\node\NodeInterface $node */
+    $access = $node->access('update', $account, TRUE)
+      ->andIf($node->title->access('edit', $account, TRUE));
+    return $return_as_object ? $access : $access->isAllowed();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function execute($node = NULL) {
+    /** @var \Drupal\node\NodeInterface $node */
+{% if configurable %}
+    $node->setTitle($this->configuration['title'])->save();
+{% else %}
+    $node->setTitle(t('New title'))->save();
+{% endif %}
+  }
+
+}