context.module 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. use Drupal\Core\Routing\RouteMatchInterface;
  3. use Drupal\Component\Utility\NestedArray;
  4. /**
  5. * Implements hook_help().
  6. */
  7. function context_help($route_name, RouteMatchInterface $route_match) {
  8. switch ($route_name) {
  9. case 'help.page.context':
  10. $output = '';
  11. $output .= '<h3>' . t('About') . '</h3>';
  12. $output .= '<p>'.t('The Context module lets users define conditions for when certain reactions should take place.').'</p>';
  13. $output .= '<p>'.t('An example of a condition could be when viewing a certain node type and blocks should be placed as a reaction when viewing a page with this node type.').'</p>';
  14. $output .= '<h3>' . t('Uses') . '</h3>';
  15. $output .= '<dl>';
  16. $output .= '<dt>' . t('Managing context') . '</dt>';
  17. $output .= '<dd>' . t('Users with <em>Administer contexts</em> permission can add contextual conditions and reactions for different portions of their site. For each context, they can choose the conditions that trigger this context to be active and choose different aspects of their site that should react to this active context.') . '</dd>';
  18. $output .= '<dt>' . t('Adding new custom reactions') . '</dt>';
  19. $output .= '<dd>' . t('Reactions for the context module are defined trough the new <a href=":link">Drupal 8 Plugin API</a>.', array(':link' =>'https://www.drupal.org/developing/api/8/plugins')) . '</dd>';
  20. $output .= '<dd>' . t('The Context module defines a plugin type named ContextReaction that users can extend when creating their own plugins.') . '</dd>';
  21. $output .= '<dd>' . t('A context reaction requires a configuration form and execute method. The execution of the plugin is also something that will have to be handled by the author of the reaction.') . '</dd>';
  22. $output .= '</dl>';
  23. return $output;
  24. }
  25. }
  26. /**
  27. * Run the body class context reactions if there are any and let them add
  28. * classes to the page body.
  29. *
  30. * Implements hook_preprocess_HOOK().
  31. */
  32. function context_preprocess_html(&$variables) {
  33. /** @var \Drupal\context\ContextManager $context_manager */
  34. $context_manager = \Drupal::service('context.manager');
  35. foreach ($context_manager->getActiveReactions('body_class') as $reaction) {
  36. $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $reaction->execute());
  37. }
  38. }
  39. /**
  40. * Implements hook_theme_suggestions_page_alter().
  41. */
  42. function context_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  43. $context_manager = \Drupal::service('context.manager');
  44. foreach ($context_manager->getActiveReactions('page_template_suggestions') as $reaction) {
  45. $template_suggestions = explode(PHP_EOL, $reaction->execute());
  46. $suggestions = array_merge($suggestions, $template_suggestions);
  47. }
  48. }
  49. /**
  50. * Implements hook_preprocess_page().
  51. */
  52. function context_preprocess_page(&$variables) {
  53. // Active theme for route.
  54. $current_theme = \Drupal::service('theme.negotiator')->determineActiveTheme(Drupal::routeMatch());
  55. // Context manager.
  56. $context_manager = \Drupal::service('context.manager');
  57. // Disable regions based on regions reaction.
  58. foreach ($context_manager->getActiveReactions('regions') as $region_reaction) {
  59. $configuration = $region_reaction->getConfiguration();
  60. if (isset($configuration['regions'][$current_theme])) {
  61. foreach ($configuration['regions'][$current_theme] as $region) {
  62. unset($variables['page'][$region]);
  63. }
  64. }
  65. }
  66. }