You are here

public function Exporter::processSetFields in GatherContent 8.5

Set value of the field.

Parameters

object $field: Field object.

\Drupal\Core\Entity\EntityInterface $entity: Entity object.

bool $isTranslatable: Translatable bool.

string $language: Language string.

string $localFieldName: Field Name.

string $bundle: Local field Info bundle string.

bool $isRepeatable: Repeatable bool.

Return value

array|string Returns value.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to Exporter::processSetFields()
Exporter::processFields in gathercontent_upload/src/Export/Exporter.php
Processes field data.

File

gathercontent_upload/src/Export/Exporter.php, line 512

Class

Exporter
Class for handling import/update logic from GatherContent to Drupal.

Namespace

Drupal\gathercontent_upload\Export

Code

public function processSetFields(object $field, EntityInterface $entity, bool $isTranslatable, string $language, string $localFieldName, string $bundle, bool $isRepeatable) {
  $value = NULL;
  switch ($field->field_type) {
    case 'attachment':

      // Fetch file targets.
      if ($isTranslatable && $entity
        ->hasTranslation($language)) {
        $targets = $entity
          ->getTranslation($language)->{$localFieldName}
          ->getValue();
      }
      else {
        $targets = $entity->{$localFieldName}
          ->getValue();
      }
      $value = [];
      foreach ($targets as $target) {
        $file = $this->entityTypeManager
          ->getStorage('file')
          ->load($target['target_id']);
        if (empty($file) || $file
          ->get('gc_file_id')
          ->isEmpty()) {
          continue;
        }
        $value[] = $file
          ->get('gc_file_id')
          ->first()
          ->getValue()['value'];
      }
      break;
    case 'choice_radio':
    case 'choice_checkbox':

      // Fetch local selected option.
      if ($isTranslatable && $entity
        ->hasTranslation($language)) {
        $targets = $entity
          ->getTranslation($language)->{$localFieldName}
          ->getValue();
      }
      else {
        $targets = $entity->{$localFieldName}
          ->getValue();
      }
      $value = [];
      foreach ($targets as $target) {
        $conditionArray = [
          'tid' => $target['target_id'],
        ];
        if ($isTranslatable && $this->moduleHandler
          ->moduleExists('content_translation') && $this->contentTranslation
          ->isEnabled('taxonomy_term', $bundle) && $language !== LanguageInterface::LANGCODE_NOT_SPECIFIED) {
          $conditionArray['langcode'] = $language;
        }
        $terms = $this->entityTypeManager
          ->getStorage('taxonomy_term')
          ->loadByProperties($conditionArray);

        /** @var \Drupal\taxonomy\Entity\Term $term */
        $term = array_shift($terms);
        if (!empty($term)) {
          $optionIds = $term->gathercontent_option_ids
            ->getValue();
          $options = $field->metadata->choice_fields->options;
          foreach ($optionIds as $optionId) {
            if (!$this
              ->validOptionId($options, $optionId['value'])) {
              continue;
            }
            $value[] = [
              'id' => $optionId['value'],
            ];
          }
        }
      }
      break;
    case 'guidelines':

      // We don't upload this because this field shouldn't be
      // edited.
      break;
    default:
      if ($localFieldName === 'title') {
        if ($isTranslatable && $entity
          ->hasTranslation($language)) {
          $value = $entity
            ->getTranslation($language)
            ->getTitle();
        }
        else {
          $value = $entity
            ->getTitle();
        }
      }
      else {
        if ($isTranslatable && $entity
          ->hasTranslation($language)) {
          if ($isRepeatable) {
            $value = $this
              ->getRepeatableFieldValues($entity
              ->getTranslation($language)->{$localFieldName});
          }
          else {
            $value = $entity
              ->getTranslation($language)->{$localFieldName}->value;
          }
        }
        else {
          if ($isRepeatable) {
            $value = $this
              ->getRepeatableFieldValues($entity->{$localFieldName});
          }
          else {
            $value = $entity->{$localFieldName}->value;
          }
        }
      }
      break;
  }
  return $value;
}