View source
<?php
use Drupal\views_revisions\ViewsRevisionFields;
use Drupal\views_revisions\Controller\ViewsRevisionsController;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views_revisions\Entity\ViewsRevisions;
use Drupal\config_entity_revisions\ConfigEntityRevisionsInterface;
use Drupal\config_entity_revisions\ConfigEntityRevisionsControllerInterface;
use Drupal\views_revisions\ViewsRevisionsUI;
function views_revisions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$view = NULL;
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();
$contentEntity = NULL;
$match = \Drupal::service('router')
->matchRequest(\Drupal::request());
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 ($routename == 'entity.view.revision') {
if (!$contentEntity
->isDefaultRevision()) {
\Drupal::messenger()
->addWarning('This is not the currently published revision of the view.');
$form['actions'] = [];
}
return;
}
if (!in_array($routename, $add_edit_form_routes)) {
if ($view) {
$form_state
->setValue('view_revision', $view
->get('loadedRevisionId'));
}
return;
}
ViewsRevisionFields::addRevisionFormFields($form);
if (!$contentEntity) {
$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;
}
function views_revisions_view_update(ViewsRevisions $view) {
$controller = ViewsRevisionsController::create(\Drupal::getContainer());
$controller
->createUpdateRevision($view);
}
function views_revisions_view_delete(ViewsRevisions $view) {
$controller = ViewsRevisionsController::create(\Drupal::getContainer());
$controller
->deleteRevisions($view);
}
function views_revisions_menu_local_tasks_alter(&$data, $route_name, \Drupal\Core\Cache\RefinableCacheableDependencyInterface &$cacheability) {
if (strpos($route_name, 'entity.view') === 0) {
foreach ($data['tabs'] as $tab_level) {
foreach ($tab_level as $tab) {
$url = $tab['#link']['url'];
$tab_route_name = $url
->getRouteName();
$tab_route_parameters = $url
->getRouteParameters();
if (strpos($tab_route_name, 'entity.view') !== FALSE && isset($tab_route_parameters['view'])) {
$url
->setRouteParameter('config_entity', $tab_route_parameters['view']);
}
}
}
}
}
function views_revisions_entity_type_alter(&$entity_types) {
if (isset($entity_types['view'])) {
$entity_types['view']
->setClass('Drupal\\views_revisions\\Entity\\ViewsRevisions')
->setStorageClass('Drupal\\views_revisions\\ViewsRevisionsStorage')
->setLinkTemplate('revisions', '/admin/structure/views/view/{view}/revisions')
->setLinkTemplate('revision', '/admin/structure/views/view/{view}/revision/{revision_id}')
->setFormClass('edit', 'Drupal\\views_revisions\\ViewsRevisionsEditForm');
}
}
function views_revisions_entity_operation_alter(array &$operations, \Drupal\Core\Entity\EntityInterface $entity) {
if ($entity
->access('revisions') && $entity
->hasLinkTemplate('revisions')) {
$operations['revisions'] = [
'title' => t('Revisions'),
'weight' => 100,
'url' => $entity
->toUrl('revisions'),
];
}
}