Skip to content

Commit

Permalink
DD-528 added missing method (copied from site level implementation)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston committed Sep 6, 2018
1 parent 6bdb9c8 commit a252903
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Context/ParagraphsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,38 @@ public function addTheFollowingParagraphsToFieldOnParagraph($field_name, $paragr
}
}
}

/**
* Load/Create taxonomy term from string (vocabulary:term_name)
* @param $value
* @return bool|int|mixed
* @throws Exception
*/
public function assertTerm($value) {
$term = FALSE;
if (strpos($value, ':') === FALSE) {
throw new Exception(printf('Unknown vocabulary in value: %s', $value));
}

$values = explode(':', $value);
$name = trim($values[1]);
$vocab = trim($values[0]);
if ($terms = taxonomy_term_load_multiple_by_name($name, $vocab)) {
$term = reset($terms);
}

// Create term if not found
if (empty($term)) {
// Sadly DrupalContext->createTerm doesn't return the newly created term, so do it ourselves
$term = (object) array(
'name' => $name,
'vocabulary_machine_name' => $vocab,
'description' => ''
);
$term_object = $this->termCreate($term);
return $term_object->tid;
}

return $term->id();
}
}

0 comments on commit a252903

Please sign in to comment.