<?php

/**
 * @file
 * Helper module for the Common tests.
 */

declare(strict_types=1);

/**
 * Implements hook_TYPE_alter().
 *
 * Same as common_test_drupal_alter_alter(), but here, we verify that themes
 * can also alter and come last.
 */
function olivero_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL): void {
  // Alter first argument.
  if (is_array($data)) {
    $data['foo'] .= ' theme';
  }
  elseif (is_object($data)) {
    $data->foo .= ' theme';
  }
  // Alter second argument, if present.
  if (isset($arg2)) {
    if (is_array($arg2)) {
      $arg2['foo'] .= ' theme';
    }
    elseif (is_object($arg2)) {
      $arg2->foo .= ' theme';
    }
  }
  // Try to alter third argument, if present.
  if (isset($arg3)) {
    if (is_array($arg3)) {
      $arg3['foo'] .= ' theme';
    }
    elseif (is_object($arg3)) {
      $arg3->foo .= ' theme';
    }
  }
}

/**
 * Implements MODULE_preprocess().
 *
 * @see RenderTest::testDrupalRenderThemePreprocessAttached()
 */
function common_test_preprocess(&$variables, $hook): void {
  if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
    return;
  }
  $variables['#attached']['library'][] = 'test/generic_preprocess';
}

/**
 * Implements MODULE_preprocess_HOOK().
 *
 * @see RenderTest::testDrupalRenderThemePreprocessAttached()
 */
function common_test_preprocess_common_test_render_element(&$variables): void {
  if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
    return;
  }
  $variables['#attached']['library'][] = 'test/specific_preprocess';
}
