You are here

class EntityPredelete in Entity Reference Integrity 8

Hook into entity deletes and throw an exception to prevent them disappearing.

Hierarchy

Expanded class hierarchy of EntityPredelete

1 file declares its use of EntityPredelete
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/EntityPredelete.php, line 14

Namespace

Drupal\entity_reference_integrity_enforce
View source
class EntityPredelete implements ContainerInjectionInterface {

  /**
   * 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, array $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_entity_delete().
   */
  public function entityDelete(EntityInterface $entity) {
    if (in_array($entity
      ->getEntityTypeId(), $this->enabledEntityTypeIds, TRUE) && $this->dependencyManager
      ->hasDependents($entity)) {
      throw new ProtectedEntityException(sprintf('Cannot delete "%s" of type "%s" with label "%s" and ID "%s" because other content is referencing it and the integrity of this entity type is enforced.', $entity
        ->getEntityTypeId(), $entity
        ->bundle(), $entity
        ->label(), $entity
        ->id()));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPredelete::$dependencyManager protected property The dependency manager.
EntityPredelete::$enabledEntityTypeIds protected property The entity type IDs protection is enabled for.
EntityPredelete::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityPredelete::entityDelete public function Implements hook_entity_delete().
EntityPredelete::__construct public function Create a DeleteFormAlter object.