You are here

common_test.module in Zircon Profile 8.0

Same filename and directory in other branches
  1. 8 core/modules/system/tests/modules/common_test/common_test.module

Helper module for the Common tests.

File

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

/**
 * @file
 * Helper module for the Common tests.
 */
use Drupal\Core\Asset\AttachedAssetsInterface;

/**
 * Applies #printed to an element to help test #pre_render.
 */
function common_test_drupal_render_printing_pre_render($elements) {
  $elements['#printed'] = TRUE;
  return $elements;
}

/**
 * Implements hook_TYPE_alter().
 */
function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {

  // Alter first argument.
  if (is_array($data)) {
    $data['foo'] = 'Drupal';
  }
  elseif (is_object($data)) {
    $data->foo = 'Drupal';
  }

  // Alter second argument, if present.
  if (isset($arg2)) {
    if (is_array($arg2)) {
      $arg2['foo'] = 'Drupal';
    }
    elseif (is_object($arg2)) {
      $arg2->foo = 'Drupal';
    }
  }

  // Try to alter third argument, if present.
  if (isset($arg3)) {
    if (is_array($arg3)) {
      $arg3['foo'] = 'Drupal';
    }
    elseif (is_object($arg3)) {
      $arg3->foo = 'Drupal';
    }
  }
}

/**
 * Implements hook_TYPE_alter() on behalf of Bartik theme.
 *
 * Same as common_test_drupal_alter_alter(), but here, we verify that themes
 * can also alter and come last.
 */
function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {

  // 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 hook_TYPE_alter() on behalf of block module.
 *
 * This is to verify that
 * \Drupal::moduleHandler()->alter(array(TYPE1, TYPE2), ...) allows
 * hook_module_implements_alter() to affect the order in which module
 * implementations are executed.
 */
function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  $data['foo'] .= ' block';
}

/**
 * Implements hook_module_implements_alter().
 *
 * @see block_drupal_alter_foo_alter()
 */
function common_test_module_implements_alter(&$implementations, $hook) {

  // For
  // \Drupal::moduleHandler()->alter(array('drupal_alter', 'drupal_alter_foo'), ...),
  // make the block module implementations run after all the other modules. Note
  // that when \Drupal::moduleHandler->alter() is called with an array of types,
  // the first type is considered primary and controls the module order.
  if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
    $group = $implementations['block'];
    unset($implementations['block']);
    $implementations['block'] = $group;
  }
}

/**
 * Implements hook_theme().
 */
function common_test_theme() {
  return array(
    'common_test_foo' => array(
      'variables' => array(
        'foo' => 'foo',
        'bar' => 'bar',
      ),
    ),
    'common_test_render_element' => array(
      'render element' => 'foo',
    ),
    'common_test_empty' => array(
      'variables' => array(
        'foo' => 'foo',
      ),
      'function' => 'theme_common_test_empty',
    ),
  );
}

/**
 * Provides a theme function for drupal_render().
 */
function theme_common_test_foo($variables) {
  return $variables['foo'] . $variables['bar'];
}

/**
 * Always returns an empty string.
 */
function theme_common_test_empty($variables) {
  return '';
}

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

/**
 * Implements hook_library_info_build().
 */
function common_test_library_info_build() {
  $libraries = [];
  if (\Drupal::state()
    ->get('common_test.library_info_build_test')) {
    $libraries['dynamic_library'] = [
      'version' => '1.0',
      'css' => [
        'base' => [
          'common_test.css' => [],
        ],
      ],
    ];
  }
  return $libraries;
}

/**
 * Implements hook_library_info_alter().
 */
function common_test_library_info_alter(&$libraries, $module) {
  if ($module == 'core' && isset($libraries['jquery.farbtastic'])) {

    // Change the version of Farbtastic to 0.0.
    $libraries['jquery.farbtastic']['version'] = '0.0';

    // Make Farbtastic depend on jQuery Form to test library dependencies.
    $libraries['jquery.farbtastic']['dependencies'][] = 'core/jquery.form';
  }

  // Alter the dynamically registered library definition.
  if ($module == 'common_test' && isset($libraries['dynamic_library'])) {
    $libraries['dynamic_library']['dependencies'] = [
      'core/jquery',
    ];
  }
}

/**
 * Implements hook_cron().
 *
 * System module should handle if a module does not catch an exception and keep
 * cron going.
 *
 * @see common_test_cron_helper()
 *
 */
function common_test_cron() {
  throw new Exception(t('Uncaught exception'));
}

/**
 * Implements hook_page_attachments().
 *
 * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
 */
function common_test_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'core/foo';
  $page['#attached']['library'][] = 'core/bar';
  $page['#cache']['tags'] = [
    'example',
  ];
  $page['#cache']['contexts'] = [
    'user.permissions',
  ];
  if (\Drupal::state()
    ->get('common_test.hook_page_attachments.descendant_attached', FALSE)) {
    $page['content']['#attached']['library'][] = 'core/jquery';
  }
  if (\Drupal::state()
    ->get('common_test.hook_page_attachments.render_array', FALSE)) {
    $page['something'] = [
      '#markup' => 'test',
    ];
  }
}

/**
 * Implements hook_page_attachments_alter().
 *
 * @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
 */
function common_test_page_attachments_alter(array &$page) {

  // Remove a library that was added in common_test_page_attachments(), to test
  // that this hook can do what it claims to do.
  if (isset($page['#attached']['library']) && ($index = array_search('core/bar', $page['#attached']['library'])) && $index !== FALSE) {
    unset($page['#attached']['library'][$index]);
  }
  $page['#attached']['library'][] = 'core/baz';
  $page['#cache']['tags'] = [
    'example',
  ];
  $page['#cache']['contexts'] = [
    'user.permissions',
  ];
  if (\Drupal::state()
    ->get('common_test.hook_page_attachments_alter.descendant_attached', FALSE)) {
    $page['content']['#attached']['library'][] = 'core/jquery';
  }
  if (\Drupal::state()
    ->get('common_test.hook_page_attachments_alter.render_array', FALSE)) {
    $page['something'] = [
      '#markup' => 'test',
    ];
  }
}

/**
 * Implements hook_js_settings_alter().
 *
 * @see \Drupal\system\Tests\Common\JavaScriptTest::testHeaderSetting()
 */
function common_test_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {

  // Modify an existing setting.
  if (array_key_exists('pluralDelimiter', $settings)) {
    $settings['pluralDelimiter'] = '☃';
  }

  // Add a setting.
  $settings['foo'] = 'bar';
}

Functions

Namesort descending Description
bartik_drupal_alter_alter Implements hook_TYPE_alter() on behalf of Bartik theme.
block_drupal_alter_foo_alter Implements hook_TYPE_alter() on behalf of block module.
common_test_cron Implements hook_cron().
common_test_drupal_alter_alter Implements hook_TYPE_alter().
common_test_drupal_render_printing_pre_render Applies #printed to an element to help test #pre_render.
common_test_js_settings_alter Implements hook_js_settings_alter().
common_test_library_info_alter Implements hook_library_info_alter().
common_test_library_info_build Implements hook_library_info_build().
common_test_module_implements_alter Implements hook_module_implements_alter().
common_test_page_attachments Implements hook_page_attachments().
common_test_page_attachments_alter Implements hook_page_attachments_alter().
common_test_preprocess Implements MODULE_preprocess().
common_test_preprocess_common_test_render_element Implements MODULE_preprocess_HOOK().
common_test_theme Implements hook_theme().
theme_common_test_empty Always returns an empty string.
theme_common_test_foo Provides a theme function for drupal_render().