You are here

ContributorCategoryDeleteForm.php in Bibliography & Citation 2.0.x

Same filename and directory in other branches
  1. 8 modules/bibcite_entity/src/Form/ContributorCategoryDeleteForm.php

File

modules/bibcite_entity/src/Form/ContributorCategoryDeleteForm.php
View source
<?php

namespace Drupal\bibcite_entity\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Builds the form to delete Contributor category entities.
 */
class ContributorCategoryDeleteForm extends EntityConfirmFormBase {

  /**
   * If author of this category is in reference entity.
   *
   * @var bool
   */
  private $inUse = FALSE;

  /**
   * Find if author of this category is in reference entity.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  private function checkInReference() {
    $storage = $this->entityTypeManager
      ->getStorage('bibcite_reference');
    $query = $storage
      ->getQuery();
    return !empty($query
      ->condition('author.category', $this->entity
      ->id())
      ->range(0, 1)
      ->execute());
  }

  /**
   * {@inheritdoc}
   */
  public function init(FormStateInterface $form_state) {
    parent::init($form_state);
    $this->inUse = $this
      ->checkInReference();
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    if (!$this->inUse) {
      return $this
        ->t('Are you sure you want to delete %name?', [
        '%name' => $this->entity
          ->label(),
      ]);
    }
    else {
      return $this
        ->t('Category %name used in reference(s) and cannot be deleted.', [
        '%name' => $this->entity
          ->label(),
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.bibcite_contributor_category.collection');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity
      ->delete();
    $this
      ->messenger()
      ->addStatus($this
      ->t('content @type: deleted @label.', [
      '@type' => $this->entity
        ->bundle(),
      '@label' => $this->entity
        ->label(),
    ]));
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($this->inUse) {
      $form_state
        ->setError($form, $this
        ->t('Category %name used in reference(s) and cannot be deleted.', [
        '%name' => $this->entity
          ->label(),
      ]));
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    $actions['submit']['#disabled'] = $this->inUse;
    return $actions;
  }

}

Classes

Namesort descending Description
ContributorCategoryDeleteForm Builds the form to delete Contributor category entities.