Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292566

More efficiently check if Layout Builder is enabled for display mode

$
0
0

I am attempting to set a Twig variable in a node preprocess hook holding whether Layout Builder is enabled for the given view display.

A few things are confusing to me. When checking the $vars['view_mode'] variable, it shows as "full" even though the "Full content" display is not enabled within the "Custom display settings" on the content type.

After a bit of digging, the node is using the "default" display even though it believes it is using the "full" display.

If I were to attempt to load the "full" display when not enabled through "Custom display settings," it returns null.

Below is the most succinct snippet I could whip up to mitigate the issue and get my desired result.

Is there an easier or more Drupal way to check if Layout Builder is enabled for the current display?

function hook_preprocess_node(array &$variables) {
  /** @var \Drupal\node\NodeInterface $node */
  $node = $variables['node'];

  /** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $view_display_storage = $entity_type_manager
    ->getStorage('entity_view_display');

  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay|null $view_display */
  $view_display = $view_display_storage
    ->load('node.' . $node->getType() . '.' . $variables['view_mode']);

  if ($view_display === NULL) {
    $view_display = $view_display_storage
      ->load('node.' . $node->getType() . '.default');
  }

  $variables['layout_builder_enabled'] = $view_display
    ->getThirdPartySetting('layout_builder', 'enabled');
}

Viewing all articles
Browse latest Browse all 292566

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>