You are here

protected function ImportExportTestBase::handleFieldValues in Acquia Content Hub 8.2

Handle custom field types to more accurately match expectations.

Parameters

\Drupal\Core\Field\FieldItemListInterface $field: The field being handled.

Return value

array|mixed Field value.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to ImportExportTestBase::handleFieldValues()
ImportExportTestBase::contentEntityImportExport in tests/src/Kernel/ImportExportTestBase.php
Import and export content.
PreExistingTermImportExportTest::testTermImportExport in tests/src/Kernel/PreExistingTermImportExportTest.php
Performs taxonomy terms import and runs assertions.

File

tests/src/Kernel/ImportExportTestBase.php, line 510

Class

ImportExportTestBase
Base for testing exports and imports.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

protected function handleFieldValues(FieldItemListInterface $field) {
  $values = $field
    ->getValue();
  if (in_array($field
    ->getFieldDefinition()
    ->getType(), self::ENTITY_REFERENCE_TYPES) && $values) {
    $values = [];
    foreach ($field as $item_delta => $item) {
      if ($item
        ->getValue()['target_id']) {
        $values[$item_delta]['target_id'] = $item->entity
          ->uuid();
      }
    }
  }
  if ($field
    ->getFieldDefinition()
    ->getType() === self::ENTITY_REFERENCE_IMAGE_TYPE && $values) {
    $values = [];
    foreach ($field as $item_delta => $item) {
      if ($item
        ->getValue()['target_id']) {
        $values[$item_delta] = $item
          ->getValue();
        $values[$item_delta]['target_id'] = $item->entity
          ->uuid();
      }
    }
  }
  if ($field
    ->getFieldDefinition()
    ->getType() === 'link') {
    foreach ($field as $item_delta => $item) {
      [
        $uri_type,
        $uri,
      ] = explode(':', $item
        ->getValue()['uri']);
      if ($uri_type === 'entity') {
        [
          $item_entity_type,
          $item_entity_id,
        ] = explode('/', $uri);
        $uri_entity = $this->entityTypeManager
          ->getStorage($item_entity_type)
          ->load($item_entity_id);
        $values[$item_delta]['uri'] = $uri_entity
          ->uuid();
      }
      elseif ($uri_type === 'internal') {
        $uri = ltrim($uri, '/');
        if (substr_count($uri, '/') > 0) {
          [
            $item_entity_type,
            $item_entity_id,
          ] = explode('/', $uri, 2);
          try {
            $uri_storage = $this->entityTypeManager
              ->getStorage($item_entity_type);
            $uri_entity = !is_null($uri_storage) ? $uri_storage
              ->load($item_entity_id) : NULL;
            if ($uri_entity) {
              $values[$item_delta]['uri'] = $uri_entity
                ->uuid();
            }
            else {
              $values[$item_delta]['uri'] = $item
                ->getValue()['uri'];
            }
          } catch (\Exception $e) {
            $values[$item_delta]['uri'] = $item
              ->getValue()['uri'];
          }
        }
        else {
          $values[$item_delta]['uri'] = $item
            ->getValue()['uri'];
        }
      }
      else {
        $values[$item_delta]['uri'] = $item
          ->getValue()['uri'];
      }
    }
  }
  return $values;
}