You are here

protected function ContentProcessor::processChoiceCheckboxField in GatherContent 8.4

Processing function for checkbox 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::processChoiceCheckboxField()
ContentProcessor::processContentPane in src/Import/ContentProcess/ContentProcessor.php
Processing function for content panes.

File

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

Class

ContentProcessor
The ContentProcessor sets the necessary fields of the entity.

Namespace

Drupal\gathercontent\Import\ContentProcess

Code

protected function processChoiceCheckboxField(EntityInterface &$entity, FieldConfig $field_info, $is_translatable, $language, array $options) {
  $local_field_name = $field_info
    ->getName();
  $entity->{$local_field_name} = [
    NULL,
  ];
  $selected_options = [];
  foreach ($options as $option) {
    if ($option['selected']) {
      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);
        }
        $taxonomy = \Drupal::entityTypeManager()
          ->getStorage('taxonomy_term')
          ->loadByProperties([
          'gathercontent_option_ids' => $option['name'],
          'vid' => $vid,
        ]);

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