Skip to content
Snippets Groups Projects
Commit 1038293b authored by Sergey Sergin's avatar Sergey Sergin
Browse files

Issue #2860034 by Bwolf: Ability to Merge Duplicate Contributor Entries After Import

parent 854254db
No related branches found
Tags 8.x-1.0-alpha3
No related merge requests found
Showing
with 1020 additions and 0 deletions
<?php
/**
* @file
* Batch callbacks.
*/
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Batch operation. Change change source contributor id to target.
*
* @param int $source_id
* Source entity identifier.
* @param int $target_id
* Target entity identifier.
* @param string $entity_type_id
* Entity type identifier.
* @param string $field_name
* Field name to search.
* @param array $context
* Batch context array.
*/
function bibcite_entity_merge_entity($source_id, $target_id, $entity_type_id, $field_name, array &$context) {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type = $entity_type_manager->getDefinition($entity_type_id);
$reference_storage = $entity_type_manager->getStorage('bibcite_reference');
$storage = $entity_type_manager->getStorage($entity_type_id);
$source_entity = $storage->load($source_id);
if (empty($context['results'])) {
$context['results'] = [
'references' => [],
'references_count' => 0,
'entities' => [],
'entity_type_id' => $entity_type_id,
'label' => $source_entity->label(),
];
}
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['sandbox']['max'] = $reference_storage->getQuery()->condition($field_name, $source_id)->count()->execute();
$context['results']['references_count'] += $context['sandbox']['max'];
}
$query = $reference_storage->getQuery();
$query
->condition($field_name, $source_id)
->condition('id', $context['sandbox']['current_id'], '>')
->sort('id')
->range(0, 10);
$reference_entities = $reference_storage->loadMultiple($query->execute());
/** @var \Drupal\bibcite_entity\Entity\ReferenceInterface $reference_entity */
foreach ($reference_entities as $reference_entity) {
$field = $reference_entity->get($field_name);
$field_value = $field->getValue();
_bibcite_entity_process_field_value($field_value, $source_id, $target_id);
$field->setValue($field_value);
$reference_entity->save();
$context['results']['references'][] = $reference_entity->id() . ' : ' . $reference_entity->label();
$context['sandbox']['progress']++;
$context['sandbox']['current_id'] = $reference_entity->id();
}
$source_entity_message = t('Current @entity_type_label: @source_entity_label', [
'@entity_type_label' => $entity_type->getLabel(),
'@source_entity_label' => $source_entity->label(),
]);
$context['message'] = $source_entity_message;
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Find source field value and replace or remove it.
*
* @param array $field_value
* Field value array.
* @param int $source_id
* Source entity identifier.
* @param int $target_id
* Target entity identifier.
*/
function _bibcite_entity_process_field_value(array &$field_value, $source_id, $target_id) {
$target_delta = _bibcite_entity_get_target_delta($field_value, $target_id);
foreach ($field_value as $delta => $value) {
if ($value['target_id'] == $source_id) {
if (isset($target_delta)) {
unset($field_value[$delta]);
}
else {
$field_value[$delta]['target_id'] = $target_id;
}
}
}
}
/**
* Find target identifier in field value.
*
* @param array $field_value
* Field value array.
* @param int $target_id
* Target entity identifier.
*
* @return int|null
* Position of target entity or NULL if target entity is not present in value.
*/
function _bibcite_entity_get_target_delta(array $field_value, $target_id) {
foreach ($field_value as $delta => $value) {
if ($value['target_id'] == $target_id) {
return $delta;
}
}
return NULL;
}
/**
* Batch operation. Delete entity.
*
* @param int $source_id
* Source entity identifier to delete.
* @param string $entity_type_id
* Entity type identifier.
* @param string $field_name
* Field name to search.
* @param array $context
* Batch context array.
*/
function bibcite_entity_merge_entity_delete($source_id, $entity_type_id, $field_name, array &$context) {
$count = \Drupal::entityQuery('bibcite_reference')->condition($field_name, $source_id)->count()->execute();
$storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
$entity = $storage->load($source_id);
if ($count) {
$context['results']['failed'][$entity->id()] = $entity->label();
}
else {
$entity->delete();
$context['results']['entities'][$entity->id()] = $entity->label();
}
}
/**
* Batch finished callback.
*
* @param bool $success
* A boolean indicating whether the batch has completed successfully.
* @param array $results
* The value set in $context['results'] by operation callback.
* @param array $operations
* If $success is FALSE, contains the operations that remained unprocessed.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Redirect to collection route.
*/
function bibcite_entity_merge_entity_finished($success, array $results, array $operations) {
$string_translation = \Drupal::translation();
$entity_type_manager = \Drupal::entityTypeManager();
$reference_message = t('@count of @max references successfully processed', [
'@count' => count($results['references']),
'@max' => $results['references_count'],
]);
drupal_set_message($reference_message);
$entity_type = $entity_type_manager->getDefinition($results['entity_type_id']);
$message = $string_translation->formatPlural(count($results['entities']),
'%label has been successfully merged and deleted',
'@count %plural_label have been successfully merged and deleted',
[
'%label' => $results['label'],
'%plural_label' => $entity_type->getPluralLabel(),
]);
drupal_set_message($message);
if (!empty($results['failed'])) {
drupal_set_message(t("These @entity_type processed with errors and have not been deleted:\n", [
'@entity_type' => $entity_type->getPluralLabel(),
]), 'error');
foreach ($results['failed'] as $id => $label) {
drupal_set_message("$label ($id)\n", 'error');
}
}
$redirect_url = new Url("entity.{$results['entity_type_id']}.collection");
return new RedirectResponse($redirect_url->toString());
}
......@@ -120,3 +120,8 @@ bibcite_entity.mapping.format:
route_name: bibcite_entity.mapping
parent_id: bibcite_entity.mapping
deriver: '\Drupal\bibcite_entity\Plugin\Derivative\FormatLocalTask'
bibcite_entity.merge:
deriver: 'Drupal\bibcite_entity\Plugin\Derivative\MergeLocalTask'
title: 'Merge'
weight: 10
\ No newline at end of file
<?php
/**
* @file
* Module hooks.
......@@ -9,6 +10,34 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
/**
* Implements hook_entity_operation().
*/
function bibcite_entity_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity->hasLinkTemplate('bibcite-merge-form')) {
$operations['bibcite_merge'] = [
'title' => t('Merge'),
'weight' => 10,
'url' => $entity->toUrl('bibcite-merge-form'),
];
}
return $operations;
}
/**
* Implements hook_preprocess_HOOK().
*
* Destination query element break redirect between forms.
*/
function bibcite_entity_preprocess_links__dropbutton__operations(&$variables) {
if (isset($variables['links']['bibcite_merge']['link']['#options']['query']['destination'])) {
unset($variables['links']['bibcite_merge']['link']['#options']['query']['destination']);
}
}
/**
* Implements hook_theme().
*/
......
langcode: en
status: true
dependencies:
module:
- bibcite_entity
id: bibcite_entity_contributor_merge
label: 'Merge contributor'
type: bibcite_contributor
plugin: bibcite_entity_contributor_merge
configuration: { }
langcode: en
status: true
dependencies:
module:
- bibcite_entity
id: bibcite_entity_keyword_merge
label: 'Merge keyword'
type: bibcite_keyword
plugin: bibcite_entity_keyword_merge
configuration: { }
......@@ -31,6 +31,7 @@ use Drupal\Core\Entity\EntityTypeInterface;
* "route_provider" = {
* "html" = "Drupal\bibcite_entity\ContributorHtmlRouteProvider",
* "delete-multiple" = "Drupal\bibcite_entity\DeleteMultipleRouterProvider",
* "merge" = "Drupal\bibcite_entity\MergeRouteProvider",
* },
* },
* base_table = "bibcite_contributor",
......@@ -45,7 +46,9 @@ use Drupal\Core\Entity\EntityTypeInterface;
* "canonical" = "/bibcite/contributor/{bibcite_contributor}",
* "edit-form" = "/bibcite/contributor/{bibcite_contributor}/edit",
* "delete-form" = "/bibcite/contributor/{bibcite_contributor}/delete",
* "bibcite-merge-form" = "/bibcite/contributor/{bibcite_contributor}/merge",
* "add-form" = "/admin/content/bibcite/contributor/add",
* "bibcite-merge-multiple-form" = "/admin/content/bibcite/contributor/merge",
* "delete-multiple-form" = "/admin/content/bibcite/contributor/delete",
* "collection" = "/admin/content/bibcite/contributor",
* },
......
......@@ -30,6 +30,7 @@ use Drupal\Core\Entity\EntityTypeInterface;
* "route_provider" = {
* "html" = "Drupal\bibcite_entity\KeywordHtmlRouteProvider",
* "delete-multiple" = "Drupal\bibcite_entity\DeleteMultipleRouterProvider",
* "merge" = "Drupal\bibcite_entity\MergeRouteProvider",
* },
* },
* base_table = "bibcite_keyword",
......@@ -45,7 +46,9 @@ use Drupal\Core\Entity\EntityTypeInterface;
* "canonical" = "/bibcite/keyword/{bibcite_keyword}",
* "edit-form" = "/bibcite/keyword/{bibcite_keyword}/edit",
* "delete-form" = "/bibcite/keyword/{bibcite_keyword}/delete",
* "bibcite-merge-form" = "/bibcite/keyword/{bibcite_keyword}/merge",
* "add-form" = "/admin/content/bibcite/keyword/add",
* "bibcite-merge-multiple-form" = "/admin/content/bibcite/keyword/merge",
* "delete-multiple-form" = "/admin/content/bibcite/keyword/delete",
* "collection" = "/admin/content/bibcite/keyword",
* },
......
<?php
namespace Drupal\bibcite_entity\Form;
use Drupal\bibcite_entity\Entity\ReferenceInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Confirm merge of bibliographic entities.
*/
class MergeConfirmForm extends ConfirmFormBase {
/**
* This entity will be merged to target.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $source;
/**
* Source entity will be merged to this one.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $target;
/**
* The field name for filtering.
*
* @var string
*/
protected $fieldName;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('current_route_match'));
}
/**
* {@inheritdoc}
*/
public function __construct(RouteMatchInterface $route_match) {
$parameter_name = $route_match->getRouteObject()->getOption('_bibcite_entity_type_id');
$this->source = $route_match->getParameter($parameter_name);
$this->target = $route_match->getParameter("{$parameter_name}_target");
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'bibcite_entity_merge_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to merge @source to @target?', [
'@source' => $this->source->label(),
'@target' => $this->target->label(),
]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->source->toUrl('bibcite-merge-form');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $field_name = NULL) {
$this->fieldName = $field_name;
$statistic = $this->getAuthoredReferencesStatistic();
$form['references'] = [
'#type' => 'fieldset',
'#title' => $this->t('This operation will cause changes in these references'),
'items' => [
'#markup' => $this->t('No one reference will be changed.'),
],
];
if (count($statistic['entities']) > 0) {
$items = array_map(function (ReferenceInterface $reference) {
return $reference->label();
}, $statistic['entities']);
$form['references']['items'] = [
'#theme' => 'item_list',
'#items' => $items,
];
}
if ($statistic['count'] > 0) {
$form['references']['count'] = [
'#markup' => $this->t('and @count more', ['@count' => $statistic['count']]),
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$batch = [
'title' => t('Merging'),
'operations' => [
[
'bibcite_entity_merge_entity', [
$this->source->id(),
$this->target->id(),
$this->source->getEntityTypeId(),
$this->fieldName,
],
],
[
'bibcite_entity_merge_entity_delete', [
$this->source->id(),
$this->source->getEntityTypeId(),
$this->fieldName,
],
],
],
'finished' => 'bibcite_entity_merge_entity_finished',
'file' => drupal_get_path('module', 'bibcite_entity') . '/bibcite_entity.batch.inc',
];
batch_set($batch);
}
/**
* Find references and get statistic data.
*
* @return array
* Statistic data with first 10 objects and count of another references.
*/
private function getAuthoredReferencesStatistic() {
$storage = \Drupal::entityTypeManager()->getStorage('bibcite_reference');
$range = 10;
$query = $storage->getQuery();
$query->condition($this->fieldName, $this->source->id());
$query->range(0, $range);
$entities = $storage->loadMultiple($query->execute());
$count = $query->range()->count()->execute();
return [
'entities' => $entities,
'count' => ($count > $range) ? $count - $range : 0,
];
}
}
<?php
namespace Drupal\bibcite_entity\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Merge bibliographic entities.
*/
class MergeForm extends FormBase {
/**
* The entity object to merge.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('current_route_match'));
}
/**
* {@inheritdoc}
*/
public function __construct(RouteMatchInterface $route_match) {
$parameter_name = $route_match->getRouteObject()->getOption('_bibcite_entity_type_id');
$this->entity = $route_match->getParameter($parameter_name);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'bibcite_entity_merge';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['target'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('Select target'),
'#description' => $this->t('@entity_type_label to be merged into.', [
'@entity_type_label' => $this->entity->getEntityType()->getLabel(),
]),
'#target_type' => $this->entity->getEntityTypeId(),
'#required' => TRUE,
];
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Merge'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($form_state->getValue('target') == $this->entity->id()) {
$form_state->setErrorByName('target', $this->t('@label cannot be merged into oneself', ['@label' => $this->entity->label()]));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRedirect("entity.{$this->entity->getEntityTypeId()}.bibcite_merge_form_confirm", [
$this->entity->getEntityTypeId() => $this->entity->id(),
"{$this->entity->getEntityTypeId()}_target" => $form_state->getValue('target'),
]);
}
/**
* Title callback.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* Page title.
*/
public function getTitle() {
return $this->t('Merge @label', ['@label' => $this->entity->label()]);
}
}
<?php
namespace Drupal\bibcite_entity\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Merge multiple bibliographic entities into one.
*/
class MergeMultipleForm extends ConfirmFormBase {
/**
* The array of entities to delete.
*
* @var array
*/
protected $entityInfo = [];
/**
* The tempstore object.
*
* @var \Drupal\user\PrivateTempStore
*/
protected $tempStore;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current user object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The entity type.
*
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $entityType;
/**
* The field name for filtering.
*
* @var string
*/
protected $fieldName;
/**
* Constructs a DeleteMultiple form object.
*
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
* The entity manager.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user object.
*/
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $manager, AccountInterface $current_user) {
$this->tempStore = $temp_store_factory->get('bibcite_entity_merge_multiple');
$this->entityTypeManager = $manager;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('user.private_tempstore'),
$container->get('entity_type.manager'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'bibcite_entity_merge_multiple';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to merge these @entity_type_label?', [
'@entity_type_label' => $this->entityType->getPluralLabel(),
]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Merge');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url("entity.{$this->entityType->id()}.collection");
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $field_name = NULL) {
$this->entityType = $this->entityTypeManager->getDefinition($entity_type_id);
$this->entityInfo = $this->tempStore->get($this->currentUser->id());
$this->fieldName = $field_name;
$form['entities'] = [
'#theme' => 'item_list',
'#items' => $this->entityInfo,
];
$form['target'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('Select target'),
'#description' => $this->t('@entity_type_label to be merged into.', [
'@entity_type_label' => $this->entityType->getLabel(),
]),
'#target_type' => $this->entityType->id(),
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if (isset($this->entityInfo[$form_state->getValue('target')])) {
$form_state->setErrorByName('target', $this->t('@label cannot be merged into oneself', ['@label' => $this->entityInfo[$form_state->getValue('target')]]));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$target_id = $form_state->getValue('target');
$operations = [];
foreach ($this->entityInfo as $id => $label) {
$operations[] = [
'bibcite_entity_merge_entity', [
$id,
$target_id,
$this->entityType->id(),
$this->fieldName,
],
];
$operations[] = [
'bibcite_entity_merge_entity_delete', [
$id,
$this->entityType->id(),
$this->fieldName,
],
];
}
$batch = [
'title' => t('Merging'),
'operations' => $operations,
'finished' => 'bibcite_entity_merge_entity_finished',
'file' => drupal_get_path('module', 'bibcite_entity') . '/bibcite_entity.batch.inc',
];
batch_set($batch);
$this->tempStore->delete($this->currentUser->id());
}
}
<?php
namespace Drupal\bibcite_entity;
use Drupal\bibcite_entity\Form\MergeConfirmForm;
use Drupal\bibcite_entity\Form\MergeForm;
use Drupal\bibcite_entity\Form\MergeMultipleForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\EntityRouteProviderInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides route for merge bibliographic entities.
*/
class MergeRouteProvider implements EntityRouteProviderInterface {
/**
* Only these entity types allowed to be merged.
*
* @var array
*
* @todo Find a better way to provide field name for filtering references.
*/
protected $entityFields = [
'bibcite_contributor' => 'author',
'bibcite_keyword' => 'keywords',
];
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$routes = new RouteCollection();
$entity_type_id = $entity_type->id();
if (isset($this->entityFields[$entity_type_id])) {
if ($entity_type->hasLinkTemplate('bibcite-merge-form')) {
if ($route = $this->getMergeRoute($entity_type)) {
$routes->add("entity.{$entity_type_id}.bibcite_merge_form", $route);
}
if ($route = $this->getMergeConfirmRoute($entity_type)) {
$routes->add("entity.{$entity_type_id}.bibcite_merge_form_confirm", $route);
}
}
if ($entity_type->hasLinkTemplate('bibcite-merge-multiple-form')) {
if ($route = $this->getMergeMultipleRoute($entity_type)) {
$routes->add("entity.{$entity_type_id}.bibcite_merge_multiple_form", $route);
}
if ($route = $this->getMergeMultipleConfirmRoute($entity_type)) {
$routes->add("entity.{$entity_type_id}.bibcite_merge_multiple_form_confirm", $route);
}
}
}
return $routes;
}
protected function getMergeRoute(EntityTypeInterface $entity_type) {
$link_template = $entity_type->getLinkTemplate('bibcite-merge-form');
$entity_type_id = $entity_type->id();
$route = new Route($link_template);
$route
->setDefault('_form', MergeForm::class)
->setDefault('_title_callback', MergeForm::class . '::getTitle')
->setOption('_bibcite_entity_type_id', $entity_type_id)
->setOption('_admin_route', TRUE)
->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id],
])
->setRequirement('_permission', $entity_type->getAdminPermission());
return $route;
}
protected function getMergeConfirmRoute(EntityTypeInterface $entity_type) {
$link_template = $entity_type->getLinkTemplate('bibcite-merge-form');
$entity_type_id = $entity_type->id();
$route = new Route($link_template . '/{' . $entity_type_id . '_target}');
$route
->setDefault('_form', MergeConfirmForm::class)
->setDefault('field_name', $this->entityFields[$entity_type_id])
->setOption('_bibcite_entity_type_id', $entity_type_id)
->setOption('_admin_route', TRUE)
->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id],
"{$entity_type_id}_target" => ['type' => 'entity:' . $entity_type_id],
])
->setRequirement('_permission', $entity_type->getAdminPermission());
return $route;
}
protected function getMergeMultipleRoute(EntityTypeInterface $entity_type) {
$link_template = $entity_type->getLinkTemplate('bibcite-merge-multiple-form');
$entity_type_id = $entity_type->id();
$route = new Route($link_template);
$route
->setDefault('_form', MergeMultipleForm::class)
->setDefault('entity_type_id', $entity_type_id)
->setDefault('field_name', $this->entityFields[$entity_type_id])
->setOption('_admin_route', TRUE)
->setRequirement('_permission', $entity_type->getAdminPermission());
return $route;
}
protected function getMergeMultipleConfirmRoute(EntityTypeInterface $entity_type) {
return NULL;
}
}
<?php
namespace Drupal\bibcite_entity\Plugin\Action;
/**
* Merge contributor action.
*
* @Action(
* id = "bibcite_entity_contributor_merge",
* label = @Translation("Merge contributor"),
* type = "bibcite_contributor",
* confirm_form_route_name = "entity.bibcite_contributor.bibcite_merge_multiple_form",
* )
*/
class ContributorMerge extends EntityMergeBase {
}
<?php
namespace Drupal\bibcite_entity\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base merge action for bibcite entities.
*/
class EntityMergeBase extends ActionBase implements ContainerFactoryPluginInterface {
/**
* The tempstore object.
*
* @var \Drupal\user\PrivateTempStore
*/
protected $tempStore;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Constructs a new DeleteNode object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
* @param \Drupal\Core\Session\AccountInterface $current_user
* Current user.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, PrivateTempStoreFactory $temp_store_factory, AccountInterface $current_user) {
$this->currentUser = $current_user;
$this->tempStore = $temp_store_factory->get('bibcite_entity_merge_multiple');
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('user.private_tempstore'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/**@var \Drupal\Core\Entity\EntityInterface $object */
return $account->hasPermission($object->getEntityType()->getAdminPermission());
}
/**
* {@inheritdoc}
*/
public function executeMultiple(array $entities) {
$info = [];
/** @var \Drupal\bibcite_entity\Entity\ReferenceInterface $entity */
foreach ($entities as $entity) {
$info[$entity->id()] = $entity->label();
}
$this->tempStore->set($this->currentUser->id(), $info);
}
/**
* {@inheritdoc}
*/
public function execute($object = NULL) {
$this->executeMultiple([$object]);
}
}
<?php
namespace Drupal\bibcite_entity\Plugin\Action;
/**
* Merge keyword action.
*
* @Action(
* id = "bibcite_entity_keyword_merge",
* label = @Translation("Merge keyword"),
* type = "bibcite_keyword",
* confirm_form_route_name = "entity.bibcite_keyword.bibcite_merge_multiple_form",
* )
*/
class KeywordMerge extends EntityMergeBase {
}
<?php
namespace Drupal\bibcite_entity\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides dynamic tabs based on available formats.
*/
class MergeLocalTask extends DeriverBase implements ContainerDeriverInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* Constructs a new DynamicLocalTasks.
*
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManager $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
/* @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type->hasLinkTemplate('bibcite-merge-form')) {
$this->derivatives["entity.$entity_type_id.bibcite_merge_form"] = [
'route_name' => "entity.$entity_type_id.bibcite_merge_form",
'base_route' => "entity.$entity_type_id.canonical",
] + $base_plugin_definition;
}
}
return $this->derivatives;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment