You are here

public static function ConfigEntityRevisionsFormAlterBase::formAlter in Config Entity Revisions 8.2

Common form_alter hook code.

Overrides ConfigEntityRevisionsFormAlterInterface::formAlter

File

src/ConfigEntityRevisionsFormAlterBase.php, line 88

Class

ConfigEntityRevisionsFormAlterBase
Class ConfigEntityRevisionsFormAlterBase.

Namespace

Drupal\config_entity_revisions

Code

public static function formAlter(&$form, FormStateInterface $form_state, $form_id) {

  // Are we interested in this form?
  $revision_routes = static::getRevisionRoutes();
  $routename = \Drupal::routeMatch()
    ->getRouteName();
  if (!in_array($routename, $revision_routes)) {
    return;
  }

  // Get the content entity if it will be needed. This needs to be the most
  // recent version of the entity - if we've saved a new revision, we want
  // the latest revision message, not the old one.
  // @TODO Make testable.
  $match = \Drupal::service('router')
    ->matchRequest(\Drupal::request());

  /* @var $container \Drupal\config_entity_revisions\ConfigEntityRevisionsConfigEntityContainerInterface */
  $container = $match[static::getRouteParameterName()] ?? NULL;
  if (is_null($container)) {
    return;
  }

  /* @var $config_entity ConfigEntityRevisionsConfigEntityInterface */
  $config_entity = $container
    ->revisionedEntity();

  // @TODO Create and specify an interface.

  /* @var $content_entity */
  $content_entity = $config_entity
    ->getContentEntity();
  $isLatestRevision = $content_entity ? $content_entity
    ->isLatestRevision() : FALSE;

  // If we're displaying a non-current revision, add a message and remove the
  // submission buttons.
  if (in_array($routename, static::getEditFormRoutes()) && $content_entity) {

    // @TODO Move so it only displays if we're not saving or if the newly
    // saved version isn't the default.
    if (!$content_entity
      ->isDefaultRevision()) {
      \Drupal::messenger()
        ->addMessage('This is not the currently published revision.', 'warning');
    }

    // If the revision is not the latest revision, disable saving it.
    if (!$isLatestRevision) {
      foreach ($form['actions'] as $key => $value) {
        if (is_array($value) && $key !== 'cancel') {
          unset($form['actions'][$key]);
        }
      }
      return;
    }
  }

  // If we're not adding a form or rendering the main edit form, don't provide
  // the option of adding a new revision or modifying the revision message.
  if (!in_array($routename, static::getAddFormRoutes()) && !in_array($routename, static::getEditFormRoutes())) {

    // @TODO What is the following needed for?
    if ($config_entity) {
      $form_state
        ->setValue($config_entity
        ->getBundleName(), $config_entity
        ->get('loadedRevisionId'));
    }
    return;
  }
  if ($form_id == $config_entity
    ->previewFormId()) {
    return;
  }
  if (!$content_entity) {
    $content_entity = $config_entity
      ->contentEntityStorage()
      ->createInitialRevision($config_entity);
  }
  if (in_array($routename, static::getNewRevisionRoutes())) {

    // Add a log field if the "Create new revision" option is checked, or if the
    // current user has the ability to check that option.
    $new_revision_default = static::getNewRevisionDefault();
    $form['revision'] = [
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
      '#default_value' => $new_revision_default,
      '#group' => 'revision_information',
    ];

    // This will add the moderation form too, if needed.
    $entity_form_display = EntityFormDisplay::create([
      'targetEntityType' => 'config_entity_revisions',
      'bundle' => $config_entity
        ->revisionsEntityName(),
      'mode' => 'default',
      'status' => TRUE,
    ]);
    $entity_form_display
      ->buildForm($content_entity, $form, $form_state);
    $form['actions']['#weight'] = 200;
  }
  else {
    if (!$isLatestRevision && in_array($routename, static::getEditFormRoutes())) {
      unset($form['actions']);
      foreach ($form as $key => &$values) {
        if (in_array($key, [
          'form_token',
          'form_id',
          'form_build_id',
        ])) {
          continue;
        }
        if (substr($key, 0, 1) !== '#') {
          $values['#disabled'] = TRUE;
        }
      }
    }
  }
}