View source
<?php
namespace Drupal\content_moderation\Plugin\Field\FieldWidget;
use Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\content_moderation\ModerationInformation;
use Drupal\content_moderation\StateTransitionValidationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModerationStateWidget extends OptionsSelectWidget {
protected $currentUser;
protected $moderationInformation;
protected $entityTypeManager;
protected $validator;
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) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->moderationInformation = $moderation_information;
$this->validator = $validator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('content_moderation.moderation_information'), $container
->get('content_moderation.state_transition_validation'));
}
public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
$entity = $items
->getEntity();
if (!$this->moderationInformation
->isModeratedEntity($entity)) {
return [];
}
return parent::form($items, $form, $form_state, $get_delta);
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$entity = $original_entity = $items
->getEntity();
$default = $this->moderationInformation
->getOriginalState($entity);
if (!$entity
->isNew()) {
$original_entity = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadRevision($entity
->getLoadedRevisionId());
if (!$entity
->isDefaultTranslation() && $original_entity
->hasTranslation($entity
->language()
->getId())) {
$original_entity = $original_entity
->getTranslation($entity
->language()
->getId());
}
}
else {
$original_entity
->set('moderation_state', $default
->id());
}
$transitions = $this->validator
->getValidTransitions($original_entity, $this->currentUser);
$transition_labels = [];
$default_value = $items->value;
foreach ($transitions as $transition) {
$transition_to_state = $transition
->to();
$transition_labels[$transition_to_state
->id()] = $transition_to_state
->label();
if ($default
->id() === $transition_to_state
->id()) {
$default_value = $default
->id();
}
}
$element += [
'#type' => 'container',
'current' => [
'#type' => 'item',
'#title' => $this
->t('Current state'),
'#markup' => $default
->label(),
'#access' => !$entity
->isNew(),
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
],
],
'state' => [
'#type' => 'select',
'#title' => $entity
->isNew() ? $this
->t('Save as') : $this
->t('Change to'),
'#key_column' => $this->column,
'#options' => $transition_labels,
'#default_value' => $default_value,
'#access' => !empty($transition_labels),
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
],
],
];
$element['#element_validate'][] = [
static::class,
'validateElement',
];
return $element;
}
public static function validateElement(array $element, FormStateInterface $form_state) {
$form_state
->setValueForElement($element, [
$element['state']['#key_column'] => $element['state']['#value'],
]);
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return is_a($field_definition
->getClass(), ModerationStateFieldItemList::class, TRUE);
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
if ($workflow = $this->moderationInformation
->getWorkflowForEntityTypeAndBundle($this->fieldDefinition
->getTargetEntityTypeId(), $this->fieldDefinition
->getTargetBundle())) {
$dependencies[$workflow
->getConfigDependencyKey()][] = $workflow
->getConfigDependencyName();
}
return $dependencies;
}
}