You are here

public function MappingEditFormBase::automaticTermsGenerator in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 gathercontent_ui/src/Form/MappingEditFormBase.php \Drupal\gathercontent_ui\Form\MappingEditFormBase::automaticTermsGenerator()

Generate automatically terms for local field from GatherContent options.

Parameters

\Drupal\field\Entity\FieldConfig $handlerSettings: Field config for local field.

array $localOptions: Array of remote options.

string $langcode: The language of the generated term.

1 call to MappingEditFormBase::automaticTermsGenerator()
MappingEditForm::submitForm in gathercontent_ui/src/Form/MappingEditForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…

File

gathercontent_ui/src/Form/MappingEditFormBase.php, line 148

Class

MappingEditFormBase
Class MappingEditFormBase.

Namespace

Drupal\gathercontent_ui\Form

Code

public function automaticTermsGenerator(FieldConfig $handlerSettings, array $localOptions, $langcode) {
  $settings = $handlerSettings
    ->getSetting('handler_settings');

  /** @var \Drupal\taxonomy\Entity\Term[] $terms */
  if (!empty($settings['auto_create_bundle'])) {
    $vid = $settings['auto_create_bundle'];
  }
  else {
    $vid = reset($settings['target_bundles']);
  }

  // Check if field exists.
  $this
    ->gcOptionIdsFieldExists($vid);
  foreach ($localOptions as $id => $localOption) {
    $query = \Drupal::entityQuery('taxonomy_term');
    $group = $query
      ->orConditionGroup()
      ->condition('gathercontent_option_ids', $id)
      ->condition('name', $localOption);
    $term_ids = $query
      ->condition($group)
      ->condition('vid', $vid)
      ->condition('langcode', $langcode)
      ->execute();
    $term_id = array_shift($term_ids);
    if (!empty($term_id)) {
      $term = Term::load($term_id);
      if ($langcode === LanguageInterface::LANGCODE_NOT_SPECIFIED) {
        if ($term
          ->label() !== $localOption) {
          $term
            ->setName($localOption);
        }
        $values = $term
          ->get('gathercontent_option_ids')
          ->getValue();
        $mappedValues = array_map(function ($array) {
          return $array['value'];
        }, $values);
        if (!in_array($id, $mappedValues)) {
          $term->gathercontent_option_ids
            ->appendItem($id);
        }
      }
      else {
        if ($term
          ->getTranslation($langcode)
          ->label() !== $localOption) {
          $term
            ->getTranslation($langcode)
            ->setName($localOption);
        }
        $values = $term
          ->getTranslation($langcode)
          ->get('gathercontent_option_ids')
          ->getValue();
        $mappedValues = array_map(function ($array) {
          return $array['value'];
        }, $values);
        if (!in_array($id, $mappedValues)) {
          $term
            ->getTranslation($langcode)->gathercontent_option_ids
            ->appendItem($id);
        }
      }
      $term
        ->save();
      $this->erImported++;
    }
    else {
      $term_values = [
        'vid' => $vid,
        'langcode' => $langcode,
      ];
      $term = Term::create($term_values);
      $term
        ->setName($localOption);
      $term
        ->set('gathercontent_option_ids', $id);
      $term
        ->save();
      $this->erImported++;
    }
  }
}