MessageAction.php 3.9 KB

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