You are here

class FormAlter in Entity Reference Integrity 8

Alter entity delete forms to provide some warning deletes will fail.

Hierarchy

Expanded class hierarchy of FormAlter

1 file declares its use of FormAlter
entity_reference_integrity_enforce.module in modules/entity_reference_integrity_enforce/entity_reference_integrity_enforce.module
Module file.

File

modules/entity_reference_integrity_enforce/src/FormAlter.php, line 15

Namespace

Drupal\entity_reference_integrity_enforce
View source
class FormAlter implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The dependency manager.
   *
   * @var \Drupal\entity_reference_integrity\EntityReferenceDependencyManagerInterface
   */
  protected $dependencyManager;

  /**
   * The entity type IDs protection is enabled for.
   *
   * @var array
   */
  protected $enabledEntityTypeIds;

  /**
   * Create a DeleteFormAlter object.
   */
  public function __construct(EntityReferenceDependencyManagerInterface $calculator, $enabled_entity_type_ids) {
    $this->dependencyManager = $calculator;
    $this->enabledEntityTypeIds = $enabled_entity_type_ids;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_reference_integrity.dependency_manager'), $container
      ->get('config.factory')
      ->get('entity_reference_integrity_enforce.settings')
      ->get('enabled_entity_type_ids'));
  }

  /**
   * Implements hook_form_alter().
   */
  public function formAlter(&$form, FormStateInterface $form_state, $form_id) {

    /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
    $form_object = $form_state
      ->getFormObject();
    if (!$this
      ->isDeleteForm($form_object)) {
      return;
    }
    $entity = $form_object
      ->getEntity();
    if (in_array($entity
      ->getEntityTypeId(), $this->enabledEntityTypeIds, TRUE) && $this->dependencyManager
      ->hasDependents($entity)) {
      $referencing_entities = $this->dependencyManager
        ->getDependentEntities($entity);
      unset($form['actions']['submit']);
      unset($form['description']);
      $form['referencing_entities_list'] = [
        '#weight' => -10,
        'explanation' => [
          '#prefix' => '<div  class="messages messages--error">',
          '#markup' => $this
            ->t('You can not delete this as it is being referenced by another entity.'),
          '#suffix' => '</div>',
        ],
        'entities' => $this
          ->buildReferencingEntitiesList($referencing_entities),
        '#suffix' => '<br/>',
      ];
    }
  }

  /**
   * Build a UI for listing the referencing entities.
   *
   * @param array $referencing_entities
   *   An array of referencing entities.
   *
   * @return array
   *   A renderable array of referencing entities.
   */
  protected function buildReferencingEntitiesList(array $referencing_entities) {
    $build = [];

    /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
    foreach ($referencing_entities as $entity_type_id => $entities) {
      $build[$entity_type_id]['label'] = [
        '#type' => 'html_tag',
        '#tag' => 'strong',
        '#value' => reset($entities)
          ->getEntityType()
          ->getLabel(),
      ];
      $build[$entity_type_id]['list'] = [
        '#theme' => 'item_list',
        '#items' => [],
      ];
      foreach ($entities as $entity) {
        $build[$entity_type_id]['list']['#items'][] = $entity
          ->hasLinkTemplate('canonical') ? $entity
          ->toLink() : $entity
          ->label();
      }
    }
    return $build;
  }

  /**
   * Check if a given generic form is applicable to be altered.
   *
   * @param mixed $form_object
   *   The form object.
   *
   * @return bool
   *   If alteration applies.
   */
  protected function isDeleteForm($form_object) {
    return $form_object instanceof EntityFormInterface && $form_object
      ->getOperation() === 'delete';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormAlter::$dependencyManager protected property The dependency manager.
FormAlter::$enabledEntityTypeIds protected property The entity type IDs protection is enabled for.
FormAlter::buildReferencingEntitiesList protected function Build a UI for listing the referencing entities.
FormAlter::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FormAlter::formAlter public function Implements hook_form_alter().
FormAlter::isDeleteForm protected function Check if a given generic form is applicable to be altered.
FormAlter::__construct public function Create a DeleteFormAlter object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.