View source
<?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\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MergeMultipleForm extends ConfirmFormBase {
protected $entityInfo = [];
protected $tempStore;
protected $entityTypeManager;
protected $currentUser;
protected $entityType;
protected $fieldName;
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;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'), $container
->get('current_user'));
}
public function getFormId() {
return 'bibcite_entity_merge_multiple';
}
public function getQuestion() {
return $this
->t('Are you sure you want to merge these @entity_type_label?', [
'@entity_type_label' => $this->entityType
->getPluralLabel(),
]);
}
public function getConfirmText() {
return $this
->t('Merge');
}
public function getCancelUrl() {
return new Url("entity.{$this->entityType->id()}.collection");
}
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);
}
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')],
]));
}
}
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());
}
}