You are here

public function MenuHierarchySubscriber::serializeMenuParent in YAML Content 8

Convert loaded parent entities for menu items to an expected value format.

Parameters

\Drupal\yaml_content\Event\EntityPreSaveEvent $event: The entity pre-save event containing data being processed.

File

src/EventSubscriber/MenuHierarchySubscriber.php, line 36

Class

MenuHierarchySubscriber
An event subscriber to correct link parent reference formatting.

Namespace

Drupal\yaml_content\EventSubscriber

Code

public function serializeMenuParent(EntityPreSaveEvent $event) {
  $import_content = $event
    ->getContentData();

  // Stop here if we're not working with a menu link.
  if (empty($import_content['entity']) || $import_content['entity'] != 'menu_link_content') {
    return;
  }

  // Stop here if there is no parent link indicated in content.
  if (empty($import_content['parent'])) {
    return;
  }

  /** @var \Drupal\Core\Menu\MenuLinkInterface $entity */
  $entity = $event
    ->getEntity();
  $parent_value = $entity->parent
    ->first()->value;
  if ($parent_value instanceof MenuLinkContentInterface) {
    $entity
      ->set('parent', $parent_value
      ->getPluginId(), FALSE);
  }
}