You are here

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

Perform the unique steps necessary to import terms items from export files.

Parameters

string|array $vocabulary_term_names: The unique identifier(s) of the vocabulary terms to import, created from the "Vocabulary name|Term name" - use the human name not machine_name.

Overrides ImportInterface::import

File

src/Terms.php, line 17

Class

Terms
Public methods for dealing with Vocabularies.

Namespace

HookUpdateDeployTools

Code

public static function import($vocabulary_term_names) {
  $t = get_t();
  $completed = array();
  $vocabulary_term_names = (array) $vocabulary_term_names;
  $total_requested = count($vocabulary_term_names);
  try {
    Check::notEmpty('$vocabulary_term_names', $vocabulary_term_names);
    self::canImport();
    foreach ($vocabulary_term_names as $key => $vocabulary_term_name) {

      // Get the elements out of the vocabulary term name mashup.
      $vocabulary_name = self::getVocabularyName($vocabulary_term_name);
      $term_name = self::getTermName($vocabulary_term_name);
      $filename = self::normalizeFileName($vocabulary_term_name);

      // If the file is there, process it.
      if (HudtInternal::canReadFile($filename, 'term')) {

        // Read the file.
        $file_contents = HudtInternal::readFileToString($filename, 'term');
        eval('$term_import = ' . $file_contents . ';');
        if (!is_object($term_import)) {
          if (empty($errors)) {
            $errors = 'Term build error on eval().';
          }
          $message = 'Unable to get a term from the import. Errors: @errors';
          throw new HudtException($message, array(
            '@errors' => $errors,
          ), WATCHDOG_ERROR);
        }
        $error_msg = '';
        $result = self::importOne($term_import, $vocabulary_name, $term_name);

        // No Exceptions so far, so it must be a success.
        $message = '@operation: @vterm - successful.';
        global $base_url;
        $link = "{$base_url}/{$result['edit_link']}";
        $vars = array(
          '@operation' => $result['operation'],
          '@vterm' => "{$vocabulary_name}:{$term_name}",
        );
        Message::make($message, $vars, WATCHDOG_INFO, 1, $link);
        $completed["{$vocabulary_name}:{$term_name}"] = $result['operation'];
      }
    }
  } catch (\Exception $e) {
    $vars = array(
      '!error' => method_exists($e, 'logMessage') ? $e
        ->logMessage() : $e
        ->getMessage(),
    );
    if (!method_exists($e, 'logMessage')) {

      // Not logged yet, so log it.
      $message = 'Term import denied because: !error';
      Message::make($message, $vars, WATCHDOG_ERROR);
    }

    // Output a summary before shutting this down.
    $done = HudtInternal::getSummary($completed, $total_requested, 'Imported');
    Message::make($done, array(), FALSE, 1);
    throw new HudtException('Caught Exception: Update aborted!  !error', $vars, WATCHDOG_ERROR, FALSE);
  }
  $done = HudtInternal::getSummary($completed, $total_requested, 'Imported Terms');
  return $done;
}