You are here

public function QuickTermTrait::createOrLoadTerm in farmOS 2.x

Given a term name, create or load a matching term entity.

Parameters

string $name: The term name.

string $vocabulary: The vocabulary to search or create in.

Return value

\Drupal\taxonomy\TermInterface The term entity that was created or loaded.

2 calls to QuickTermTrait::createOrLoadTerm()
QuickQuantityTrait::createQuantity in modules/core/quick/src/Traits/QuickQuantityTrait.php
Create a quantity.
Test::submitForm in modules/core/quick/tests/modules/farm_quick_test/src/Plugin/QuickForm/Test.php
Form submission handler.

File

modules/core/quick/src/Traits/QuickTermTrait.php, line 50

Class

QuickTermTrait
Provides methods for working with terms.

Namespace

Drupal\farm_quick\Traits

Code

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,
  ]);
}