You are here

public function DomainPathHelper::deleteEntityDomainPaths in Domain Path 8

Helper function for deleting domain paths from an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

File

src/DomainPathHelper.php, line 407

Class

DomainPathHelper

Namespace

Drupal\domain_path

Code

public function deleteEntityDomainPaths(EntityInterface $entity, $delete_all_translations = FALSE) {
  if ($this
    ->domainPathsIsEnabled($entity)) {
    $properties_map = [
      'source' => '/' . $entity
        ->toUrl()
        ->getInternalPath(),
    ];
    if (!$delete_all_translations) {
      $properties_map['language'] = $entity
        ->language()
        ->getId();
    }
    $domain_paths = $this->entityTypeManager
      ->getStorage('domain_path')
      ->loadByProperties($properties_map);
    if ($domain_paths) {
      foreach ($domain_paths as $domain_path) {
        $domain_path
          ->delete();
      }
    }
  }

  // Delete domain paths on domain delete.
  if ($entity instanceof DomainInterface) {
    $domain_paths = $this->entityTypeManager
      ->getStorage('domain_path')
      ->loadByProperties([
      'domain_id' => $entity
        ->id(),
    ]);
    if ($domain_paths) {
      foreach ($domain_paths as $domain_path) {
        $domain_path
          ->delete();
      }
    }
  }
}