You are here

protected function ContentProcessor::processChoiceRadioField in GatherContent 8.4

Processing function for radio type of field.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Object of node.

\Drupal\field\Entity\FieldConfig $field_info: Local field Info object.

bool $is_translatable: Indicator if node is translatable.

string $language: Language of translation if applicable.

array $options: Array of options.

1 call to ContentProcessor::processChoiceRadioField()
ContentProcessor::processContentPane in src/Import/ContentProcess/ContentProcessor.php
Processing function for content panes.

File

src/Import/ContentProcess/ContentProcessor.php, line 584

Class

ContentProcessor
The ContentProcessor sets the necessary fields of the entity.

Namespace

Drupal\gathercontent\Import\ContentProcess

Code

protected function processChoiceRadioField(EntityInterface &$entity, FieldConfig $field_info, $is_translatable, $language, array $options) {
  $local_field_name = $field_info
    ->getName();
  foreach ($options as $option) {
    if (!$option['selected']) {
      continue;
    }
    if (isset($option['value'])) {
      if (empty($option['value'])) {
        continue;
      }

      // Dealing with "Other" option.
      if ($field_info
        ->getType() === 'entity_reference') {

        // Load vocabulary id.
        if (!empty($field_info
          ->getSetting('handler_settings')['auto_create_bundle'])) {
          $vid = $field_info
            ->getSetting('handler_settings')['auto_create_bundle'];
        }
        else {
          $handler_settings = $field_info
            ->getSetting('handler_settings');
          $handler_settings = reset($handler_settings);
          $vid = array_shift($handler_settings);
        }

        // Prepare confitions.
        $condition_array = [
          'name' => $option['value'],
          'vid' => $vid,
        ];
        if ($is_translatable && $language !== LanguageInterface::LANGCODE_NOT_SPECIFIED) {
          $condition_array['langcode'] = $language;
        }
        $terms = \Drupal::entityTypeManager()
          ->getStorage('taxonomy_term')
          ->loadByProperties($condition_array);

        /** @var \Drupal\taxonomy\Entity\Term $term */
        $term = array_shift($terms);
        if (empty($term)) {
          $term = Term::create([
            'vid' => $vid,
            'name' => $option['value'],
            'langcode' => $language,
          ]);
          $term
            ->save();
        }
        if ($is_translatable && $entity
          ->hasTranslation($language)) {
          $entity
            ->getTranslation($language)
            ->set($local_field_name, $term
            ->id());
        }
        else {
          $entity
            ->set($local_field_name, $term
            ->id());
        }
      }
      else {
        if ($is_translatable) {
          $entity
            ->getTranslation($language)->{$local_field_name}->value = $option['value'];
        }
        else {
          $entity->{$local_field_name}->value = $option['value'];
        }
      }
    }
    else {

      // Dealing with predefined options.
      if ($field_info
        ->getType() === 'entity_reference') {
        if (!empty($field_info
          ->getSetting('handler_settings')['auto_create_bundle'])) {
          $vid = $field_info
            ->getSetting('handler_settings')['auto_create_bundle'];
        }
        else {
          $handler_settings = $field_info
            ->getSetting('handler_settings');
          $handler_settings = reset($handler_settings);
          $vid = array_shift($handler_settings);
        }
        $terms = \Drupal::entityTypeManager()
          ->getStorage('taxonomy_term')
          ->loadByProperties([
          'gathercontent_option_ids' => $option['name'],
          'vid' => $vid,
        ]);

        /** @var \Drupal\taxonomy\Entity\Term $term */
        $term = array_shift($terms);
        if (!empty($term)) {
          if ($is_translatable) {
            $entity
              ->getTranslation($language)
              ->set($local_field_name, $term
              ->id());
          }
          else {
            $entity
              ->set($local_field_name, $term
              ->id());
          }
        }
      }
      else {
        if ($is_translatable) {
          $entity
            ->getTranslation($language)->{$local_field_name}->value = $option['name'];
        }
        else {
          $entity->{$local_field_name}->value = $option['name'];
        }
      }
    }
  }
}