FormAlter.php in Entity Reference Integrity 8
File
modules/entity_reference_integrity_enforce/src/FormAlter.php
View source
<?php
namespace Drupal\entity_reference_integrity_enforce;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\entity_reference_integrity\EntityReferenceDependencyManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FormAlter implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $dependencyManager;
protected $enabledEntityTypeIds;
public function __construct(EntityReferenceDependencyManagerInterface $calculator, $enabled_entity_type_ids) {
$this->dependencyManager = $calculator;
$this->enabledEntityTypeIds = $enabled_entity_type_ids;
}
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'));
}
public function formAlter(&$form, FormStateInterface $form_state, $form_id) {
$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/>',
];
}
}
protected function buildReferencingEntitiesList(array $referencing_entities) {
$build = [];
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;
}
protected function isDeleteForm($form_object) {
return $form_object instanceof EntityFormInterface && $form_object
->getOperation() === 'delete';
}
}
Classes
Name |
Description |
FormAlter |
Alter entity delete forms to provide some warning deletes will fail. |