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 extends ViewPreviewForm {
private $revisionId = NULL;
private function getRevisionId(View $view) {
if (!is_null($this->revisionId)) {
return $this->revisionId;
}
$this->revisionId = $view
->get('loadedRevisionId');
if (is_null($this->revisionId)) {
$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;
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$view = $this->entity;
$linkTemplates = $view
->get('storage')
->getEntityType()
->getLinkTemplates();
$old_uri = $view
->urlInfo('preview-form');
$route_parameters = $old_uri
->getRouteParameters();
$route_options = $old_uri
->getOptions();
$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;
}
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;
}
}