You are here

public function ConfigEntityRevisionsEntityTypeInfo::formAlter in Config Entity Revisions 8.2

Alters bundle forms to enforce revision handling.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $form_id: The form id.

See also

hook_form_alter()

File

src/ConfigEntityRevisionsEntityTypeInfo.php, line 130

Class

ConfigEntityRevisionsEntityTypeInfo
Class ConfigEntityRevisionsEntityTypeInfo.

Namespace

Drupal\config_entity_revisions

Code

public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state
    ->getFormObject();
  if ($form_object instanceof BundleEntityFormBase) {
    $config_entity_type = $form_object
      ->getEntity()
      ->getEntityType();
    $bundle_of = $config_entity_type
      ->getBundleOf();
    if ($bundle_of && ($bundle_of_entity_type = $this->entityTypeManager
      ->getDefinition($bundle_of)) && $this->moderationInfo
      ->canModerateEntitiesOfEntityType($bundle_of_entity_type)) {
      $this->entityTypeManager
        ->getHandler($config_entity_type
        ->getBundleOf(), 'moderation')
        ->enforceRevisionsBundleFormAlter($form, $form_state, $form_id);
    }
  }
  elseif ($this
    ->isModeratedEntityEditForm($form_object)) {

    /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_object
      ->getEntity();
    if ($this->moderationInfo
      ->isModeratedEntity($entity)) {
      $this->entityTypeManager
        ->getHandler($entity
        ->getEntityTypeId(), 'moderation')
        ->enforceRevisionsEntityFormAlter($form, $form_state, $form_id);

      // Submit handler to redirect to the latest version, if available.
      $form['actions']['submit']['#submit'][] = [
        ConfigEntityRevisionsEntityTypeInfo::class,
        'bundleFormRedirect',
      ];

      // Move the 'moderation_state' field widget to the footer region, if
      // available.
      if (isset($form['footer'])) {
        $form['moderation_state']['#group'] = 'footer';
      }

      // If the publishing status exists in the meta region, replace it with
      // the current state instead.
      if (isset($form['meta']['published'])) {
        $form['meta']['published']['#markup'] = $this->moderationInfo
          ->getWorkflowForEntity($entity)
          ->getTypePlugin()
          ->getState($entity->moderation_state->value)
          ->label();
      }
    }
  }
}