<?php

/**
 * @file
 * Test module.
 */

use Drupal\Core\Extension\Extension;

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

/**
 * Theme function for hook 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
 * the 'theme.manager' service 'render' method 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 ['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;
}
