You are here

function gin_lb_theme_suggestions_alter in Gin Layout Builder 1.0.x

Implements hook_theme_suggestions_alter().

File

./gin_lb.module, line 168
Provides hooks for gin_lb module.

Code

function gin_lb_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if (gin_lb_is_valid_theme() === FALSE) {
    return;
  }
  $add_suggestions = FALSE;
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  if (in_array($route_name, [
    'layout_builder.add_block',
    'layout_builder.choose_block',
    'layout_builder.choose_inline_block',
    'layout_builder.remove_section',
    'layout_builder.remove_block',
    'layout_builder.choose_section',
    'view.media_library.widget_table',
    'view.media_library.widget',
    'media_library.ui',
    'editor.media_dialog',
    'editor.link_dialog',
    'editor.image_dialog',
  ])) {
    $add_suggestions = TRUE;
  }

  // For ajax the route is views.ajax
  // So a look to the suggestions help.
  if ($route_name === 'views.ajax') {
    if (\Drupal::request()->query
      ->get('media_library_opener_id')) {
      $add_suggestions = TRUE;
    }
    $view = isset($variables['view']) && $variables['view'] instanceof ViewExecutable;
    if ($view && $variables['view']
      ->id() === 'media_library') {
      $add_suggestions = TRUE;
    }
    if ($hook === 'media' && $variables['elements']['#view_mode'] === 'media_library') {
      $add_suggestions = TRUE;
    }
  }
  if (gin_lb_is_layout_builder_route() && in_array($hook, [
    'status_messages',
    'media_library_wrapper',
    'views_view',
    'views_view_unformatted',
    'media',
    'views_mini_pager',
    'pager',
  ])) {
    $add_suggestions = TRUE;
  }
  if (isset($variables['element']['#gin_lb_form']) || $add_suggestions === TRUE) {

    // Fix form element suggestions when they are not implemented in the theme.
    if (empty($suggestions) && !empty($variables['theme_hook_original'])) {
      $suggestions = [
        $variables['theme_hook_original'],
      ];
    }
    $additional_suggestions = [];
    foreach ($suggestions as $suggestion) {
      $additional_suggestions[] = $suggestion . '__gin_lb';
    }
    $suggestions[] = $hook . '__gin_lb';
    $suggestions = array_merge($suggestions, $additional_suggestions);
    if (isset($variables['view']) && $hook === 'views_view_unformatted') {
      $suggestions[] = $hook . '__' . $variables['view']
        ->id() . '__gin_lb';
    }
  }
}