layout_discovery.module 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * @file
  4. * Provides hook implementations for Layout Discovery.
  5. */
  6. use Drupal\Core\Render\Element;
  7. use Drupal\Core\Template\Attribute;
  8. /**
  9. * Implements hook_help().
  10. */
  11. function layout_discovery_help($route_name) {
  12. switch ($route_name) {
  13. case 'help.page.layout_discovery':
  14. $output = '<h3>' . t('About') . '</h3>';
  15. $output .= '<p>' . t('Layout Discovery allows modules or themes to register layouts, and for other modules to list the available layouts and render them.') . '</p>';
  16. $output .= '<p>' . t('For more information, see the <a href=":layout-discovery-documentation">online documentation for the Layout Discovery module</a>.', [':layout-discovery-documentation' => 'https://www.drupal.org/docs/8/api/layout-api']) . '</p>';
  17. return $output;
  18. }
  19. }
  20. /**
  21. * Implements hook_theme().
  22. */
  23. function layout_discovery_theme() {
  24. return \Drupal::service('plugin.manager.core.layout')->getThemeImplementations();
  25. }
  26. /**
  27. * Prepares variables for layout templates.
  28. *
  29. * @param array &$variables
  30. * An associative array containing:
  31. * - content: An associative array containing the properties of the element.
  32. * Properties used: #settings, #layout.
  33. */
  34. function template_preprocess_layout(&$variables) {
  35. $variables['settings'] = isset($variables['content']['#settings']) ? $variables['content']['#settings'] : [];
  36. $variables['layout'] = isset($variables['content']['#layout']) ? $variables['content']['#layout'] : [];
  37. // Create an attributes variable for each region.
  38. foreach (Element::children($variables['content']) as $name) {
  39. if (!isset($variables['content'][$name]['#attributes'])) {
  40. $variables['content'][$name]['#attributes'] = [];
  41. }
  42. $variables['region_attributes'][$name] = new Attribute($variables['content'][$name]['#attributes']);
  43. }
  44. }