Skip to content

Commit a252903

Browse files
committed
DD-528 added missing method (copied from site level implementation)
1 parent 6bdb9c8 commit a252903

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/Context/ParagraphsContext.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,38 @@ public function addTheFollowingParagraphsToFieldOnParagraph($field_name, $paragr
344344
}
345345
}
346346
}
347+
348+
/**
349+
* Load/Create taxonomy term from string (vocabulary:term_name)
350+
* @param $value
351+
* @return bool|int|mixed
352+
* @throws Exception
353+
*/
354+
public function assertTerm($value) {
355+
$term = FALSE;
356+
if (strpos($value, ':') === FALSE) {
357+
throw new Exception(printf('Unknown vocabulary in value: %s', $value));
358+
}
359+
360+
$values = explode(':', $value);
361+
$name = trim($values[1]);
362+
$vocab = trim($values[0]);
363+
if ($terms = taxonomy_term_load_multiple_by_name($name, $vocab)) {
364+
$term = reset($terms);
365+
}
366+
367+
// Create term if not found
368+
if (empty($term)) {
369+
// Sadly DrupalContext->createTerm doesn't return the newly created term, so do it ourselves
370+
$term = (object) array(
371+
'name' => $name,
372+
'vocabulary_machine_name' => $vocab,
373+
'description' => ''
374+
);
375+
$term_object = $this->termCreate($term);
376+
return $term_object->tid;
377+
}
378+
379+
return $term->id();
380+
}
347381
}

0 commit comments

Comments
 (0)