You are here

public function EntityTypeInfo::formAlter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/EntityTypeInfo.php \Drupal\content_moderation\EntityTypeInfo::formAlter()

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

core/modules/content_moderation/src/EntityTypeInfo.php, line 334

Class

EntityTypeInfo
Manipulates entity type information.

Namespace

Drupal\content_moderation

Code

public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state
    ->getFormObject();
  if ($form_object instanceof BundleEntityFormBase) {
    $config_entity = $form_object
      ->getEntity();
    $bundle_of = $config_entity
      ->getEntityType()
      ->getBundleOf();
    if ($bundle_of && ($bundle_of_entity_type = $this->entityTypeManager
      ->getDefinition($bundle_of)) && $this->moderationInfo
      ->shouldModerateEntitiesOfBundle($bundle_of_entity_type, $config_entity
      ->id())) {
      $this->entityTypeManager
        ->getHandler($bundle_of, '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();
    $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'][] = [
      EntityTypeInfo::class,
      'bundleFormRedirect',
    ];

    // Move the 'moderation_state' field widget to the footer region, if
    // available.
    if (isset($form['footer']) && in_array($form_object
      ->getOperation(), [
      'edit',
      'default',
    ], TRUE)) {
      $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();
    }
  }
}