You are here

public static function Terms::loadByName in Hook Update Deploy Tools 7

Load a term by name.

Parameters

string $term_name: The human readable name of a Term.

string $vocabulary_name: The human readable or machine name of a Vocabulary.

bool $strict: Flag to cause exception to be thrown if not able to load.

Return value

mixed (object) term object if found. (bool) FALSE.

Throws

HudtException In the event of something that fails the import.

3 calls to Terms::loadByName()
Terms::delete in src/Terms.php
Delete a Term.
Terms::importOne in src/Terms.php
Validated Updates/Imports one term from the contents of an import file.
Terms::unpackParents in src/Terms.php
Unpacks import ->parents and creates ->parent for importing any parents.

File

src/Terms.php, line 462

Class

Terms
Public methods for dealing with Vocabularies.

Namespace

HookUpdateDeployTools

Code

public static function loadByName($term_name, $vocabulary_name, $strict = FALSE) {
  Check::canUse('taxonomy');
  Check::canCall('taxonomy_get_term_by_name');
  $variables = array(
    '@name' => $term_name,
    '@vocab' => $vocabulary_name,
  );

  // Convert vocabulary name to machine name.
  $vocabulary = Vocabularies::loadByName($vocabulary_name, FALSE);
  $vocabulary = !empty($vocabulary) ? $vocabulary : Vocabularies::loadByMachineName($vocabulary_name, FALSE);
  if (empty($vocabulary)) {

    // The Vocabulary was not found, throw exception, call this a failure.
    // Even if not $strict this should throw an exception because you need a
    // vocabulary to look for a term.,
    $message = "There is no Vocabulary '@vocab'. It could not be loaded.";
    throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
  }
  $term = array_shift(taxonomy_get_term_by_name($term_name, $vocabulary->machine_name));
  if (!empty($term)) {
    return $term;
  }
  else {
    if ($strict) {

      // The Term was not found, throw exception, call this a failure.
      $message = "There is no Term '@name'in @vocab. It could not be loaded.";
      throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
    }
    else {
      return FALSE;
    }
  }
}