language_test.module 3.43 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Mock module for language layer tests.
6 7
 */

8
use Drupal\Core\Language\LanguageInterface;
9
use Drupal\Core\Url;
10
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;
11

12
/**
13
 * Implements hook_page_top().
14
 */
15
function language_test_page_top() {
16 17
  if (\Drupal::moduleHandler()->moduleExists('language')) {
    language_test_store_language_negotiation();
18
    \Drupal::messenger()->addStatus(t('Language negotiation method: @name', ['@name' => \Drupal::languageManager()->getNegotiatedLanguageMethod()]));
19
  }
20 21 22 23 24
}

/**
 * Implements hook_language_types_info().
 */
25
function language_test_language_types_info() {
26
  if (\Drupal::state()->get('language_test.language_types')) {
27 28
    return [
      'test_language_type' => [
29 30
        'name' => t('Test'),
        'description' => t('A test language type.'),
31 32 33
      ],
      'fixed_test_language_type' => [
        'fixed' => ['test_language_negotiation_method'],
34
        'locked' => TRUE,
35 36
      ],
    ];
37 38 39
  }
}

40 41 42
/**
 * Implements hook_language_types_info_alter().
 */
43
function language_test_language_types_info_alter(array &$language_types) {
44
  if (\Drupal::state()->get('language_test.content_language_type')) {
45 46 47 48
    $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.
49 50
    $config = \Drupal::configFactory()->getEditable('language.types');
    $configurable = $config->get('configurable');
51 52
    if (!in_array(LanguageInterface::TYPE_CONTENT, $configurable)) {
      $configurable[] = LanguageInterface::TYPE_CONTENT;
53
      $config->set('configurable', $configurable)->save();
54
    }
55 56 57 58 59 60
  }
}

/**
 * Implements hook_language_negotiation_info_alter().
 */
61
function language_test_language_negotiation_info_alter(array &$negotiation_info) {
62
  if (\Drupal::state()->get('language_test.language_negotiation_info_alter')) {
63
    unset($negotiation_info[LanguageNegotiationUI::METHOD_ID]);
64 65
  }
}
66 67 68 69

/**
 * Store the last negotiated languages.
 */
70
function language_test_store_language_negotiation() {
71
  $last = [];
72
  foreach (\Drupal::languageManager()->getDefinedLanguageTypes() as $type) {
73
    $last[$type] = \Drupal::languageManager()->getCurrentLanguage($type)->getId();
74
  }
75
  \Drupal::state()->set('language_test.language_negotiation_last', $last);
76
}
77 78 79 80 81 82

/**
 * 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')) {
83
    unset($candidates[LanguageInterface::LANGCODE_NOT_SPECIFIED]);
84 85 86 87 88 89 90 91
  }
}

/**
 * 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')) {
92
    $langcode = LanguageInterface::LANGCODE_NOT_APPLICABLE;
93 94 95
    $candidates[$langcode] = $langcode;
  }
}
96 97 98 99 100

/**
 * Implements hook_module_preinstall().
 */
function language_test_module_preinstall() {
101
  \Drupal::state()->set('language_test.language_count_preinstall', count(\Drupal::languageManager()->getLanguages()));
102
}
103 104 105 106 107 108 109

/**
 * Implements hook_language_switch_links_alter().
 */
function language_test_language_switch_links_alter(array &$links, $type, Url $url) {
  // I'll just sit here and wait to be called with the right arguments.
}