Contains \Drupal\action\Plugin\Action\MessageAction.
<?php
/**
* @file
* Contains \Drupal\action\Plugin\Action\MessageAction.
*/
namespace Drupal\action\Plugin\Action;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Annotation\Action;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Utility\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Sends a message to the current user's screen.
*
* @Action(
* id = "action_message_action",
* label = @Translation("Display a message to the user"),
* type = "system"
* )
*/
class MessageAction extends ConfigurableActionBase {
/**
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Constructs a MessageAction object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'));
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if (empty($this->configuration['node'])) {
$this->configuration['node'] = $entity;
}
$message = $this->token
->replace(Xss::filterAdmin($this->configuration['message']), $this->configuration);
drupal_set_message($message);
}
/**
* {@inheritdoc}
*/
protected function getDefaultConfiguration() {
return array(
'message' => '',
);
}
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => $this->configuration['message'],
'#required' => TRUE,
'#rows' => '8',
'#description' => t('The message to be displayed to the current user. You may include placeholders like [node:title], [user: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.'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submit(array &$form, array &$form_state) {
$this->configuration['message'] = $form_state['values']['message'];
unset($this->configuration['node']);
}
}
Name | Description |
---|---|
MessageAction | Sends a message to the current user's screen. |