ModerationStateWidget.php in Thunder 8.5
File
modules/thunder_workflow/src/Plugin/Field/FieldWidget/ModerationStateWidget.php
View source
<?php
namespace Drupal\thunder_workflow\Plugin\Field\FieldWidget;
use Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget as CoreModerationStateWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class ModerationStateWidget extends CoreModerationStateWidget {
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' => 'select',
'#key_column' => $this->column,
'#options' => $transition_labels,
'#default_value' => $default_value,
'#access' => !empty($transition_labels),
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
],
];
$element['#element_validate'][] = [
get_class($this),
'validateElement',
];
return $element;
}
public static function validateElement(array $element, FormStateInterface $form_state) {
$form_state
->setValueForElement($element, [
$element['#key_column'] => $element['#value'],
]);
}
}