You are here

trait QuickTermTrait in farmOS 2.x

Provides methods for working with terms.

Hierarchy

1 file declares its use of QuickTermTrait
Test.php in modules/core/quick/tests/modules/farm_quick_test/src/Plugin/QuickForm/Test.php

File

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

Namespace

Drupal\farm_quick\Traits
View 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

Namesort descending Modifiers Type Description Overrides
QuickTermTrait::createOrLoadTerm public function Given a term name, create or load a matching term entity.
QuickTermTrait::createTerm public function Create a term.