3 namespace Drupal\content_moderation\Plugin\Field\FieldWidget;
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;
18 * Plugin implementation of the 'moderation_state_default' widget.
21 * id = "moderation_state_default",
22 * label = @Translation("Moderation state"),
28 class ModerationStateWidget extends OptionsSelectWidget implements ContainerFactoryPluginInterface {
31 * Current user service.
33 * @var \Drupal\Core\Session\AccountInterface
35 protected $currentUser;
38 * Moderation information service.
40 * @var \Drupal\content_moderation\ModerationInformation
42 protected $moderationInformation;
45 * The entity type manager.
47 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
49 protected $entityTypeManager;
52 * Moderation state transition validation service.
54 * @var \Drupal\content_moderation\StateTransitionValidationInterface
59 * Constructs a new ModerationStateWidget object.
61 * @param string $plugin_id
63 * @param mixed $plugin_definition
65 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
67 * @param array $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.
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;
91 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
108 public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
109 $entity = $items->getEntity();
110 if (!$this->moderationInformation->isModeratedEntity($entity)) {
113 return parent::form($items, $form, $form_state, $get_delta);
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();
123 $workflow = $this->moderationInformation->getWorkflowForEntity($entity);
124 $default = $items->get($delta)->value ? $workflow->getTypePlugin()->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($entity);
126 /** @var \Drupal\workflows\Transition[] $transitions */
127 $transitions = $this->validator->getValidTransitions($entity, $this->currentUser);
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();
137 '#type' => 'container',
140 '#title' => $this->t('Current state'),
141 '#markup' => $default->label(),
142 '#access' => !$entity->isNew(),
143 '#wrapper_attributes' => [
144 'class' => ['container-inline'],
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'],
159 $element['#element_validate'][] = [get_class($this), 'validateElement'];
167 public static function validateElement(array $element, FormStateInterface $form_state) {
168 $form_state->setValueForElement($element, [$element['state']['#key_column'] => $element['state']['#value']]);
174 public static function isApplicable(FieldDefinitionInterface $field_definition) {
175 return is_a($field_definition->getClass(), ModerationStateFieldItemList::class, TRUE);