You are here

public function ExifContent::handleTaxonomyField in Exif 8.2

Same name and namespace in other branches
  1. 8 src/ExifContent.php \Drupal\exif\ExifContent::handleTaxonomyField()

Handle taxonomy field.

Parameters

int $index: The index to set the new value.

\Drupal\Core\Field\FieldItemListInterface $field: The field to update.

string $exif_section: The exif section where value has been retrieved.

string $exif_name: The exif label where value has been retrieved.

string $exif_value: The exif value to update.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to ExifContent::handleTaxonomyField()
ExifContent::handleField in src/ExifContent.php
Handle field by delegating to specific type handler.

File

src/ExifContent.php, line 508

Class

ExifContent
Class ExifContent make link between drupal content and file content.

Namespace

Drupal\exif

Code

public function handleTaxonomyField($index, FieldItemListInterface &$field, $exif_section, $exif_name, $exif_value) {

  // Look for the term.
  if (!Unicode::validateUtf8($exif_value)) {
    $exif_value = Html::escape(utf8_encode($exif_value));
  }
  $config = Drupal::config('exif.settings');
  $chosen_vocabulary = array_keys($field
    ->getSettings('vocabulary')['handler_settings']['target_bundles'])[0];
  if (isset($chosen_vocabulary)) {
    $terms = taxonomy_term_load_multiple_by_name($exif_value, $chosen_vocabulary);
    if (is_array($terms) && count($terms) > 0) {
      $term = array_shift($terms);
    }
    else {

      // If not exist, create it and also parents if needed.
      $terms = taxonomy_term_load_multiple_by_name($exif_name, $chosen_vocabulary);
      if (is_array($terms) && count($terms) > 0) {
        $parent_term = array_shift($terms);
      }
      else {
        $terms = taxonomy_term_load_multiple_by_name($exif_section, $chosen_vocabulary);
        if (is_array($terms) && count($terms) > 0) {
          $section_term = array_shift($terms);
        }
        else {
          $section_term = $this
            ->createTerm($chosen_vocabulary, $exif_section);
        }
        $parent_term = $this
          ->createTerm($chosen_vocabulary, $exif_name, $section_term
          ->id());
      }
      $term = $this
        ->createTerm($chosen_vocabulary, $exif_value, $parent_term
        ->id());
    }
    $field
      ->offsetSet($index, $term
      ->id());
  }
}