You are here

ViewRevisionsPreviewForm.php in Config Entity Revisions 8.2

File

modules/view_revisions/src/ViewRevisionsPreviewForm.php
View source
<?php

namespace Drupal\view_revisions;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Entity\View;
use Drupal\views_ui\ViewPreviewForm;
use Drupal\Core\Url;

/**
 * Class ViewRevisionsPreviewForm.
 *
 * @package Drupal\view_revisions
 */
class ViewRevisionsPreviewForm extends ViewPreviewForm {

  /**
   * The revision ID we're working with.
   *
   * @var \Drupal\views\Entity\View
   */
  private $revisionId = NULL;

  /**
   * Get revision id, reloading view if it has only just been created.
   *
   * @param \Drupal\views\Entity\View $view
   *   The view from which a revision ID should be retrieved.
   *
   * @return int
   *   The revision ID for the view.
   */
  private function getRevisionId(View $view) {
    if (!is_null($this->revisionId)) {
      return $this->revisionId;
    }
    $this->revisionId = $view
      ->get('loadedRevisionId');
    if (is_null($this->revisionId)) {

      // A new view has just been created. The revision has been made too, but
      // it's not in the entities provided. We need to get it directly from the
      // database.
      $reloaded = $this->entityTypeManager
        ->getStorage('view')
        ->load($view
        ->id());
      $config_entity_id = $reloaded
        ->getContentEntityId();
      $revision = $reloaded
        ->contentEntityStorage()
        ->getLatestRevision($config_entity_id);
      $this->revisionId = $revision
        ->id();
    }
    return $this->revisionId;
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    // Modify the preview URL to the version with a revision ID.
    $view = $this->entity;
    $linkTemplates = $view
      ->get('storage')
      ->getEntityType()
      ->getLinkTemplates();

    //    foreach($linkTemplates as $name => $template) {
    $old_uri = $view
      ->urlInfo('preview-form');
    $route_parameters = $old_uri
      ->getRouteParameters();
    $route_options = $old_uri
      ->getOptions();

    // We can't replace the route name, so we need to replace the whole object.
    $uri = Url::fromRoute('entity.view.preview_revision_form', $route_parameters, $route_options);
    $uri
      ->setRouteParameter('revision_id', $this
      ->getRevisionId($view
      ->get('storage')));
    $form['#action'] = $uri
      ->toString();
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    $view = $this->entity;
    $actions['button']['#ajax']['url'] = Url::fromRoute('entity.view.preview_revision_form', [
      'view' => $view
        ->id(),
      'revision_id' => $this
        ->getRevisionId($view
        ->get('storage')),
      'display_id' => $this->displayID,
    ]);
    return $actions;
  }

}

Classes

Namesort descending Description
ViewRevisionsPreviewForm Class ViewRevisionsPreviewForm.