You are here

public function ContentHubEntityLinkFieldHandler::denormalizeItem in Acquia Content Hub 8

Converts Entity UUIDs into IDs in a field value item.

Parameters

array $item: An array of a field value item with UUIDs.

Return value

array|null An array of a value item with IDs if dependency exists, NULL otherwise.

File

src/ContentHubEntityLinkFieldHandler.php, line 89

Class

ContentHubEntityLinkFieldHandler
Content Hub Entity Link Field.

Namespace

Drupal\acquia_contenthub

Code

public function denormalizeItem(array $item) {
  $entity_info = $this
    ->getDependentEntityInfo($item);
  if (!empty($entity_info)) {
    list($entity_type_id, $uuid) = $entity_info;
    if (strpos($item['uri'], 'entity:') !== FALSE) {
      $entities = \Drupal::entityTypeManager()
        ->getStorage($entity_type_id)
        ->loadByProperties([
        'uuid' => $uuid,
      ]);
      $entity = $entities ? reset($entities) : NULL;
    }
    elseif (strpos($item['uri'], 'internal:') !== FALSE) {
      $path = pathinfo($item['uri']);
      $entity_type_id = NULL;
      if (substr($path['dirname'], -4) == 'node') {
        $entity_type_id = 'node';
      }
      elseif (substr($path['dirname'], -13) == 'taxonomy/term') {
        $entity_type_id = 'taxonomy_term';
      }
      if ($entity_type_id) {
        $entities = \Drupal::entityTypeManager()
          ->getStorage($entity_type_id)
          ->loadByProperties([
          'uuid' => $uuid,
        ]);
        $entity = $entities ? reset($entities) : NULL;
      }
    }
    if ($entity) {
      $item['uri'] = str_replace($entity
        ->uuid(), $entity
        ->id(), $item['uri']);
    }
  }
  return $item;
}