language_test.module

Same filename in other branches
  1. 9 core/modules/language/tests/language_test/language_test.module
  2. 8.9.x core/modules/language/tests/language_test/language_test.module
  3. 11.x core/modules/language/tests/language_test/language_test.module

Mock module for language layer tests.

File

core/modules/language/tests/language_test/language_test.module

View source
<?php


/**
 * @file
 * Mock module for language layer tests.
 */
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;

/**
 * Implements hook_page_top().
 */
function language_test_page_top() {
    if (\Drupal::moduleHandler()->moduleExists('language')) {
        language_test_store_language_negotiation();
        \Drupal::messenger()->addStatus(t('Language negotiation method: @name', [
            '@name' => \Drupal::languageManager()->getNegotiatedLanguageMethod() ?? 'Not defined',
        ]));
    }
}

/**
 * Implements hook_language_types_info().
 */
function language_test_language_types_info() {
    if (\Drupal::state()->get('language_test.language_types')) {
        return [
            'test_language_type' => [
                'name' => t('Test'),
                'description' => t('A test language type.'),
            ],
            'fixed_test_language_type' => [
                'fixed' => [
                    'test_language_negotiation_method',
                ],
                'locked' => TRUE,
            ],
        ];
    }
}

/**
 * Implements hook_language_types_info_alter().
 */
function language_test_language_types_info_alter(array &$language_types) {
    if (\Drupal::state()->get('language_test.content_language_type')) {
        $language_types[LanguageInterface::TYPE_CONTENT]['locked'] = FALSE;
        unset($language_types[LanguageInterface::TYPE_CONTENT]['fixed']);
        // By default languages are not configurable. Make
        // LanguageInterface::TYPE_CONTENT configurable.
        $config = \Drupal::configFactory()->getEditable('language.types');
        $configurable = $config->get('configurable');
        if (!in_array(LanguageInterface::TYPE_CONTENT, $configurable)) {
            $configurable[] = LanguageInterface::TYPE_CONTENT;
            $config->set('configurable', $configurable)
                ->save();
        }
    }
}

/**
 * Implements hook_language_negotiation_info_alter().
 */
function language_test_language_negotiation_info_alter(array &$negotiation_info) {
    if (\Drupal::state()->get('language_test.language_negotiation_info_alter')) {
        unset($negotiation_info[LanguageNegotiationUI::METHOD_ID]);
    }
}

/**
 * Store the last negotiated languages.
 */
function language_test_store_language_negotiation() {
    $last = [];
    foreach (\Drupal::languageManager()->getDefinedLanguageTypes() as $type) {
        $last[$type] = \Drupal::languageManager()->getCurrentLanguage($type)
            ->getId();
    }
    \Drupal::keyValue('language_test')->set('language_negotiation_last', $last);
}

/**
 * Implements hook_language_fallback_candidates_alter().
 */
function language_test_language_fallback_candidates_alter(array &$candidates, array $context) {
    if (Drupal::state()->get('language_test.fallback_alter.candidates')) {
        unset($candidates[LanguageInterface::LANGCODE_NOT_SPECIFIED]);
    }
}

/**
 * Implements hook_language_fallback_candidates_OPERATION_alter().
 */
function language_test_language_fallback_candidates_test_alter(array &$candidates, array $context) {
    if (Drupal::state()->get('language_test.fallback_operation_alter.candidates')) {
        $langcode = LanguageInterface::LANGCODE_NOT_APPLICABLE;
        $candidates[$langcode] = $langcode;
    }
}

/**
 * Implements hook_module_preinstall().
 */
function language_test_module_preinstall() {
    \Drupal::state()->set('language_test.language_count_preinstall', count(\Drupal::languageManager()->getLanguages()));
}

/**
 * Implements hook_language_switch_links_alter().
 */
function language_test_language_switch_links_alter(array &$links, $type, Url $url) {
    // Record which languages had links passed in.
    \Drupal::state()->set('language_test.language_switch_link_ids', array_keys($links));
}

Functions

Title Deprecated Summary
language_test_language_fallback_candidates_alter Implements hook_language_fallback_candidates_alter().
language_test_language_fallback_candidates_test_alter Implements hook_language_fallback_candidates_OPERATION_alter().
language_test_language_negotiation_info_alter Implements hook_language_negotiation_info_alter().
language_test_language_switch_links_alter Implements hook_language_switch_links_alter().
language_test_language_types_info Implements hook_language_types_info().
language_test_language_types_info_alter Implements hook_language_types_info_alter().
language_test_module_preinstall Implements hook_module_preinstall().
language_test_page_top Implements hook_page_top().
language_test_store_language_negotiation Store the last negotiated languages.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.