0

I'm attempting to be able to load a page variant given a specific path. My ultimate goal is to loop through all the links in a menu, find out what page_manager variant is being used and load the context data from that variant. I'm able to grab the data just fine if I manually supply a page id. Below is me obtaining the data from the about page I've created in page manager.

$page_storage = \Drupal::entityTypeManager()->getStorage('page');
$variant_storage = \Drupal::entityTypeManager()->getStorage('page_variant');
$about = $page_storage->load('about');
$about_variant = $about->getVariants();
$about_variant = reset($about_variant);
foreach($about_variant->getContexts() as $context){
    $label = $context->getContextDefinition()->getLabel();
    if($label == 'Icon'){
        var_dump($context->getContextValue());
    }
}

This works just fine. But what if I want to load the context data of each link in a menu? These are links that I can't anticipate. Also some of these links will be regular pages and some will be actual nodes that also have their own variant.

I can load the nodes from the route like so

$params = \Drupal\Core\Url::fromUserInput($path)->getRouteParameters();
if (isset($params['node'])) {
    $node = \Drupal\node\Entity\Node::load($params['node']);
    //We have a node which I don't know how to get the variant of
} else {
    //We have a regular page that I don't know obtain the id of
}

But even though I have the node I have no idea of a way to interface with it in a way that allows me to obtain the page variant I'm using. If it's a regular page, then as long as I have the page id of it I can load the page variant like I have done before, but I don't know how to obtain that.

So ultimately I suppose I need to be able to programmatically obtain two things

  • How to obtain the page id from a page given its path
  • How to see the page variant being used on a node

UPDATE: I did manage to obtain the page with this line

$page = \Drupal::entityTypeManager()->getStorage('page')->loadByProperties(['path' => $link]);

Still not sure how to grab the variant the node is using yet though.

1

In order to get the variant being used by the node I was able to retrieve the info inside the array returned by

$node->panelizer->getValue()

And to get the page I used

$page = \Drupal::entityTypeManager()->getStorage('page')->loadByProperties(['path' => $link]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.