You are here

class MenuHierarchySubscriber in YAML Content 8

An event subscriber to correct link parent reference formatting.

Using entity references and processing for parent menu links within YAML results in the entity being loaded and assigned to the parent value, but the schema for menu content links expects this value to be a string instead. This event subscriber replaces the entity object with the appropriate reference string instead prior to saving.

Hierarchy

  • class \Drupal\yaml_content\EventSubscriber\MenuHierarchySubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MenuHierarchySubscriber

1 string reference to 'MenuHierarchySubscriber'
yaml_content.services.yml in ./yaml_content.services.yml
yaml_content.services.yml
1 service uses MenuHierarchySubscriber
yaml_content.event_subscriber.yaml_content_menu_hierarchy in ./yaml_content.services.yml
Drupal\yaml_content\EventSubscriber\MenuHierarchySubscriber

File

src/EventSubscriber/MenuHierarchySubscriber.php, line 19

Namespace

Drupal\yaml_content\EventSubscriber
View source
class MenuHierarchySubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[YamlContentEvents::ENTITY_PRE_SAVE][] = [
      'serializeMenuParent',
    ];
    return $events;
  }

  /**
   * Convert loaded parent entities for menu items to an expected value format.
   *
   * @param \Drupal\yaml_content\Event\EntityPreSaveEvent $event
   *   The entity pre-save event containing data being processed.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuHierarchySubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MenuHierarchySubscriber::serializeMenuParent public function Convert loaded parent entities for menu items to an expected value format.