You are here

protected function ContentEntityForm::addRevisionableFormFields in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/ContentEntityForm.php \Drupal\Core\Entity\ContentEntityForm::addRevisionableFormFields()

Add revision form fields if the entity enabled the UI.

Parameters

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

1 call to ContentEntityForm::addRevisionableFormFields()
ContentEntityForm::form in core/lib/Drupal/Core/Entity/ContentEntityForm.php
Gets the actual form array to be built.

File

core/lib/Drupal/Core/Entity/ContentEntityForm.php, line 410

Class

ContentEntityForm
Entity form variant for content entity types.

Namespace

Drupal\Core\Entity

Code

protected function addRevisionableFormFields(array &$form) {

  /** @var ContentEntityTypeInterface $entity_type */
  $entity_type = $this->entity
    ->getEntityType();
  $new_revision_default = $this
    ->getNewRevisionDefault();

  // Add a log field if the "Create new revision" option is checked, or if the
  // current user has the ability to check that option.
  $form['revision_information'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Revision information'),
    // Open by default when "Create new revision" is checked.
    '#open' => $new_revision_default,
    '#group' => 'advanced',
    '#weight' => 20,
    '#access' => $new_revision_default || $this->entity
      ->get($entity_type
      ->getKey('revision'))
      ->access('update'),
    '#optional' => TRUE,
    '#attributes' => [
      'class' => [
        'entity-content-form-revision-information',
      ],
    ],
    '#attached' => [
      'library' => [
        'core/drupal.entity-form',
      ],
    ],
  ];
  $form['revision'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Create new revision'),
    '#default_value' => $new_revision_default,
    '#access' => !$this->entity
      ->isNew() && $this->entity
      ->get($entity_type
      ->getKey('revision'))
      ->access('update'),
    '#group' => 'revision_information',
  ];

  // Get log message field's key from definition.
  $log_message_field = $entity_type
    ->getRevisionMetadataKey('revision_log_message');
  if ($log_message_field && isset($form[$log_message_field])) {
    $form[$log_message_field] += [
      '#group' => 'revision_information',
      '#states' => [
        'visible' => [
          ':input[name="revision"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
}