You are here

protected function TaxonomyTermMatch::findTaxonomyTerm in Acquia Content Hub 8.2

Find local taxonomy term.

Parameters

string|null $label: Term label.

string|null $vocabulary: Vocabulary machine name.

string $parent: Term's parent UUID.

\Drupal\depcalc\DependencyStack $stack: The Dependency Stack.

Return value

\Drupal\taxonomy\Entity\Term|null Taxonomy term if exists, NULL otherwise.

Throws

\Exception

1 call to TaxonomyTermMatch::findTaxonomyTerm()
TaxonomyTermMatch::onLoadLocalEntity in modules/acquia_contenthub_subscriber/src/EventSubscriber/LoadLocalEntity/TaxonomyTermMatch.php
Load local terms with the same name, vocabulary and relative parent.

File

modules/acquia_contenthub_subscriber/src/EventSubscriber/LoadLocalEntity/TaxonomyTermMatch.php, line 160

Class

TaxonomyTermMatch
Class TaxonomyTermMatch.

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\LoadLocalEntity

Code

protected function findTaxonomyTerm(?string $label, ?string $vocabulary, string $parent, DependencyStack $stack) {
  if (!$label || !$vocabulary) {
    return NULL;
  }
  if (Uuid::isValid($parent)) {
    $parent_term = $stack
      ->getDependency($parent);

    // The stack should ALWAYS have a term representing the parent. This
    // could be a local or remote term, but the remote uuid should always
    // retrieve it.
    if (!$parent_term) {
      throw new \Exception(sprintf("Taxonomy term %s could not be found in the dependency stack during DataTamper.", $parent));
    }
    $parent = $parent_term
      ->getId();
  }
  $terms = $this
    ->getTermStorage()
    ->loadByProperties([
    'name' => $label,
    'vid' => $vocabulary,
    'parent' => $parent,
  ]);

  // No local terms were found that match our criteria. This is the normal
  // state of a new import.
  if ($parent && empty($terms)) {
    return NULL;
  }
  return array_shift($terms);
}