<?php

/**
 * @file
 * Test module for legacy theme functions.
 *
 * @todo Remove in https://www.drupal.org/project/drupal/issues/3097889
 */

/**
 * Implements hook_theme().
 */
function theme_legacy_test_theme($existing, $type, $theme, $path) {
  $items['theme_test'] = [
    'file' => 'theme_legacy_test.inc',
    'variables' => ['foo' => ''],
    'function' => 'theme_theme_test',
  ];
  $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_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',
  ];
  return $items;
}

/**
 * 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.';
}

/**
 * Theme function for testing rendering of child elements.
 *
 * Theme hooks defining a 'render element' add an internal '#render_children'
 * property. When this property is found, \Drupal::service('renderer')->render()
 * avoids calling the 'theme.manager' service's ThemeManagerInterface::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::service('renderer')->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_legacy_test_theme_suggestions_theme_test_suggestion_provided(array $variables) {
  return ['theme_test_suggestion_provided__foo'];
}

/**
 * Implements hook_theme_suggestions_alter().
 */
function theme_legacy_test_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  \Drupal::messenger()
    ->addStatus(__FUNCTION__ . '() executed for ' . $hook . '.');
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function theme_legacy_test_theme_suggestions_theme_test_suggestions_alter(array &$suggestions, array $variables) {
  \Drupal::messenger()->addStatus(__FUNCTION__ . '() executed.');
}

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