5d4c2b7bf6ab312e7283a52a0e09dccadc5f06f0
[yaffs-website] / web / core / modules / content_moderation / src / Plugin / Field / FieldFormatter / ContentModerationStateFormatter.php
1 <?php
2
3 namespace Drupal\content_moderation\Plugin\Field\FieldFormatter;
4
5 use Drupal\content_moderation\ModerationInformationInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FormatterBase;
8 use Drupal\Core\Field\FieldItemListInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Plugin implementation of the 'content_moderation_state' formatter.
14  *
15  * @FieldFormatter(
16  *   id = "content_moderation_state",
17  *   label = @Translation("Content moderation state"),
18  *   field_types = {
19  *     "string",
20  *   }
21  * )
22  */
23 class ContentModerationStateFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
24
25   /**
26    * The moderation information service.
27    *
28    * @var \Drupal\content_moderation\ModerationInformationInterface
29    */
30   protected $moderationInformation;
31
32   /**
33    * Create an instance of ContentModerationStateFormatter.
34    */
35   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ModerationInformationInterface $moderation_information) {
36     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
37     $this->moderationInformation = $moderation_information;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
44     return new static(
45       $plugin_id,
46       $plugin_definition,
47       $configuration['field_definition'],
48       $configuration['settings'],
49       $configuration['label'],
50       $configuration['view_mode'],
51       $configuration['third_party_settings'],
52       $container->get('content_moderation.moderation_information')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function viewElements(FieldItemListInterface $items, $langcode) {
60     $elements = [];
61     $workflow = $this->moderationInformation->getWorkflowForEntity($items->getEntity());
62     foreach ($items as $delta => $item) {
63       $elements[$delta] = [
64         '#markup' => $workflow->getTypePlugin()->getState($item->value)->label(),
65       ];
66     }
67     return $elements;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public static function isApplicable(FieldDefinitionInterface $field_definition) {
74     return $field_definition->getName() === 'moderation_state' && $field_definition->getTargetEntityTypeId() !== 'content_moderation_state';
75   }
76
77 }