Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / action / src / Plugin / Action / MessageAction.php
1 <?php
2
3 namespace Drupal\action\Plugin\Action;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Action\ConfigurableActionBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\Core\Render\RendererInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\Core\Utility\Token;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Sends a message to the current user's screen.
16  *
17  * @Action(
18  *   id = "action_message_action",
19  *   label = @Translation("Display a message to the user"),
20  *   type = "system"
21  * )
22  */
23 class MessageAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
24
25   /**
26    * The token service.
27    *
28    * @var \Drupal\Core\Utility\Token
29    */
30   protected $token;
31
32   /**
33    * The renderer.
34    *
35    * @var \Drupal\Core\Render\RendererInterface
36    */
37   protected $renderer;
38
39   /**
40    * Constructs a MessageAction object.
41    *
42    * @param array $configuration
43    *   A configuration array containing information about the plugin instance.
44    * @param string $plugin_id
45    *   The plugin_id for the plugin instance.
46    * @param mixed $plugin_definition
47    *   The plugin implementation definition.
48    * @param \Drupal\Core\Utility\Token $token
49    *   The token service.
50    * @param \Drupal\Core\Render\RendererInterface $renderer
51    *   The renderer.
52    */
53   public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, RendererInterface $renderer) {
54     parent::__construct($configuration, $plugin_id, $plugin_definition);
55
56     $this->token = $token;
57     $this->renderer = $renderer;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64     return new static($configuration, $plugin_id, $plugin_definition, $container->get('token'), $container->get('renderer'));
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function execute($entity = NULL) {
71     if (empty($this->configuration['node'])) {
72       $this->configuration['node'] = $entity;
73     }
74     $message = $this->token->replace($this->configuration['message'], $this->configuration);
75     $build = [
76       '#markup' => $message,
77     ];
78
79     // @todo Fix in https://www.drupal.org/node/2577827
80     drupal_set_message($this->renderer->renderPlain($build));
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function defaultConfiguration() {
87     return [
88       'message' => '',
89     ];
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
96     $form['message'] = [
97       '#type' => 'textarea',
98       '#title' => t('Message'),
99       '#default_value' => $this->configuration['message'],
100       '#required' => TRUE,
101       '#rows' => '8',
102       '#description' => t('The message to be displayed to the current user. You may include placeholders like [node:title], [user:account-name], [user:display-name] and [comment:body] to represent data that will be different each time message is sent. Not all placeholders will be available in all contexts.'),
103     ];
104     return $form;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
111     $this->configuration['message'] = $form_state->getValue('message');
112     unset($this->configuration['node']);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
119     $result = AccessResult::allowed();
120     return $return_as_object ? $result : $result->isAllowed();
121   }
122
123 }