Custom route to open in modal is restricted

Created on 16 March 2023, almost 2 years ago

The Layout Builder Component Attributes add a "Manage attributes" contextual links on component (blocks) in Layout Builder.

In the config form of Layout Builder iFrame Modal, there is a "custom routes" textarea which seems to allow for arbitrary routes to open in an iframe modal. The description does not specify any restriction. If I add the "layout_builder_component_attributes.manage_attributes" route in the textarea, nothing happens (I cleared the local storage to be sure the contextual links get rebuild).

Inspecting the code shows that the list of contextual links which can actually open in an iframe modal are hard-coded:

function layout_builder_iframe_modal_contextual_links_alter(array &$links, $group, array $route_parameters) {
  $keys = [
    'layout_builder_block_update',
    'layout_builder_block_remove',
    'layout_builder_block_move',
    'layout_builder_inline_block_update',
  ];

  if (\Drupal::service('module_handler')->moduleExists('layout_builder_st')) {
    $keys[] = 'layout_builder_block_translate';
    $keys[] = 'layout_builder_inline_block_translate';
  }

  foreach ($keys as $key) {
    if (isset($links[$key])) {
      $open = FALSE;
      switch($key) {
        case 'layout_builder_block_update':
        case 'layout_builder_inline_block_update':
          $open = \Drupal::service('layout_builder_iframe_modal.helper')->isModalRoute('layout_builder.update_block');
          break;
        case 'layout_builder_block_remove':
          $open = \Drupal::service('layout_builder_iframe_modal.helper')->isModalRoute('layout_builder.remove_block');
          break;
        case 'layout_builder_block_move':
          $open = \Drupal::service('layout_builder_iframe_modal.helper')->isModalRoute('layout_builder.move_block_form');
          break;
        default:
          break;
      }
      if ($open) {
        $links[$key]['localized_options']['attributes']['data-dialog-type'] = 'iframe';
        unset($links[$key]['localized_options']['attributes']['data-dialog-renderer']);
      }
    }
  }
}

Even the layout_builder_st routes which are added to the $keys variables are not processed.

Given the config used in isModalRoute store the route names, I would suggest to remove any hardcoded route and to browse the $links to check if the route_name is part of the config. Adding a small warning in the "custom routes" description can explain that it may require additional code for this to apply depending the custom route implementation (which is vague enough to not engage the module to work in all possible cases).

The same apply to simple links in layout_builder_iframe_modal_link_alter()

/**
 * Implements hook_link_alter().
 */
function layout_builder_iframe_modal_link_alter(&$variables) {
  $route_name = \Drupal::routeMatch()->getRouteName();

  // Only change links on layout builder routes.
  if ($route_name === NULL || strpos($route_name, 'layout_builder') === FALSE) {
    return;
  }

It only allows for routes which start with layout_builder. It is not an issue in the case of layout_builder_component_attributes but I can imagine some custom module which the module name and route names will be prefixed with a project code and won't be able to use the "Custom route" feature even if all the criteria for this to work are matching.

I will raise a patch in that direction.

🐛 Bug report
Status

Active

Version

1.3

Component

Code

Created by

🇫🇷France vbouchet

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

Comments & Activities

Production build 0.71.5 2024