View source
<?php
namespace Drupal\lightning_scheduler\Plugin\Field\FieldWidget;
use Drupal\Component\Serialization\Json;
use Drupal\content_moderation\ModerationInformation;
use Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget as BaseModerationStateWidget;
use Drupal\content_moderation\StateTransitionValidationInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\lightning_scheduler\TransitionManager;
use Drupal\lightning_scheduler\TransitionSet;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModerationStateWidget extends BaseModerationStateWidget {
protected $transitionManager;
protected $moduleHandler;
protected $entity;
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, TransitionManager $transition_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $current_user, $entity_type_manager, $moderation_information, $validator);
$this->transitionManager = $transition_manager;
$this->moduleHandler = $module_handler;
}
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'), $container
->get('lightning_scheduler.transition_manager'), $container
->get('module_handler'));
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$entity = $items
->getEntity();
$has_fields = $entity
->hasField('scheduled_transition_date') && $entity
->hasField('scheduled_transition_state');
if (!$has_fields) {
return $element;
}
$states = $this->transitionManager
->getStates($entity);
$latest_revision = $this->moderationInformation
->getLatestRevision($entity
->getEntityTypeId(), $entity
->id()) ?: $entity;
$transition_set = new TransitionSet($latest_revision
->get('scheduled_transition_date'), $latest_revision
->get('scheduled_transition_state'));
$element['scheduled_transitions'] = [
'#type' => 'html_tag',
'#tag' => 'TransitionSet',
'#attributes' => [
'states' => Json::encode($states),
],
'#attached' => [
'library' => [
'lightning_scheduler/widget',
],
],
'data' => [
'#type' => 'hidden',
'#element_validate' => [
[
get_class($this->transitionManager),
'validate',
],
[
$this,
'storeValue',
],
],
'#default_value' => $transition_set
->toJSON(),
'#process' => [
[
$this,
'processComponentInput',
],
],
],
];
if ($this->moduleHandler
->moduleExists('lightning_dev')) {
$element['scheduled_transitions']['#attributes']['step'] = 1;
}
$this->entity = $entity;
return $element;
}
public function processComponentInput(array $element, FormStateInterface $form_state) {
$key = $element['#parents'];
if ($form_state
->hasValue($key)) {
$element['#default_value'] = $form_state
->getValue($key);
}
return $element;
}
public function storeValue(array $element, FormStateInterface $form_state) {
if ($form_state
->getErrors()) {
return;
}
$decoded = Json::decode($element['#value']);
assert(is_array($decoded));
$transition_storage = $form_state
->getValue('transition_storage') ?: [];
$uuid = $this->entity
->uuid();
$transition_storage[$uuid] = $decoded;
$form_state
->setValue('transition_storage', $transition_storage);
}
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
parent::extractFormValues($items, $form, $form_state);
$transitions = $form_state
->getValue('transition_storage');
$entity = $items
->getEntity();
$uuid = $entity
->uuid();
if (!isset($transitions[$uuid])) {
return;
}
$states = array_map(function (array $transition) {
assert(!empty($transition['state']) && is_string($transition['state']));
return [
'value' => $transition['state'],
];
}, $transitions[$uuid]);
$dates = array_map(function (array $transition) {
$date_time = new DrupalDateTime($transition['when'], 'UTC');
return [
'value' => $date_time
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
];
}, $transitions[$uuid]);
assert(count($states) === count($dates));
$entity
->set('scheduled_transition_state', $states)
->set('scheduled_transition_date', $dates);
}
}