You are here

public function Exporter::processFields in GatherContent 8.5

Processes field data.

Parameters

object $group: Group object.

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

array $mappingData: Mapping array.

bool $isTranslatable: Translatable.

string $language: Language.

Return value

array Returns data.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to Exporter::processFields()
Exporter::processGroups in gathercontent_upload/src/Export/Exporter.php
Manages the panes and changes the Item object values.

File

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

Class

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

Namespace

Drupal\gathercontent_upload\Export

Code

public function processFields(object $group, EntityInterface $entity, array $mappingData, bool $isTranslatable, string $language) {
  $exportedFields = [];
  $fields = [];
  $assets = [];
  foreach ($group->fields as $field) {

    // Skip field if it is not mapped.
    if (empty($mappingData[$group->uuid]['elements'][$field->uuid])) {
      continue;
    }
    $localFieldId = $mappingData[$group->uuid]['elements'][$field->uuid];
    if (isset($mappingData[$group->uuid]['type']) && $mappingData[$group->uuid]['type'] === 'content' || !isset($mappingData[$group->uuid]['type'])) {
      $localIdArray = explode('||', $localFieldId);

      /** @var \Drupal\field\Entity\FieldConfig $fieldInfo */
      $fieldInfo = FieldConfig::load($localIdArray[0]);
      $currentEntity = $entity;
      $type = '';
      $bundle = '';
      $titleField = $currentEntity
        ->getEntityTypeId() . '.' . $currentEntity
        ->bundle() . '.title';
      if ($localIdArray[0] === $titleField || $localIdArray[0] === 'title') {
        $currentFieldName = 'title';
      }
      else {
        $currentFieldName = $fieldInfo
          ->getName();
        $type = $fieldInfo
          ->getType();
        $bundle = $fieldInfo
          ->getTargetBundle();
      }

      // Get the deepest field's value, we need this to collect
      // the referenced entities values.
      $this
        ->processTargets($currentEntity, $currentFieldName, $type, $bundle, $exportedFields, $localIdArray, $isTranslatable, $language);
      $this->collectedReferenceRevisions[] = $currentEntity;
      $isRepeatable = FALSE;
      if ($fieldInfo) {
        $fieldType = $fieldInfo
          ->getType();

        // Field can be an entity reference.
        if (!in_array($fieldType, self::ALLOWED_MULTI_VALUE_TYPES)) {
          if (!empty($localIdArray[1])) {
            $fieldInfo = FieldConfig::load($localIdArray[1]);
          }
        }
        if ($fieldInfo) {
          $fieldType = $fieldInfo
            ->getType();
          $isMultiple = $fieldInfo
            ->getFieldStorageDefinition()
            ->isMultiple();
          $isGcFieldRepeatable = FALSE;
          if (property_exists($field, 'metadata')) {
            if (!empty($field->metadata) && property_exists($field->metadata, 'repeatable')) {
              $isGcFieldRepeatable = $field->metadata->repeatable->isRepeatable;
            }
          }
          if ($isMultiple && $isGcFieldRepeatable && in_array($fieldType, self::ALLOWED_MULTI_VALUE_TYPES)) {
            $isRepeatable = TRUE;
          }
        }
      }
      $value = $this
        ->processSetFields($field, $currentEntity, $isTranslatable, $language, $currentFieldName, $bundle, $isRepeatable);
      if (!empty($value)) {
        $fields[$field->uuid] = $value;
      }
      $asset = $this
        ->processSetAssets($field, $currentEntity, $isTranslatable, $language, $currentFieldName);
      if (!empty($asset)) {
        $assets[$field->uuid] = $asset;
      }
    }
    elseif ($mappingData[$group->uuid]['type'] === 'metatag') {
      if ($this->moduleHandler
        ->moduleExists('metatag') && $this->metatag
        ->checkMetatag($entity
        ->getEntityTypeId(), $entity
        ->bundle())) {
        $fields[$field->uuid] = $this
          ->processMetaTagFields($entity, $localFieldId, $isTranslatable, $language);
      }
    }
  }
  return [
    'content' => $fields,
    'assets' => $assets,
  ];
}