context_reaction_template_suggestions.inc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Add template suggestions as a context reaction.
  4. */
  5. class context_reaction_template_suggestions extends context_reaction {
  6. /**
  7. * Display the text area field for adding new template suggestions.
  8. */
  9. function options_form($context) {
  10. $default_value = $this->fetch_from_context($context);
  11. return array(
  12. '#title' => t('Template suggestions'),
  13. '#type' => 'textarea',
  14. '#description' => t('Enter template suggestions such as "page__front", one per line, in order of preference (using underscores instead of hyphens). For more information, please visit <a href="@template-suggestions">Drupal 7 Template (Theme Hook) Suggestions</a>.', array('@template-suggestions' => 'http://drupal.org/node/1089656')),
  15. '#default_value' => is_string($default_value) ? $default_value : '',
  16. );
  17. }
  18. /**
  19. * Add any new template suggestions to the current list.
  20. */
  21. function execute(&$vars = NULL) {
  22. // Get the list of contexts associated with this reaction.
  23. $contexts = $this->get_contexts();
  24. // Iterate through each, and process those with something set.
  25. foreach ($contexts as $context) {
  26. if (isset($context->reactions) && (!empty($context->reactions[$this->plugin]))) {
  27. // Get the suggestion data entered by the user.
  28. $suggestions = $this->fetch_from_context($context, 'values');
  29. // Convert it to an list and reverse it (as higher priority items
  30. // should be on the bottom).
  31. $suggestions = array_reverse(explode("\n", $suggestions));
  32. // Append the suggested list to the existing list.
  33. $vars['theme_hook_suggestions'] = array_merge($vars['theme_hook_suggestions'], $suggestions);
  34. }
  35. }
  36. }
  37. }