function ModerationStateWidget::formElement

Same name in other branches
  1. 9 core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php \Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::formElement()
  2. 8.9.x core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php \Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::formElement()
  3. 10 core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php \Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::formElement()

Overrides OptionsSelectWidget::formElement

File

core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php, line 117

Class

ModerationStateWidget
Plugin implementation of the 'moderation_state_default' widget.

Namespace

Drupal\content_moderation\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $original_entity = $items->getEntity();
    $default = $this->moderationInformation
        ->getOriginalState($entity);
    // If the entity already exists, grab the most recent revision and load it.
    // The moderation state of the saved revision will be used to display the
    // current state as well determine the appropriate transitions.
    if (!$entity->isNew()) {
        
        /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
        $storage = $this->entityTypeManager
            ->getStorage($entity->getEntityTypeId());
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */
        $original_entity = $storage->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());
    }
    
    /** @var \Drupal\workflows\Transition[] $transitions */
    $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.