There is a Page with id: front_page, and a number of variants.

I want to programmatically load the page, evaluate the context and render the appropriate variant.

I have the following code to load the page:

$page_id = 'featured_content';
$page_storage = \Drupal::entityTypeManager()->getStorage('page');
$variant_storage = \Drupal::entityTypeManager()->getStorage('page_variant');
$page = $page_storage->load($page_id);
// ...

How can I build or render the $page?


Here is how :

$page = \Drupal\page_manager\Entity\Page::load('featured_content');
//all variants ids, for the example
$variants = $page->getVariants();
$variantIds = array_keys($variants);
// last variant... could have been $page->getVariant('{VARIANT_ID}');
$pageVariant = end($variants);
$viewer = new \Drupal\page_manager\Entity\PageVariantViewBuilder();
$arr = $viewer->view($pageVariant);
  • This renders the page without the theme. How do you render it with the theme wrapper as well? – iamamused Nov 22 '18 at 19:18
  • Just return $arr on a controller method – Oulalahakabu Nov 23 '18 at 2:35

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.