View source
<?php
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Utility\Error;
use Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\multiversion\MultiversionFieldItemList;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Core\Entity\EntityInterface;
use Drupal\multiversion\Entity\Workspace;
function multiversion_module_implements_alter(&$implementations, $hook) {
if ($hook == 'entity_type_alter') {
$group = $implementations['multiversion'];
unset($implementations['multiversion']);
$implementations = [
'multiversion' => $group,
] + $implementations;
}
if ($hook == 'field_info_alter') {
$group = $implementations['multiversion'];
unset($implementations['multiversion']);
$implementations['multiversion'] = $group;
}
}
function multiversion_entity_type_alter(array &$entity_types) {
$manager = \Drupal::service('multiversion.manager');
foreach ($entity_types as $entity_type) {
if ($manager
->allowToAlter($entity_type)) {
if (!$entity_type
->isRevisionable()) {
$keys = $entity_type
->getKeys();
$keys['revision'] = 'revision_id';
$entity_type
->set('entity_keys', $keys);
if ($entity_type
->getRevisionTable() === null) {
$entity_type
->set('revision_table', $entity_type
->id() . '_revision');
}
if ($entity_type
->getRevisionDataTable() === null) {
$entity_type
->set('revision_data_table', $entity_type
->id() . '_field_revision');
}
}
$namespace = 'Drupal\\multiversion\\Entity\\Storage\\Sql';
$original_storage_class = $entity_type
->getHandlerClass('storage');
$entity_type
->setHandlerClass('original_storage', $original_storage_class);
switch ($entity_type
->id()) {
case 'node':
$entity_type
->setHandlerClass('storage', "{$namespace}\\NodeStorage");
break;
case 'taxonomy_term':
$entity_type
->setHandlerClass('storage', "{$namespace}\\TermStorage");
break;
case 'comment':
$entity_type
->setHandlerClass('storage', "{$namespace}\\CommentStorage");
break;
case 'menu_link_content':
$entity_type
->setClass('Drupal\\multiversion\\Entity\\MenuLinkContent');
$entity_type
->setHandlerClass('storage', "{$namespace}\\MenuLinkContentStorage");
break;
case 'file':
$entity_type
->setHandlerClass('storage', "{$namespace}\\FileStorage");
break;
case 'media':
$entity_type
->setHandlerClass('storage', "{$namespace}\\MediaStorage");
break;
case 'paragraph':
$entity_type
->setClass('Drupal\\multiversion\\Entity\\Paragraph');
$entity_type
->setHandlerClass('storage', "{$namespace}\\ContentEntityStorage");
break;
case 'poll':
$entity_type
->setHandlerClass('storage', "{$namespace}\\PollStorage");
break;
case 'crop':
$entity_type
->setHandlerClass('storage', "{$namespace}\\CropStorage");
break;
case 'redirect':
$entity_type
->setHandlerClass('storage_schema', 'Drupal\\multiversion\\Redirect\\RedirectStorageSchema');
if (in_array($original_storage_class, [
NULL,
'Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage',
])) {
$entity_type
->setHandlerClass('storage', "{$namespace}\\ContentEntityStorage");
}
break;
default:
if (in_array($original_storage_class, [
NULL,
'Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage',
])) {
$entity_type
->setHandlerClass('storage', "{$namespace}\\ContentEntityStorage");
}
break;
}
}
}
if (isset($entity_types['block_content']) && $manager
->allowToAlter($entity_types['block_content'])) {
$entity_types['block']
->setHandlerClass('storage', 'Drupal\\multiversion\\Entity\\Storage\\Sql\\BlockStorage');
}
}
function multiversion_entity_base_field_info(EntityTypeInterface $entity_type) {
$manager = \Drupal::service('multiversion.manager');
if ($manager
->allowToAlter($entity_type)) {
$fields = [];
$minor_version = substr(\Drupal::VERSION, 0, 3);
$revision_key = $entity_type
->getKey('revision') ?: 'revision_id';
$fields[$revision_key] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The local revision ID of the entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
if (version_compare($minor_version, '8.5', '>=')) {
$fields['revision_default'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Default revision'))
->setDescription(t('A flag indicating whether this was a default revision when it was saved.'))
->setStorageRequired(TRUE)
->setTranslatable(FALSE)
->setRevisionable(TRUE)
->setInitialValue(TRUE);
}
if ($entity_type
->get('workspace') !== FALSE) {
$fields['workspace'] = BaseFieldDefinition::create('workspace_reference')
->setLabel(t('Workspace reference'))
->setDescription(t('The workspace this entity belongs to.'))
->setSetting('target_type', 'workspace')
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setCardinality(1)
->setReadOnly(TRUE);
}
$fields['_deleted'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Deleted flag'))
->setDescription(t('Indicates if the entity is flagged as deleted or not.'))
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setDefaultValue(FALSE)
->setCardinality(1);
$fields['_rev'] = BaseFieldDefinition::create('revision_token')
->setLabel(t('Revision token'))
->setDescription(t('The token for this entity revision.'))
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setCardinality(1)
->setReadOnly(TRUE);
if (version_compare($minor_version, '8.4', '>=') && $entity_type
->isTranslatable()) {
$fields[$entity_type
->getKey('revision_translation_affected')] = BaseFieldDefinition::create('boolean')
->setName($entity_type
->getKey('revision_translation_affected'))
->setTargetEntityTypeId($entity_type
->id())
->setTargetBundle(NULL)
->setLabel(new TranslatableMarkup('Revision translation affected'))
->setDescription(new TranslatableMarkup('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
}
return $fields;
}
}
function multiversion_data_type_info_alter(&$info) {
$info['entity_reference']['class'] = '\\Drupal\\multiversion\\EntityReference';
}
function multiversion_field_info_alter(&$info) {
if (\Drupal::state()
->get('multiversion_uninstalling', FALSE)) {
return;
}
$info['uuid']['class'] = '\\Drupal\\multiversion\\Field\\UuidItem';
$info['entity_reference']['class'] = '\\Drupal\\multiversion\\EntityReferenceItem';
$info['file']['class'] = '\\Drupal\\multiversion\\FileItem';
$info['image']['class'] = '\\Drupal\\multiversion\\ImageItem';
if (isset($info['entity_reference_revisions'])) {
$info['entity_reference_revisions']['class'] = '\\Drupal\\multiversion\\EntityReferenceRevisionsItem';
}
if (\Drupal::moduleHandler()
->moduleExists('pathauto')) {
$info['path']['list_class'] = MultiversionFieldItemList::class;
}
}
function multiversion_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
$manager = \Drupal::service('multiversion.manager');
if ($manager
->allowToAlter($entity_type)) {
$exclude_fields = [
$entity_type
->getKey('id'),
$entity_type
->getKey('revision'),
$entity_type
->getKey('uuid'),
$entity_type
->getKey('bundle'),
$entity_type
->getKey('langcode'),
'workspace',
'_deleted',
'_rev',
];
if ($entity_type
->id() == 'comment') {
$exclude_fields[] = 'comment_type';
}
foreach ($fields as $key => $field) {
if (!in_array($key, $exclude_fields)) {
$field
->setRevisionable(TRUE);
}
}
}
}
function multiversion_workspace_update(EntityInterface $entity) {
if (!$entity
->isPublished() && $entity->original
->isPublished()) {
$default_workspace = \Drupal::getContainer()
->getParameter('workspace.default');
\Drupal::service('workspace.manager')
->setActiveWorkspace(Workspace::load($default_workspace));
}
}
function multiversion_workspace_presave(EntityInterface $entity) {
if ($entity
->isDefaultWorkspace() && !$entity
->isPublished() && $entity->original
->isPublished()) {
throw new Exception('The default workspace cannot be archived.');
}
}
function multiversion_paragraph_update(EntityInterface $entity) {
$parent = $entity
->getParentEntity();
$entity_revision_parent_field_name = $entity
->getEntityType()
->get('entity_revision_parent_field_name_field');
if ($parent instanceof ContentEntityInterface && !empty($entity->workspace) && $entity_revision_parent_field_name && !empty($entity->{$entity_revision_parent_field_name}->value) && ($entity->_rev->is_stub || !empty($entity->original) && $entity->original->_rev->is_stub)) {
$parent_field_name = $entity->{$entity_revision_parent_field_name}->value;
$storage = \Drupal::entityTypeManager()
->getStorage($parent
->getEntityTypeId());
$storage
->resetCache([
$parent
->id(),
]);
$parent = $storage
->load($parent
->id());
if ($parent && !empty($parent->{$parent_field_name})) {
$save_parent = FALSE;
$values = $parent->{$parent_field_name}
->getValue();
$updated_values = [];
foreach ($values as $delta => $value) {
$updated_values[$delta] = $value;
if ($value['target_id'] == $entity
->id() && $value['target_revision_id'] != $entity
->getRevisionId()) {
$updated_values[$delta]['target_revision_id'] = $entity
->getRevisionId();
$save_parent = TRUE;
}
}
if ($save_parent) {
$parent->{$parent_field_name}
->setValue($updated_values);
try {
$storage
->saveWithoutForcingNewRevision($parent);
$storage
->resetCache([
$parent
->id(),
]);
} catch (\Exception $e) {
$details = t('Failed to save parent entity with UUID: %uuid_parent for paragraph entity with UUID: %uuid_paragraph.', [
'%uuid_parent' => $parent
->uuid(),
'%uuid_paragraph' => $entity
->uuid(),
]);
\Drupal::logger('Multiversion')
->error('%type: @message in %function (line %line of %file). ' . $details, Error::decodeException($e));
}
}
}
}
}
function multiversion_views_data_alter(array &$data) {
foreach ($data as $key => $item) {
if (isset($data[$key]['_rev'])) {
$data[$key]['_rev']['field']['id'] = 'standard';
}
if (isset($data[$key]['_deleted'])) {
$data[$key]['_deleted']['filter']['use_equal'] = TRUE;
}
if (isset($data[$key]['workspace'])) {
$data[$key]['current_workspace'] = [
'title' => t('Current workspace'),
'help' => t('Filters content by current active workspace.'),
'filter' => [
'field' => 'workspace',
'id' => 'current_workspace',
'label' => t('Current workspace'),
],
];
unset($data[$key]['workspace']);
}
}
}
function multiversion_views_post_execute(ViewExecutable $view) {
$base_field = $view->storage
->get('base_field');
$table_info = $view->query
->getEntityTableInfo();
$content_type_info = array_column($table_info, 'entity_type');
if (is_array($view->result) && ($content_type = reset($content_type_info))) {
$manager = \Drupal::service('multiversion.manager');
$storage = \Drupal::entityTypeManager()
->getStorage($content_type);
if ($storage instanceof ContentEntityStorageInterface && $manager
->allowToAlter($storage
->getEntityType())) {
$ids = [];
foreach ($view->result as $index => $row) {
if (empty($row->_entity) && !empty($row->{$base_field})) {
$ids[$index] = $row->{$base_field};
}
}
$entities = $storage
->loadMultipleDeleted($ids);
foreach ($view->result as $index => $row) {
if (empty($row->_entity) && !empty($row->{$base_field}) && isset($entities[$row->{$base_field}])) {
$view->result[$index]->_entity = $entities[$row->{$base_field}];
}
elseif (empty($row->_entity)) {
unset($view->result[$index]);
}
}
}
}
}
function multiversion_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
$views_ids = [
'content',
'frontpage',
'comments_recent',
'content_recent',
'taxonomy_term',
'glossary',
'archive',
'block_content',
'poll_admin',
'poll_list',
'media_library',
];
if (in_array($view
->id(), $views_ids)) {
$manager = \Drupal::service('multiversion.manager');
$entity_type = $view
->getBaseEntityType();
if (!$manager
->isEnabledEntityType($entity_type)) {
return;
}
$base_table = $view->storage
->get('base_table');
$view->query->where[1]['conditions'][] = [
'field' => $base_table . '._deleted',
'value' => FALSE,
'operator' => '=',
];
$view->query->where[1]['conditions'][] = [
'field' => $base_table . '.workspace',
'value' => multiversion_get_active_workspace_id(),
'operator' => '=',
];
}
}
function multiversion_element_info_alter(array &$types) {
foreach ($types as &$type) {
if (!isset($type['#pre_render'])) {
$type['#pre_render'] = [];
}
$type['#pre_render'][] = 'multiversion_element_pre_render';
}
}
function multiversion_element_pre_render($element) {
if (isset($element['#cache'])) {
if (!isset($element['#cache']['contexts'])) {
$element['#cache']['contexts'] = [];
}
$element['#cache']['contexts'] = Cache::mergeContexts($element['#cache']['contexts'], [
'workspace',
]);
}
return $element;
}
function multiversion_get_active_workspace_id() {
return \Drupal::service('multiversion.manager')
->getActiveWorkspaceId();
}
function multiversion_workspace_uri(WorkspaceInterface $entity) {
return $entity
->id();
}
function multiversion_form_node_type_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
unset($form['workflow']['options']['#options']['revision']);
}
function multiversion_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (isset($form['revision']['#group']) && $form['revision']['#group'] == 'revision_information') {
$form['revision']['#default_value'] = TRUE;
$form['revision']['#disabled'] = TRUE;
}
}
function multiversion_prepare_file_destination($destination) {
$dirname = \Drupal::service('file_system')
->dirname($destination);
return file_prepare_directory($dirname, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
}
function multiversion_menu_links_discovered_alter(&$links) {
if (\Drupal::moduleHandler()
->moduleExists('menu_link_content')) {
$storage = \Drupal::entityTypeManager()
->getStorage('menu_link_content');
if (\Drupal::service('multiversion.manager')
->isEnabledEntityType($storage
->getEntityType())) {
$workspaces = Workspace::loadMultiple();
foreach ($workspaces as $workspace_id => $workspace) {
$storage
->useWorkspace($workspace_id);
$menu_link_content_entities = $storage
->loadMultiple();
$new_ids = [];
$links_to_purge = [];
foreach ($menu_link_content_entities as $menu_link_content) {
$uuid = $menu_link_content
->uuid();
$old_id = "menu_link_content:{$uuid}";
$new_id = "{$old_id}:" . $menu_link_content
->id();
if (isset($links[$old_id])) {
$links_to_purge[] = $old_id;
unset($links[$old_id]);
}
$new_ids[$old_id] = $new_id;
if (!isset($links[$new_id])) {
$links[$new_id] = $menu_link_content
->getPluginDefinition();
}
$links[$new_id]['class'] = 'Drupal\\multiversion\\Plugin\\Menu\\MenuLinkContent';
}
if ($links_to_purge) {
\Drupal::service('menu.tree_storage')
->purgeMultiple($links_to_purge);
}
foreach ($links as $id => $link) {
if (!empty($link['parent']) && in_array($link['parent'], array_keys($new_ids))) {
$links[$id]['parent'] = $new_ids[$link['parent']];
}
}
$storage
->useWorkspace(NULL);
}
}
}
}
function multiversion_modules_installed($modules) {
$entity_type_manager = \Drupal::entityTypeManager();
$supported_entity_types = \Drupal::configFactory()
->getEditable('multiversion.settings')
->get('supported_entity_types');
$supported_entity_types = $supported_entity_types ?: [];
$entities_to_enable = [];
foreach ($supported_entity_types as $entity_type_id) {
$entity_type = $entity_type_manager
->getDefinition($entity_type_id, FALSE);
if (!empty($entity_type) && in_array($entity_type
->getProvider(), $modules)) {
$entities_to_enable[$entity_type_id] = $entity_type;
}
}
if (!empty($entities_to_enable)) {
\Drupal::service('multiversion.manager')
->enableEntityTypes($entities_to_enable);
}
}
function multiversion_search_plugin_alter(array &$definitions) {
if (isset($definitions['node_search'])) {
$definitions['node_search']['class'] = 'Drupal\\multiversion\\Entity\\Search\\NodeSearch';
}
}
function multiversion_preprocess_html(&$variables) {
$active_workspace = \Drupal::service('workspace.manager')
->getActiveWorkspace();
if ($active_workspace && ($machine_name = $active_workspace
->getMachineName())) {
$variables['attributes']['class'][] = 'workspace-' . $machine_name;
}
}
function multiversion_form_revision_overview_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['node_revisions_table'])) {
foreach ($form['node_revisions_table'] as $key => $revision_info) {
if (empty($revision_info['operations']) || !is_array($revision_info['operations'])) {
continue;
}
if (isset($revision_info['operations']['#links']['delete'])) {
unset($form['node_revisions_table'][$key]['operations']['#links']['delete']);
}
}
}
}
function _multiversion_add_workspace_field_in_url_alias_table($install = FALSE) {
$database = \Drupal::database();
$schema = $database
->schema();
$table = 'url_alias';
$field = 'workspace';
if (!$schema
->fieldExists($table, $field)) {
$spec = [
'description' => 'The workspace the alias belongs to.',
'type' => 'int',
'unsigned' => TRUE,
'default' => $install ? 1 : 0,
'size' => 'normal',
];
$schema
->addField($table, $field, $spec);
}
$table_data = $database
->select($table)
->fields($table)
->execute()
->fetchAll();
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_manager
->clearCachedDefinitions();
$entity_ids = [];
foreach ($table_data as $row) {
$row_data = (array) $row;
$source_elements = explode('/', $row_data['source']);
if (!empty($source_elements[1]) && !empty($source_elements[2]) && is_numeric($source_elements[2])) {
if ($entity_type_manager
->getDefinition($source_elements[1], FALSE)) {
$entity_ids[$source_elements[1]][$row_data['pid']] = $source_elements[2];
}
}
}
$workspaces = Workspace::loadMultiple();
foreach ($workspaces as $workspace_id => $workspace) {
foreach ($entity_ids as $entity_type_id => $ids) {
$storage = $entity_type_manager
->getStorage($entity_type_id);
if ($storage instanceof ContentEntityStorageInterface) {
$storage
->useWorkspace($workspace_id);
$entities = $storage
->loadMultiple($ids);
$storage
->useWorkspace(NULL);
}
if (empty($entities)) {
continue;
}
foreach ($ids as $pid => $entity_id) {
if (in_array($entity_id, array_keys($entities))) {
$database
->update($table)
->fields([
$field => $workspace_id,
])
->condition('pid', $pid)
->execute();
}
}
}
}
}