You are here

public function RevisionableContentEntityForm::form in Entity API 8.0

Same name and namespace in other branches
  1. 8 src/Form/RevisionableContentEntityForm.php \Drupal\entity\Form\RevisionableContentEntityForm::form()

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/RevisionableContentEntityForm.php, line 60
Contains \Drupal\entity\Form\RevisionableContentEntityForm.

Class

RevisionableContentEntityForm
Extends the base entity form with revision support in the UI.

Namespace

Drupal\entity\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $entity_type = $this->entity
    ->getEntityType();
  $bundle_entity = $this
    ->getBundleEntity();
  $account = $this
    ->currentUser();
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('Edit %bundle_label @label', [
      '%bundle_label' => $bundle_entity ? $bundle_entity
        ->label() : '',
      '@label' => $this->entity
        ->label(),
    ]);
  }
  $form['advanced'] = [
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  ];

  // Add a log field if the "Create new revision" option is checked, or if the
  // current user has the ability to check that option.
  // @todo Could we autogenerate this form by using some widget on the
  //   revision info field.
  $form['revision_information'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Revision information'),
    // Open by default when "Create new revision" is checked.
    '#open' => $this->entity
      ->isNewRevision(),
    '#group' => 'advanced',
    '#weight' => 20,
    '#access' => $this->entity
      ->isNewRevision() || $account
      ->hasPermission($entity_type
      ->get('admin_permission')),
  ];
  $form['revision_information']['revision'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Create new revision'),
    '#default_value' => $this->entity
      ->isNewRevision(),
    '#access' => $account
      ->hasPermission($entity_type
      ->get('admin_permission')),
  ];

  // Check the revision log checkbox when the log textarea is filled in.
  // This must not happen if "Create new revision" is enabled by default,
  // since the state would auto-disable the checkbox otherwise.
  if (!$this->entity
    ->isNewRevision()) {
    $form['revision_information']['revision']['#states'] = [
      'checked' => [
        'textarea[name="revision_log"]' => [
          'empty' => FALSE,
        ],
      ],
    ];
  }
  $form['revision_information']['revision_log'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Revision log message'),
    '#rows' => 4,
    '#default_value' => $this->entity
      ->getRevisionLogMessage(),
    '#description' => $this
      ->t('Briefly describe the changes you have made.'),
  ];
  return parent::form($form, $form_state);
}