function EntityTypeInfo::formAlter

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/EntityTypeInfo.php \Drupal\content_moderation\EntityTypeInfo::formAlter()
  2. 10 core/modules/content_moderation/src/EntityTypeInfo.php \Drupal\content_moderation\EntityTypeInfo::formAlter()
  3. 11.x 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 321

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();
        }
    }
}

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