You are here

public function Exporter::processTargets in GatherContent 8.5

Processes the target ids for a field.

Parameters

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

string $currentFieldName: Current field name.

string $type: Current type name.

string $bundle: Current bundle name.

array $exportedFields: Array of exported fields, preventing duplications.

array $localIdArray: Array of mapped embedded field id array.

bool $isTranslatable: Translatable.

string $language: Language.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

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

File

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

Class

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

Namespace

Drupal\gathercontent_upload\Export

Code

public function processTargets(EntityInterface &$currentEntity, string &$currentFieldName, string &$type, string &$bundle, array &$exportedFields, array $localIdArray, bool $isTranslatable, string $language) {
  $idCount = count($localIdArray);

  // Loop through the references, going deeper and deeper.
  for ($i = 0; $i < $idCount - 1; $i++) {
    $localId = $localIdArray[$i];
    $fieldInfo = FieldConfig::load($localId);
    $currentFieldName = $fieldInfo
      ->getName();
    $type = $fieldInfo
      ->getType();
    $bundle = $fieldInfo
      ->getTargetBundle();
    if ($isTranslatable && $currentEntity
      ->hasTranslation($language)) {
      $targetFieldValue = $currentEntity
        ->getTranslation($language)
        ->get($currentFieldName)
        ->getValue();
    }
    else {
      $targetFieldValue = $currentEntity
        ->get($currentFieldName)
        ->getValue();
    }

    // Load the targeted entity and process the data.
    if (!empty($targetFieldValue)) {
      $fieldTargetInfo = FieldConfig::load($localIdArray[$i + 1]);
      $entityStorage = $this->entityTypeManager
        ->getStorage($fieldTargetInfo
        ->getTargetEntityTypeId());
      $childFieldName = $fieldTargetInfo
        ->getName();
      $childType = $fieldInfo
        ->getType();
      $childBundle = $fieldInfo
        ->getTargetBundle();
      foreach ($targetFieldValue as $target) {
        $exportKey = $target['target_id'] . '_' . $childFieldName;

        // The field is already collected.
        if (!empty($exportedFields[$exportKey])) {
          continue;
        }
        $childEntity = $entityStorage
          ->loadByProperties([
          'id' => $target['target_id'],
          'type' => $fieldTargetInfo
            ->getTargetBundle(),
        ]);
        if (!empty($childEntity[$target['target_id']])) {
          $currentEntity = $childEntity[$target['target_id']];
          $currentFieldName = $childFieldName;
          $type = $childType;
          $bundle = $childBundle;
          if ($i == $idCount - 2) {
            $exportedFields[$exportKey] = TRUE;
          }
          break;
        }
      }
    }
  }
}