You are here

protected function ContentEntitySourcePluginUi::buildContentModerationElement in Translation Management Tool 8

Build a content moderation elemenet for the translation.

Parameters

\Drupal\tmgmt\JobItemInterface $item: The job item.

\Drupal\Core\Entity\ContentEntityInterface $entity: The source moderated entity.

Return value

array A content moderation form element.

1 call to ContentEntitySourcePluginUi::buildContentModerationElement()
ContentEntitySourcePluginUi::reviewForm in sources/content/src/ContentEntitySourcePluginUi.php
Form callback for the job item review form.

File

sources/content/src/ContentEntitySourcePluginUi.php, line 658

Class

ContentEntitySourcePluginUi
Content entity source plugin UI.

Namespace

Drupal\tmgmt_content

Code

protected function buildContentModerationElement(JobItemInterface $item, ContentEntityInterface $entity) {
  $element = [];

  /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  $moderation_info = \Drupal::service('content_moderation.moderation_information');
  $workflow = $moderation_info
    ->getWorkflowForEntity($entity);
  $moderation_validator = \Drupal::service('content_moderation.state_transition_validation');

  // Extract the current moderation state stored within the special key.
  $moderation_state = $item
    ->getData([
    '#moderation_state',
  ], 0);
  $current_state = isset($moderation_state[0]) ? $moderation_state[0] : $entity
    ->get('moderation_state')->value;
  $default = $workflow
    ->getTypePlugin()
    ->getState($current_state);

  // Get a list of valid transitions.

  /** @var \Drupal\workflows\Transition[] $transitions */
  $transitions = $moderation_validator
    ->getValidTransitions($entity, \Drupal::currentUser());
  $transition_labels = [];
  $default_value = NULL;
  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();
    }
  }

  // Get the state of the new config, if not set fallback to the current one.
  if ($default_moderation_state = \Drupal::config('tmgmt_content.settings')
    ->get('default_moderation_states.' . $workflow
    ->id())) {
    $default_value = $default_moderation_state;
  }

  // See \Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::formElement()
  $element += [
    '#type' => 'container',
    '#tree' => TRUE,
    'current' => [
      '#type' => 'item',
      '#title' => $this
        ->t('Current source state'),
      '#markup' => $default
        ->label(),
      '#wrapper_attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
    ],
    'new_state' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Translation state'),
      '#options' => $transition_labels,
      '#default_value' => $default_value,
      '#access' => !empty($transition_labels),
      '#wrapper_attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
    ],
  ];
  $element['#theme'] = [
    'entity_moderation_form',
  ];
  $element['#attached']['library'][] = 'content_moderation/content_moderation';
  return $element;
}