967dce880a582a9c41601c143be3a51cc376341b
[yaffs-website] / web / core / modules / content_moderation / src / Plugin / Field / FieldWidget / ModerationStateWidget.php
1 <?php
2
3 namespace Drupal\content_moderation\Plugin\Field\FieldWidget;
4
5 use Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\Core\Field\FieldItemListInterface;
9 use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\content_moderation\ModerationInformation;
14 use Drupal\content_moderation\StateTransitionValidationInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18  * Plugin implementation of the 'moderation_state_default' widget.
19  *
20  * @FieldWidget(
21  *   id = "moderation_state_default",
22  *   label = @Translation("Moderation state"),
23  *   field_types = {
24  *     "string"
25  *   }
26  * )
27  */
28 class ModerationStateWidget extends OptionsSelectWidget implements ContainerFactoryPluginInterface {
29
30   /**
31    * Current user service.
32    *
33    * @var \Drupal\Core\Session\AccountInterface
34    */
35   protected $currentUser;
36
37   /**
38    * Moderation information service.
39    *
40    * @var \Drupal\content_moderation\ModerationInformation
41    */
42   protected $moderationInformation;
43
44   /**
45    * The entity type manager.
46    *
47    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
48    */
49   protected $entityTypeManager;
50
51   /**
52    * Moderation state transition validation service.
53    *
54    * @var \Drupal\content_moderation\StateTransitionValidationInterface
55    */
56   protected $validator;
57
58   /**
59    * Constructs a new ModerationStateWidget object.
60    *
61    * @param string $plugin_id
62    *   Plugin id.
63    * @param mixed $plugin_definition
64    *   Plugin definition.
65    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
66    *   Field definition.
67    * @param array $settings
68    *   Field settings.
69    * @param array $third_party_settings
70    *   Third party settings.
71    * @param \Drupal\Core\Session\AccountInterface $current_user
72    *   Current user service.
73    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
74    *   Entity type manager.
75    * @param \Drupal\content_moderation\ModerationInformation $moderation_information
76    *   Moderation information service.
77    * @param \Drupal\content_moderation\StateTransitionValidationInterface $validator
78    *   Moderation state transition validation service.
79    */
80   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, ModerationInformation $moderation_information, StateTransitionValidationInterface $validator) {
81     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
82     $this->entityTypeManager = $entity_type_manager;
83     $this->currentUser = $current_user;
84     $this->moderationInformation = $moderation_information;
85     $this->validator = $validator;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
92     return new static(
93       $plugin_id,
94       $plugin_definition,
95       $configuration['field_definition'],
96       $configuration['settings'],
97       $configuration['third_party_settings'],
98       $container->get('current_user'),
99       $container->get('entity_type.manager'),
100       $container->get('content_moderation.moderation_information'),
101       $container->get('content_moderation.state_transition_validation')
102     );
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
109     $entity = $items->getEntity();
110     if (!$this->moderationInformation->isModeratedEntity($entity)) {
111       return [];
112     }
113     return parent::form($items, $form, $form_state, $get_delta);
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
120     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
121     $entity = $items->getEntity();
122
123     $workflow = $this->moderationInformation->getWorkflowForEntity($entity);
124     $default = $items->get($delta)->value ? $workflow->getTypePlugin()->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($entity);
125
126     /** @var \Drupal\workflows\Transition[] $transitions */
127     $transitions = $this->validator->getValidTransitions($entity, $this->currentUser);
128
129     $transition_labels = [];
130     $default_value = NULL;
131     foreach ($transitions as $transition) {
132       $transition_to_state = $transition->to();
133       $transition_labels[$transition_to_state->id()] = $transition_to_state->label();
134     }
135
136     $element += [
137       '#type' => 'container',
138       'current' => [
139         '#type' => 'item',
140         '#title' => $this->t('Current state'),
141         '#markup' => $default->label(),
142         '#access' => !$entity->isNew(),
143         '#wrapper_attributes' => [
144           'class' => ['container-inline'],
145         ],
146       ],
147       'state' => [
148         '#type' => 'select',
149         '#title' => $entity->isNew() ? $this->t('Save as') : $this->t('Change to'),
150         '#key_column' => $this->column,
151         '#options' => $transition_labels,
152         '#default_value' => $default_value,
153         '#access' => !empty($transition_labels),
154         '#wrapper_attributes' => [
155           'class' => ['container-inline'],
156         ],
157       ],
158     ];
159     $element['#element_validate'][] = [get_class($this), 'validateElement'];
160
161     return $element;
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public static function validateElement(array $element, FormStateInterface $form_state) {
168     $form_state->setValueForElement($element, [$element['state']['#key_column'] => $element['state']['#value']]);
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public static function isApplicable(FieldDefinitionInterface $field_definition) {
175     return is_a($field_definition->getClass(), ModerationStateFieldItemList::class, TRUE);
176   }
177
178 }