You are here

abstract class ConfigEntityRevisionsFormAlterBase in Config Entity Revisions 8.2

Class ConfigEntityRevisionsFormAlterBase.

@package Drupal\config_entity_revisions

Hierarchy

Expanded class hierarchy of ConfigEntityRevisionsFormAlterBase

2 files declare their use of ConfigEntityRevisionsFormAlterBase
ViewRevisionsFormAlter.php in modules/view_revisions/src/ViewRevisionsFormAlter.php
WebformRevisionsFormAlter.php in modules/webform_revisions/src/WebformRevisionsFormAlter.php

File

src/ConfigEntityRevisionsFormAlterBase.php, line 13

Namespace

Drupal\config_entity_revisions
View source
abstract class ConfigEntityRevisionsFormAlterBase implements ConfigEntityRevisionsFormAlterInterface {

  /**
   * Routes that provide an add form.
   *
   * @return array
   *   Routes that provide an add form.
   */
  public static function getAddFormRoutes() {
    return [];
  }

  /**
   * Routes that provide add or edit forms.
   *
   * @return array
   *   Routes that provide add or edit forms.
   */
  public static function getEditFormRoutes() {
    return [];
  }

  /**
   * Routes that get new revision form fields.
   *
   * @return array
   *   Routes that need the new revision subform.
   */
  public static function getNewRevisionRoutes() {
    return [];
  }

  /**
   * Routes that view the entity.
   *
   * @return array
   *   Routes that view the entity.
   */
  public static function getViewFormRoutes() {
    return [];
  }

  /**
   * Additional routes (to $add_edit_form_routes) that need a revision id.
   *
   * @return array
   *   Additional routes (to $add_edit_form_routes) that need a revision id.
   */
  public static function getRevisionRoutes() {
    return array_merge(self::getAddFormRoutes(), self::getEditFormRoutes(), self::getViewFormRoutes(), []);
  }

  /**
   * Do we create a new revision by default? (Generally no for config entities).
   *
   * @return bool
   *   Whether the 'New revision' checkbox should default on.
   */
  public static function getNewRevisionDefault() {
    return FALSE;
  }

  /**
   * Common form_alter hook 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;
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigEntityRevisionsFormAlterBase::formAlter public static function Common form_alter hook code. Overrides ConfigEntityRevisionsFormAlterInterface::formAlter
ConfigEntityRevisionsFormAlterBase::getAddFormRoutes public static function Routes that provide an add form. Overrides ConfigEntityRevisionsFormAlterInterface::getAddFormRoutes 2
ConfigEntityRevisionsFormAlterBase::getEditFormRoutes public static function Routes that provide add or edit forms. Overrides ConfigEntityRevisionsFormAlterInterface::getEditFormRoutes 2
ConfigEntityRevisionsFormAlterBase::getNewRevisionDefault public static function Do we create a new revision by default? (Generally no for config entities). Overrides ConfigEntityRevisionsFormAlterInterface::getNewRevisionDefault
ConfigEntityRevisionsFormAlterBase::getNewRevisionRoutes public static function Routes that get new revision form fields. 2
ConfigEntityRevisionsFormAlterBase::getRevisionRoutes public static function Additional routes (to $add_edit_form_routes) that need a revision id. Overrides ConfigEntityRevisionsFormAlterInterface::getRevisionRoutes 2
ConfigEntityRevisionsFormAlterBase::getViewFormRoutes public static function Routes that view the entity. 1
ConfigEntityRevisionsFormAlterInterface::getRouteParameterName public static function Route parameter name. 2