You are here

function views_revisions_form_alter in Config Entity Revisions 1.x

Same name and namespace in other branches
  1. 8 modules/views_revisions/views_revisions.module \views_revisions_form_alter()

Implements hook_form_alter().

File

modules/views_revisions/views_revisions.module, line 16

Code

function views_revisions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $view = NULL;

  // Modify the label on the workflow form so it doesn't say "Config entities".
  if ($form_id == 'workflow_edit_form') {
    $entity_types =& $form['type_settings']['entity_types_container']['entity_types'];
    if (!empty($entity_types['ViewRevisions'])) {
      $entity_types['ViewRevisions']['type']['#context']['label'] = t('Views');
    }
    return;
  }
  $add_edit_form_routes = [
    'entity.view.edit_form',
    'entity.view.source_form',
  ];
  $revision_routes = array_merge($add_edit_form_routes, [
    'view_ui_element_form',
    'entity.view.revision',
  ]);
  $routename = \Drupal::routeMatch()
    ->getRouteName();

  /* @var $contentEntity ConfigEntityRevisionsInterface */
  $contentEntity = NULL;

  // 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.
  $match = \Drupal::service('router')
    ->matchRequest(\Drupal::request());

  /* @var $view ViewUI */
  if (isset($match['view'])) {
    $view = $match['view'];
    if ($view instanceof ViewsRevisionsUI) {
      $view = $view
        ->get('storage');
      $contentEntity = $view
        ->getContentEntity();
    }
  }
  if (in_array($routename, $revision_routes) && !$view) {
    return;
  }

  // If we're displaying a non-current revision, add a message and remove the
  // submission buttons.
  if ($routename == 'entity.view.revision') {
    if (!$contentEntity
      ->isDefaultRevision()) {
      \Drupal::messenger()
        ->addWarning('This is not the currently published revision of the view.');
      $form['actions'] = [];
    }
    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, $add_edit_form_routes)) {
    if ($view) {
      $form_state
        ->setValue('view_revision', $view
        ->get('loadedRevisionId'));
    }
    return;
  }
  ViewsRevisionFields::addRevisionFormFields($form);
  if (!$contentEntity) {

    /* @var $revisionsController ConfigEntityRevisionsControllerInterface */
    $revisionsController = ViewsRevisionsController::create(\Drupal::getContainer());
    $contentEntity = $revisionsController
      ->createInitialRevision($view);
  }
  $entity_form_display = EntityFormDisplay::create([
    'targetEntityType' => 'config_entity_revisions',
    'bundle' => 'ViewRevisions',
    'mode' => 'default',
    'status' => TRUE,
  ]);
  $entity_form_display
    ->buildForm($contentEntity, $form, $form_state);
  $form['actions']['#weight'] = 200;
}