You are here

public function MediaDirectoriesUiHelper::termIsAChildOf in Media Directories 2.x

Checks if a term belongs to the specified parent.

Parameters

\Drupal\taxonomy\entity\Term $term: The term to check against.

\Drupal\taxonomy\entity\Term $parent|null: The parent to search for, or NULL if the parent is <ROOT>.

Return value

boolean Wheater the provided parent is a parent.

File

modules/media_directories_ui/src/MediaDirectoriesUiHelper.php, line 155

Class

MediaDirectoriesUiHelper
Media directories UI helper service.

Namespace

Drupal\media_directories_ui

Code

public function termIsAChildOf(Term $term, $parent) {
  if ($term === NULL) {
    return FALSE;
  }

  /** @var \Drupal\taxonomy\Entity\Term[] $types */
  $parents = $this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadParents($term
    ->id());
  if ($parent === NULL) {
    if (count($parents) == 0) {

      // See https://www.drupal.org/node/2019905
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
  foreach ($parents as $one_of_the_parents) {
    if ($parent
      ->id() == $one_of_the_parents
      ->id()) {
      return TRUE;
    }
  }
  return FALSE;
}