class MessageArgumentsWorker in Message UI 8
Queue worker plugin instance to update the message arguments.
Plugin annotation
@QueueWorker(
id = "message_ui_arguments",
title = @Translation("Message UI arguments"),
cron = {"time" = 60}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
- class \Drupal\message_ui\Plugin\QueueWorker\MessageArgumentsWorker
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
Expanded class hierarchy of MessageArgumentsWorker
1 file declares its use of MessageArgumentsWorker
- message_ui.module in ./
message_ui.module - Contains Drupal\message_ui\message_ui.module.
File
- src/
Plugin/ QueueWorker/ MessageArgumentsWorker.php, line 19
Namespace
Drupal\message_ui\Plugin\QueueWorkerView source
class MessageArgumentsWorker extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
public function processItem($data) {
// Load all of the messages.
$query = \Drupal::entityQuery('message');
$result = $query
->condition('template', $data['template'])
->sort('mid', 'DESC')
->condition('mid', $data['last_mid'], '>=')
->range(0, $data['item_to_process'])
->execute();
if (empty($result)) {
return FALSE;
}
// Update the messages.
$messages = Message::loadMultiple(array_keys($result));
foreach ($messages as $message) {
/* @var Message $message */
self::messageArgumentsUpdate($message, $data['new_arguments']);
$data['last_mid'] = $message
->id();
}
// Create the next queue worker.
$queue = \Drupal::queue('message_ui_arguments');
return $queue
->createItem($data);
}
/**
* Get hard coded arguments.
*
* @param string $template
* The message template.
* @param bool $count
* Determine weather to the count the arguments or return a list of them.
*
* @return int|array
* The number of the arguments.
*/
public static function getArguments($template, $count = FALSE) {
/* @var $message_template MessageTemplate */
if (!($message_template = MessageTemplate::load($template))) {
return [];
}
if (!($output = $message_template
->getText())) {
return [];
}
$text = array_map(function (Markup $markup) {
return (string) $markup;
}, $output);
$text = implode("\n", $text);
preg_match_all('/[@|%|\\!]\\{([a-z0-9:_\\-]+?)\\}/i', $text, $matches);
return $count ? count($matches[0]) : $matches[0];
}
/**
* A helper function for generate a new array of the message's arguments.
*
* @param \Drupal\message\Entity\Message $message
* The message which her arguments need an update.
* @param array $arguments
* The new arguments need to be calculated.
*/
public static function messageArgumentsUpdate(Message $message, array $arguments) {
$message_arguments = [];
foreach ($arguments as $token) {
// Get the hard coded value of the message and him in the message.
$token_name = str_replace([
'@{',
'}',
], [
'[',
']',
], $token);
$token_service = \Drupal::token();
$value = $token_service
->replace($token_name, [
'message' => $message,
]);
$message_arguments[$token] = $value;
}
$message
->setArguments($message_arguments);
$message
->save();
}
/**
* The message batch or queue item callback function.
*
* @param array $mids
* The messages ID for process.
* @param array $arguments
* The new state arguments.
*/
public static function argumentsUpdate(array $mids, array $arguments) {
// Load the messages and update them.
$messages = Message::loadMultiple($mids);
foreach ($messages as $message) {
/* @var Message $message */
MessageArgumentsWorker::messageArgumentsUpdate($message, $arguments);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessageArgumentsWorker:: |
public static | function | The message batch or queue item callback function. | |
MessageArgumentsWorker:: |
public static | function | Get hard coded arguments. | |
MessageArgumentsWorker:: |
public static | function | A helper function for generate a new array of the message's arguments. | |
MessageArgumentsWorker:: |
public | function |
Works on a single queue item. Overrides QueueWorkerInterface:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |