View source
<?php
namespace Drupal\content_moderation_notifications;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Tokens implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $notificationInformation;
public function __construct(NotificationInformationInterface $notification_information) {
$this->notificationInformation = $notification_information;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('content_moderation_notifications.notification_information'));
}
public static function info() {
$type = [
'name' => t('Content moderation states'),
'description' => t('Content moderation state transition tokens.'),
'needs-data' => 'entity',
];
$tokens['workflow'] = [
'name' => t('Workflow'),
'description' => t('The name of the corresponding workflow.'),
];
$tokens['from-state'] = [
'name' => t('Old moderation state'),
'description' => t('The previous state of the moderated content.'),
];
$tokens['to-state'] = [
'name' => t('Current moderation state'),
'description' => t('The new/current state of the moderated content.'),
];
return [
'types' => [
'content_moderation_notifications' => $type,
],
'tokens' => [
'content_moderation_notifications' => $tokens,
],
];
}
public function getTokens($type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type === 'content_moderation_notifications' && isset($data['entity']) && $data['entity'] instanceof ContentEntityInterface) {
$entity = $data['entity'];
if ($this->notificationInformation
->isModeratedEntity($entity)) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'workflow':
$workflow = $this->notificationInformation
->getWorkflow($entity)
->label();
$replacements[$original] = $workflow;
$bubbleable_metadata
->addCacheableDependency($workflow);
break;
case 'from-state':
if ($transition = $this->notificationInformation
->getTransition($entity)) {
$replacements[$original] = $this->notificationInformation
->getPreviousState($entity)
->label();
$bubbleable_metadata
->addCacheableDependency($transition);
}
break;
case 'to-state':
if ($transition = $this->notificationInformation
->getTransition($entity)) {
$replacements[$original] = $transition
->to()
->label();
$bubbleable_metadata
->addCacheableDependency($transition);
}
break;
}
}
}
}
return $replacements;
}
}