Commit 2c578487 authored by Kristof De Jaeger's avatar Kristof De Jaeger
Browse files

Issue #2887778 by swentel, MXT, gordon: Template suggestions does not work

parent 1b5ea2c2
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\ds\Ds;
use Drupal\ds\DsAttribute;
@@ -176,6 +177,33 @@ function ds_preprocess_layout(&$variables): void {
  }
}

/**
 * Implements hook_theme_suggestions_alter().
 */
function ds_theme_suggestions_alter(&$suggestions, $variables, $base_theme_hook) {
  if (isset($variables['content']) && is_array($variables['content']) && isset($variables['content']['#ds_variables'])) {
    try {
      $template_id = $variables['content']['#layout']->getThemeHook();
      $entity_type_id = $variables['content']['#entity_type'];
      $entity = $variables['content']['#' . $entity_type_id];
      $view_mode = $variables['content']['#view_mode'];
      $bundle = $entity->bundle();
      $entity_id = $entity->id();
      $suggestions[] = $template_id . '__' . $entity_type_id;
      $suggestions[] = $template_id . '__' . $entity_type_id . '__' . $view_mode;
      $suggestions[] = $template_id . '__' . $entity_type_id . '__' . $bundle;
      $suggestions[] = $template_id . '__' . $entity_type_id . '__' . $bundle . '__' . $view_mode;
      $suggestions[] = $template_id . '__' . $entity_type_id . '__' . $entity_id;
    }
    catch (\Exception $e) {
      if (Settings::get('ds_log_ds_suggestions_error', TRUE)) {
        \Drupal::logger('ds_layout_suggestions')->notice('Exception getting suggestions: @message', ['@message' => $e->getMessage()]);
      }
    }
  }
}


/**
 * Implements hook_contextual_links_view_alter().
 */
+21 −1
Original line number Diff line number Diff line
@@ -958,13 +958,29 @@ function _ds_field_ui_table_layouts_preview(array &$form, FormStateInterface $fo
    $layout_string = $form_state->hasValue('ds_layout') ? $form_state->getValue('ds_layout') : $layout['layout'];
  }

  $suggestions_array = [];
  if (!empty($layout_string)) {
    $selected = '';
    $chosen_layout = NULL;
    if (isset($ds_layouts[$layout_string])) {
      $chosen_layout = $ds_layouts[$layout_string];
      /** @var \Drupal\Core\Layout\LayoutDefinition $chosen_layout */
      $selected = '<strong>' . $chosen_layout->getLabel() . '</strong>';
      $selected .= '<br/>' . t('The default template can be found in %path', ['%path' => $chosen_layout->getPath() . '/' . $chosen_layout->getTemplate()]);
      $suggestions_array[0] = $chosen_layout->getThemeHook() . '--' . $display->getTargetEntityTypeId();
      $suggestions_array[2] = $chosen_layout->getThemeHook() . '--' . $display->getTargetEntityTypeId() . '--' . $display->getTargetBundle();
      $suggestions_array[4] = $chosen_layout->getThemeHook() . '--' . $display->getTargetEntityTypeId() . '--{id}';
      if ($display->getMode() != 'default') {
        $suggestions_array[1] = $chosen_layout->getThemeHook() . '--' . $display->getTargetEntityTypeId() . '--' . $display->getMode();
        $suggestions_array[3] = $chosen_layout->getThemeHook() . '--' . $display->getTargetEntityTypeId() . '--' . $display->getTargetBundle() . '--' . $display->getMode();
      }
    }

    ksort($suggestions_array);
    // Append correct extension.
    foreach ($suggestions_array as $key => $value) {
      $suggestion_replace = strtr($value, '_', '-');
      $suggestions_array[$key] = $suggestion_replace . '.html.twig';
    }

    if ($form_state->hasValue('ds_layout') || (!empty($layout) && isset($layout['regions'])) && (!empty($chosen_layout))) {
@@ -988,7 +1004,11 @@ function _ds_field_ui_table_layouts_preview(array &$form, FormStateInterface $fo
        ],
      ];
      $form['ds_layouts']['preview']['info']['suggestions'] = [
        '#markup' => '<p>' . $selected . '</p>',
        '#markup' => '<p>' . $selected . '</p><p>' . t('Template suggestions') . ':' . '</p>',
      ];
      $form['ds_layouts']['preview']['info']['suggestions_list'] = [
        '#theme' => 'item_list',
        '#items' => $suggestions_array,
      ];

      $form['ds_layouts']['preview']['clear'] = [
+14 −10
Original line number Diff line number Diff line
@@ -47,34 +47,38 @@ class EntityViewAlter implements EntityViewAlterInterface {
    }

    // Get configuration.
    $configuration = $display->getThirdPartySettings('ds');
    $manage_display_settings = $display->getThirdPartySettings('ds');

    // Don't fatal on missing layout plugins.
    $layout_id = $configuration['layout']['id'] ?? '';
    $layout_id = $manage_display_settings['layout']['id'] ?? '';
    if (!Ds::layoutExists($layout_id)) {
      return;
    }

    /** @var \Drupal\Core\Layout\LayoutInterface $layout */
    $layout_settings = $configuration['layout']['settings'];
    $layout_settings = $manage_display_settings['layout']['settings'];

    // Add entity and view mode to settings.
    $layout_settings['_ds_entity'] = $entity;
    $layout_settings['_ds_view_mode'] = $view_mode;

    // Get the layout.
    $layout = \Drupal::service('plugin.manager.core.layout')->createInstance($configuration['layout']['id'], $layout_settings);
    $layout = \Drupal::service('plugin.manager.core.layout')->createInstance($manage_display_settings['layout']['id'], $layout_settings);

    // TODO make sure every key is 'ok'
    $layout_build = $layout->build($configuration['regions']);
    $layout_build = $layout->build($manage_display_settings['regions']);
    $build['#ds_variables'] = $layout_build;
    $build['#layout'] = $layout;
    $build['#settings'] = $configuration;
    $build['#layout'] = $layout_build['#layout'];
    $build['#settings'] = $layout_build['#settings'];
    $build['#theme'] = $layout_build['#theme'];

    $this->addDsFields($build, $entity, $bundle, $view_mode, $display, $configuration, $field_permissions);
    // For some reason, #entity_type is not always set.
    if (!isset($build['#entity_type'])) {
      $build['#entity_type'] = $entity->getEntityTypeId();
    }

    $this->addDsFields($build, $entity, $bundle, $view_mode, $display, $manage_display_settings, $field_permissions);
    $this->implementUiLimit($build, $display);
    $this->moveFieldsIntoRegions($build, $configuration);
    $this->moveFieldsIntoRegions($build, $manage_display_settings);

  }

+4 −0
Original line number Diff line number Diff line
@@ -258,6 +258,10 @@ class DsLayout extends LayoutDefault implements PluginFormInterface {
    $entity = $layout_settings['_ds_entity'] ?? FALSE;
    $view_mode = $layout_settings['_ds_view_mode'] ?? 'full';

    unset($layout_settings['_ds_entity']);
    unset($layout_settings['_ds_view_mode']);
    $this->setConfiguration($layout_settings);

    // Parent build.
    $ds_build = parent::build($regions);

+1 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ class JavascriptTest extends WebDriverTestBase {
    // if the new template is displayed.
    $page->selectFieldOption('ds_layout', 'ds_2col');
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertSession()->pageTextContains('ds-2col--node.html.twig');
    $page->pressButton('Save');

    $this->drupalGet('admin/structure/types/manage/article/display');
Loading