View source
<?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 Symfony\Component\DependencyInjection\ContainerInterface;
class MergeConfirmForm extends ConfirmFormBase {
protected $source;
protected $target;
protected $fieldName;
public static function create(ContainerInterface $container) {
return new static($container
->get('current_route_match'));
}
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");
}
public function getFormId() {
return 'bibcite_entity_merge_confirm';
}
public function getQuestion() {
return $this
->t('Are you sure you want to merge @source to @target?', [
'@source' => $this->source
->label(),
'@target' => $this->target
->label(),
]);
}
public function getCancelUrl() {
return $this->source
->toUrl('bibcite-merge-form');
}
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);
}
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);
}
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,
];
}
}