ThemeTestController.php 3.76 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\theme_test\ThemeTestController.
 */

namespace Drupal\theme_test;

10
use Drupal\Core\Controller\ControllerBase;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12 13 14 15

/**
 * Controller routines for theme test routes.
 */
16
class ThemeTestController extends ControllerBase {
17 18

  /**
19 20 21 22
   * A theme template that overrides a theme function.
   *
   * @return array
   *   Render array containing a theme.
23
   */
24 25 26 27
  public function functionTemplateOverridden() {
    return array(
      '#theme' => 'theme_test_function_template_override',
    );
28 29 30
  }

  /**
31 32 33 34
   * Adds stylesheets to test theme .info.yml property processing.
   *
   * @return array
   *   A render array containing custom stylesheets.
35
   */
36 37
  public function testInfoStylesheets() {
    $path = drupal_get_path('module', 'theme_test');
38
    return array(
39 40 41 42 43 44 45 46 47 48
      '#attached' => array(
        'css' => array(
          "$path/css/base-override.css",
          "$path/css/base-override.sub-remove.css",
          "$path/css/base-remove.css",
          "$path/css/base-remove.sub-override.css",
          "$path/css/sub-override.css",
          "$path/css/sub-remove.css",
        ),
      ),
49 50 51
    );
  }

52 53 54 55 56 57 58
  /**
   * Tests template overridding based on filename.
   *
   * @return array
   *   A render array containing a theme override.
   */
  public function testTemplate() {
59
    return \Drupal::theme()->render('theme_test_template_test', array());
60 61
  }

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  /**
   * Tests the inline template functionality.
   *
   * @return array
   *   A render array containing an inline template.
   */
  public function testInlineTemplate() {
    $element = array();
    $element['test'] = array(
      '#type' => 'inline_template',
      '#template' => 'test-with-context {{ lama }}',
      '#context' => array('lama' => 'muuh'),
    );
    return $element;
  }

78 79 80 81 82 83 84
  /**
   * Calls a theme hook suggestion.
   *
   * @return string
   *   An HTML string containing the themed output.
   */
  public function testSuggestion() {
85
    return \Drupal::theme()->render(array('theme_test__suggestion', 'theme_test'), array());
86 87 88 89 90 91 92 93 94 95 96 97
  }

  /**
   * Tests themed output generated in a request listener.
   *
   * @return string
   *   Content in theme_test_output GLOBAL.
   */
  public function testRequestListener() {
    return $GLOBALS['theme_test_output'];
  }

98 99 100 101 102 103 104 105 106 107 108 109 110 111
  /**
   * Menu callback for testing suggestion alter hooks with template files.
   */
  function suggestionProvided() {
    return array('#theme' => 'theme_test_suggestion_provided');
  }

  /**
   * Menu callback for testing suggestion alter hooks with template files.
   */
  function suggestionAlter() {
    return array('#theme' => 'theme_test_suggestions');
  }

112 113 114 115 116 117 118
  /**
   * Menu callback for testing hook_theme_suggestions_alter().
   */
  function generalSuggestionAlter() {
    return array('#theme' => 'theme_test_general_suggestions');
  }

119 120 121 122 123 124 125 126 127 128 129 130 131 132
  /**
   * Menu callback for testing suggestion alter hooks with specific suggestions.
   */
  function specificSuggestionAlter() {
    return array('#theme' => 'theme_test_specific_suggestions__variant');
  }

  /**
   * Menu callback for testing suggestion alter hooks with theme functions.
   */
  function functionSuggestionAlter() {
    return array('#theme' => 'theme_test_function_suggestions');
  }

133 134 135 136 137 138 139 140

  /**
   * Menu callback for testing includes with suggestion alter hooks.
   */
  function suggestionAlterInclude() {
    return array('#theme' => 'theme_test_suggestions_include');
  }

141 142 143 144 145 146 147 148 149 150 151
  /**
   * Controller to ensure that no theme is initialized.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The json response with the theme initialized information.
   */
  public function nonHtml() {
    $theme_initialized = \Drupal::theme()->hasActiveTheme();
    return new JsonResponse(['theme_initialized' => $theme_initialized]);
  }

152
}