More efficiently check if Layout Builder is enabled for display mode

Created on 7 February 2024, 24 days ago
Updated 26 February 2024, 5 days ago

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 $variables['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->bundle() . '.' . $variables['view_mode']);

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

  $variables['layout_builder_enabled'] = $view_display
    ->getThirdPartySetting('layout_builder', 'enabled');
}
πŸ’¬ Support request
Status

Fixed

Version

10.2 ✨

Component
Layout builderΒ  β†’

Last updated 23 minutes ago

Created by

πŸ‡ΊπŸ‡ΈUnited States FrankieD3

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @FrankieD3
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    You can use the entity display repository service

  • Status changed to Fixed 19 days ago
  • πŸ‡ΊπŸ‡ΈUnited States FrankieD3

    Apologies for the late reply.

    This was the solution! Thanks so much for your help.

    Below is my refactored snippet.

    function hook_preprocess_node(array &$variables) {
      /** @var \Drupal\node\NodeInterface $node */
      $node = $variables['node'];
    
      /** @var \Drupal\Core\Entity\EntityDisplayRepository entity_display_repository */
      $entity_display_repository = \Drupal::service('entity_display.repository');
    
      $view_display = $entity_display_repository
        ->getViewDisplay($node->getEntityTypeId(), $node->bundle());
    
      $variables['layout_builder_enabled'] = $view_display
        ->getThirdPartySetting('layout_builder', 'enabled');
    }
    
  • πŸ‡ΊπŸ‡ΈUnited States FrankieD3
  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    Was this added to Core?
    Is there a patch?
    Or is the solution in #3 a workaround?
    Without a patch shouldn't this be active?

    Should the workaround be incorporated into core in someway?

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    This was a support request

  • πŸ‡ΊπŸ‡ΈUnited States FrankieD3

    I hadn't thought about it at the time, but #5 raises a good point.

    Is the discrepancy between the view mode available at $variables['view_mode'] and the real currently-active view mode worth raising Pan issue in core?

    Or is this perhaps by design, and I am misunderstanding the variable's intention?

Production build https://api.contrib.social 0.61.6-2-g546bc20