trait QuickTermTrait in farmOS 2.x
Provides methods for working with terms.
Hierarchy
- trait \Drupal\farm_quick\Traits\QuickTermTrait
1 file declares its use of QuickTermTrait
File
- modules/
core/ quick/ src/ Traits/ QuickTermTrait.php, line 10
Namespace
Drupal\farm_quick\TraitsView source
trait QuickTermTrait {
/**
* Create a term.
*
* @param array $values
* An array of values to initialize the term with.
*
* @return \Drupal\taxonomy\TermInterface
* The term entity that was created.
*/
public function createTerm(array $values = []) {
// Alias 'vocabulary' to 'vid'.
if (!empty($values['vocabulary'])) {
$values['vid'] = $values['vocabulary'];
}
// Start a new term entity with the provided values.
/** @var \Drupal\taxonomy\TermInterface $term */
$term = Term::create($values);
// Save the term.
$term
->save();
// Return the term entity.
return $term;
}
/**
* Given a term name, create or load a matching term entity.
*
* @param string $name
* The term name.
* @param string $vocabulary
* The vocabulary to search or create in.
*
* @return \Drupal\taxonomy\TermInterface
* The term entity that was created or loaded.
*/
public function createOrLoadTerm(string $name, string $vocabulary) {
// First try to load an existing term.
$search = taxonomy_term_load_multiple_by_name($name, $vocabulary);
if (!empty($search)) {
return reset($search);
}
// Create a new term.
return $this
->createTerm([
'name' => $name,
'vid' => $vocabulary,
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
QuickTermTrait:: |
public | function | Given a term name, create or load a matching term entity. | |
QuickTermTrait:: |
public | function | Create a term. |