You are here

class MergeConfirmForm in Bibliography & Citation 2.0.x

Same name and namespace in other branches
  1. 8 modules/bibcite_entity/src/Form/MergeConfirmForm.php \Drupal\bibcite_entity\Form\MergeConfirmForm

Confirm merge of bibliographic entities.

Hierarchy

Expanded class hierarchy of MergeConfirmForm

1 string reference to 'MergeConfirmForm'
bibcite_entity.routing.yml in modules/bibcite_entity/bibcite_entity.routing.yml
modules/bibcite_entity/bibcite_entity.routing.yml

File

modules/bibcite_entity/src/Form/MergeConfirmForm.php, line 14

Namespace

Drupal\bibcite_entity\Form
View source
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,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 2
ConfirmFormBase::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormInterface::getConfirmText 22
ConfirmFormBase::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormInterface::getDescription 14
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MergeConfirmForm::$fieldName protected property The field name for filtering.
MergeConfirmForm::$source protected property This entity will be merged to target.
MergeConfirmForm::$target protected property Source entity will be merged to this one.
MergeConfirmForm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
MergeConfirmForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
MergeConfirmForm::getAuthoredReferencesStatistic private function Find references and get statistic data.
MergeConfirmForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
MergeConfirmForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MergeConfirmForm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
MergeConfirmForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
MergeConfirmForm::__construct public function
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.