You are here

class LinkFieldUnserializer in Acquia Content Hub 8.2

Link Field Unserializer.

This class handles the unserialization of menu_link entities.

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField\LinkFieldUnserializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LinkFieldUnserializer

1 string reference to 'LinkFieldUnserializer'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses LinkFieldUnserializer
menu_link.field.cdf.unserializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField\LinkFieldUnserializer

File

src/EventSubscriber/UnserializeContentField/LinkFieldUnserializer.php, line 14

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField
View source
class LinkFieldUnserializer implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::UNSERIALIZE_CONTENT_ENTITY_FIELD] = [
      'onUnserializeContentField',
      10,
    ];
    return $events;
  }

  /**
   * On unserialize field event function.
   *
   * Handles the unserialization of menu_link entities.
   *
   * @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
   *   The unserialize event.
   */
  public function onUnserializeContentField(UnserializeCdfEntityFieldEvent $event) {

    // Get field meta data.
    $meta = $event
      ->getFieldMetadata();

    // Make sure the field type is link.
    if ($meta['type'] !== 'link') {
      return;
    }

    // Get field and init values array to set later.
    $field = $event
      ->getField();
    $values = [];

    // Return early if no attr values are set.
    if (empty($field['value'])) {
      return;
    }

    // Loop through field values.
    foreach ($field['value'] as $langcode => $fieldValues) {
      foreach ($fieldValues as $value) {
        if ($value['uri_type'] === 'entity') {

          // Get the entity from event stack.
          $uuid = $value['uri'];
          $uri_entity = $event
            ->getStack()
            ->getDependency($uuid)
            ->getEntity();

          // Construct the entity link.
          // Format: entity:<ENT_TYPE>/<ENT_ID>.
          $entity_link = "entity:{$uri_entity->getEntityTypeId()}/{$uri_entity->id()}";

          // Set entity link as target.
          $value['uri'] = $entity_link;
        }
        if ($value['uri_type'] === 'internal') {
          $internal_type = array_key_exists('internal_type', $value) ? $value['internal_type'] : '';
          if ($internal_type === 'internal_entity') {
            $uuid = $value['uri'];
            $uri_entity = $event
              ->getStack()
              ->getDependency($uuid)
              ->getEntity();

            // Construct the internal link.
            // Format: internal:/<ENT_TYPE>/<ENT_ID>.
            if ($uri_entity
              ->getEntityType()
              ->hasLinkTemplate('canonical') && $uri_entity
              ->toUrl('canonical')
              ->isRouted()) {
              $internal_link = "internal:/" . $uri_entity
                ->toUrl('canonical')
                ->getInternalPath();
            }
            else {
              $internal_link = "internal:/{$uri_entity->getEntityTypeId()}/{$uri_entity->id()}";
            }
            $value['uri'] = $internal_link;
          }
          if (!empty($internal_type)) {
            unset($value['internal_type']);
          }
        }
        unset($value['uri_type']);
        $values[$langcode][$event
          ->getFieldName()][] = $value;
      }
    }

    // Set updated event values.
    $event
      ->setValue($values);
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkFieldUnserializer::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
LinkFieldUnserializer::onUnserializeContentField public function On unserialize field event function.