You are here

private static function Terms::importOne in Hook Update Deploy Tools 7

Validated Updates/Imports one term from the contents of an import file.

Parameters

string $term_import: The term object to import.

string $vocabulary_name: The name of the vocabulary to import/update.

string $term_name: The name of the term to import/update

Return value

array Contains the elements page, operation, and edit_link.

Throws

HudtException In the event of something that fails the import.

1 call to Terms::importOne()
Terms::import in src/Terms.php
Perform the unique steps necessary to import terms items from export files.

File

src/Terms.php, line 286

Class

Terms
Public methods for dealing with Vocabularies.

Namespace

HookUpdateDeployTools

Code

private static function importOne($term_import, $vocabulary_name, $term_name) {

  // Determine if the term exists in that vocabulary.
  $term_existing = Terms::loadByName($term_name, $vocabulary_name);
  $msg_vars = array(
    '@vocabulary_name' => $vocabulary_name,
    '@term_name' => $term_name,
  );
  self::unpackParents($term_import);
  if (!empty($term_existing)) {

    // A term already exists.  Update it.
    $operation = t('Updated');
    $op = 'update';
    $saved_term = self::updateExistingTerm($term_import, $term_existing);
  }
  else {

    // No term exists in this Vocabulary.  Create It.
    $operation = t('Created');
    $op = 'create';
    $saved_term = self::createNewTerm($term_import);
  }
  $msg_vars['@operation'] = $operation;

  // Begin validation.
  // Case race.  First to evaluate TRUE wins.
  switch (TRUE) {
    case empty($saved_term->tid):

      // Save did not complete.  No tid granted.
      $message = '@operation of @language: @vocabulary_name : @vocabulary_term failed: The saved term ended up with no tid.';
      $valid = FALSE;
      break;
    case $saved_term->name !== $term_import->name:

      // Simple validation check to see if the saved title matches.
      $msg_vars['@intended_title'] = $term_import->title;
      $msg_vars['@saved_title'] = $saved_term->title;
      $message = '@operation failure: The term names do not match. Intended title: @intended_title  Saved Title: @saved_title';
      $valid = FALSE;
      break;

    // @TODO Consider other Term properties that could be validated without
    // leading to false negatives.
    default:

      // Passed all the validations, likely it is valid.
      $valid = TRUE;
  }
  if (!$valid) {

    // Validation failed so perform rollback.
    self::rollbackImport($op, $saved_term);
    throw new HudtException($message, $msg_vars, WATCHDOG_ERROR, TRUE);
  }
  $return = array(
    'term' => $saved_term,
    'operation' => "{$operation}: term/{$saved_term->tid}",
    'edit_link' => "taxonomy/term/{$saved_term->tid}/edit",
  );
  return $return;
}