You are here

theme_test.module in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 core/modules/system/tests/modules/theme_test/theme_test.module

File

core/modules/system/tests/modules/theme_test/theme_test.module
View source
<?php

use Drupal\Core\Extension\Extension;

/**
 * Implements hook_theme().
 */
function theme_test_theme($existing, $type, $theme, $path) {
  $items['theme_test'] = array(
    'file' => 'theme_test.inc',
    'variables' => array(
      'foo' => '',
    ),
    'function' => 'theme_theme_test',
  );
  $items['theme_test_template_test'] = array(
    'template' => 'theme_test.template_test',
  );
  $items['theme_test_template_test_2'] = array(
    'template' => 'theme_test.template_test',
  );
  $items['theme_test_suggestion_provided'] = array(
    'variables' => array(),
  );
  $items['theme_test_specific_suggestions'] = array(
    'variables' => array(),
  );
  $items['theme_test_suggestions'] = array(
    'variables' => array(),
  );
  $items['theme_test_general_suggestions'] = array(
    'variables' => array(),
  );
  $items['theme_test_function_suggestions'] = array(
    'variables' => array(),
    'function' => 'theme_theme_test_function_suggestions',
  );
  $items['theme_test_suggestions_include'] = array(
    'variables' => array(),
    'function' => 'theme_theme_test_suggestions_include',
  );
  $items['theme_test_foo'] = array(
    'variables' => array(
      'foo' => NULL,
    ),
    'function' => 'theme_theme_test_foo',
  );
  $items['theme_test_render_element'] = array(
    'render element' => 'elements',
  );
  $items['theme_test_render_element_children'] = array(
    'render element' => 'element',
    'function' => 'theme_theme_test_render_element_children',
  );
  $items['theme_test_function_template_override'] = array(
    'variables' => array(),
    'function' => 'theme_theme_test_function_template_override',
  );
  $info['test_theme_not_existing_function'] = array(
    'function' => 'test_theme_not_existing_function',
  );
  $items['theme_test_preprocess_suggestions'] = [
    'variables' => [
      'foo' => '',
      'bar' => '',
    ],
  ];
  return $items;
}

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 */
function theme_test_preprocess_html(&$variables) {
  $variables['html_attributes']['theme_test_html_attribute'] = 'theme test html attribute value';
  $variables['attributes']['theme_test_body_attribute'] = 'theme test body attribute value';
  $variables['attributes']['theme_test_page_variable'] = 'Page variable is an array.';
}

/**
 * Implements hook_page_bottom().
 */
function theme_test_page_bottom(array &$page_bottom) {
  $page_bottom['theme_test_page_bottom'] = array(
    '#markup' => 'theme test page bottom markup',
  );
}

/**
 * Implements template_preprocess_HOOK() for theme_test_function_suggestions theme functions.
 */
function template_preprocess_theme_test_function_suggestions(&$variables) {
}

/**
 * Theme function for testing _theme('theme_test_foo').
 */
function theme_theme_test_foo($variables) {
  return $variables['foo'];
}

/**
 * Theme function for testing _theme('theme_test_function_template_override').
 */
function theme_theme_test_function_template_override($variables) {
  return 'theme_test_function_template_override test failed.';
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function theme_test_theme_suggestions_theme_test_preprocess_suggestions($variables) {
  return [
    'theme_test_preprocess_suggestions__' . $variables['foo'],
  ];
}

/**
 * Implements hook_preprocess_HOOK().
 */
function theme_test_preprocess_theme_test_preprocess_suggestions(&$variables) {
  $variables['foo'] = 'Theme hook implementor=theme_theme_test_preprocess_suggestions().';
}

/**
 * Tests a module overriding a default hook with a suggestion.
 */
function theme_test_preprocess_theme_test_preprocess_suggestions__monkey(&$variables) {
  $variables['foo'] = 'Monkey';
}

/**
 * Prepares variables for test render element templates.
 *
 * Default template: theme-test-render-element.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the properties of the element.
 */
function template_preprocess_theme_test_render_element(&$variables) {
  $variables['attributes']['data-variables-are-preprocessed'] = TRUE;
}

/**
 * Theme function for testing rendering of child elements via drupal_render().
 *
 * Theme hooks defining a 'render element' add an internal '#render_children'
 * property. When this property is found, drupal_render() avoids calling
 * _theme() on the top-level element to prevent infinite recursion.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 */
function theme_theme_test_render_element_children($variables) {
  return drupal_render($variables['element']);
}

/**
 * Returns HTML for a theme function suggestion test.
 */
function theme_theme_test_function_suggestions($variables) {
  return 'Original theme function.';
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function theme_test_theme_suggestions_theme_test_suggestion_provided(array $variables) {
  return array(
    'theme_test_suggestion_provided__' . 'foo',
  );
}

/**
 * Implements hook_theme_suggestions_alter().
 */
function theme_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  drupal_set_message(__FUNCTION__ . '() executed.');
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function theme_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
  drupal_set_message(__FUNCTION__ . '() executed.');
}

/**
 * Returns HTML for a theme function include test.
 */
function theme_theme_test_suggestions_include($variables) {
  return 'Original function before altering theme suggestions.';
}

/**
 * Implements hook_system_info_alter().
 *
 * @see \Drupal\system\Tests\Theme\ThemeInfoTest::testChanges()
 */
function theme_test_system_info_alter(array &$info, Extension $file, $type) {
  if ($type == 'theme' && $file
    ->getName() == 'test_theme' && \Drupal::state()
    ->get('theme_test.modify_info_files')) {

    // Add a library to see if the system picks it up.
    $info += [
      'libraries' => [],
    ];
    $info['libraries'][] = 'core/backbone';
  }
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function theme_test_theme_suggestions_node(array $variables) {
  $xss = '<script type="text/javascript">alert(\'yo\');</script>';
  $suggestions[] = 'node__' . $xss;
  return $suggestions;
}

Functions

Namesort descending Description
template_preprocess_theme_test_function_suggestions Implements template_preprocess_HOOK() for theme_test_function_suggestions theme functions.
template_preprocess_theme_test_render_element Prepares variables for test render element templates.
theme_test_page_bottom Implements hook_page_bottom().
theme_test_preprocess_html Implements hook_preprocess_HOOK() for HTML document templates.
theme_test_preprocess_theme_test_preprocess_suggestions Implements hook_preprocess_HOOK().
theme_test_preprocess_theme_test_preprocess_suggestions__monkey Tests a module overriding a default hook with a suggestion.
theme_test_system_info_alter Implements hook_system_info_alter().
theme_test_theme Implements hook_theme().
theme_test_theme_suggestions_alter Implements hook_theme_suggestions_alter().
theme_test_theme_suggestions_node Implements hook_theme_suggestions_HOOK().
theme_test_theme_suggestions_theme_test_preprocess_suggestions Implements hook_theme_suggestions_HOOK().
theme_test_theme_suggestions_theme_test_suggestions_alter Implements hook_theme_suggestions_HOOK_alter().
theme_test_theme_suggestions_theme_test_suggestion_provided Implements hook_theme_suggestions_HOOK().
theme_theme_test_foo Theme function for testing _theme('theme_test_foo').
theme_theme_test_function_suggestions Returns HTML for a theme function suggestion test.
theme_theme_test_function_template_override Theme function for testing _theme('theme_test_function_template_override').
theme_theme_test_render_element_children Theme function for testing rendering of child elements via drupal_render().
theme_theme_test_suggestions_include Returns HTML for a theme function include test.