drowl_layouts_force_render not set when translating a layout paragraphed entity

Created on 26 June 2024, 4 months ago
Updated 10 July 2024, 3 months ago

Problem/Motivation

Until now, we're using a dirty workaround to detect we're in layout paragraphs builder (=edit) mode, by checking if the "lpBuilder" library is attached. This does not work when (synchronously) translating a node, because the library is not being attached if you can't change the paragraphs positions (Message: "You are in translation mode. You cannot add or remove items while translating. Reordering items will affect all languages.").

So our condition fails, and the indicator variable is not passed to the twig templates, which causes wrong conditions there.

/**
 * Implements template_preprocess_layout().
 */
function drowl_layouts_bs_preprocess_layout(&$variables) {
 [...]

  // Layout Paragraphs module support:
  if (\Drupal::service('module_handler')->moduleExists('layout_paragraphs')) {
    // Ensure each layout_paragraphs builder region is rendered, even if empty:
    // @see https://www.drupal.org/project/drowl_layouts_bs/issues/3294075
    if (!empty($variables['layout'])) {
      $defaultRegion = $variables['layout']->getDefaultRegion();
      $hasDefaultRegion = !empty($defaultRegion);
      $isLayoutParagraphsBuilder = $hasDefaultRegion && !empty($variables['content'][$defaultRegion]['#attached']['drupalSettings']['lpBuilder']);
      if ($isLayoutParagraphsBuilder) {
        // This is a layout paragraphs builder (edit mode).
        // Set a layout twig variable to implicate we need to force rendering
        // empty regions:
        $variables['drowl_layouts_bs_force_render'] = TRUE;
      }
    }
  }
}

Steps to reproduce

Translate a paragraph and see drowl_layouts_bs_force_render is not passed to the template.

Proposed resolution

We now use the following hook additionally to detect this situation in a cleaner way and pass the information we don't have available in the other hook:

/**
 * Implements template_preprocess_paragraph.
 */
function drowl_layouts_preprocess_paragraph(&$variables) {
  if (!empty($variables['elements']['#layout_paragraphs_component']) && !empty($variables['content']['regions'])) {
    // This is a layout paragraph component and layout:
    // We're in layout paragraphs builder mode!
    // Set indicator class to fprce the rendering of empty regions for the builder:
    $variables['content']['regions']['#drowl_layouts_force_render'] = TRUE;

    // Additionally provide the paragraph view mode to the contained layout,
    // otherwise it would not be available there.
    // Typically the view mode is confugired as "preview" in our configuration:
    $view_mode = $variables['elements']['#view_mode'];
    $variables['content']['regions']['#paragraph_view_mode'] = $view_mode;
  }
}

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024