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

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

8 9 10
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

11 12 13
/**
 * Implements hook_init().
 */
14 15
function language_test_init() {
  language_test_store_language_negotiation();
16 17
  if (isset(language(LANGUAGE_TYPE_INTERFACE)->langcode) && isset(language(LANGUAGE_TYPE_INTERFACE)->method_id)) {
    drupal_set_message(t('Language negotiation method: @name', array('@name' => language(LANGUAGE_TYPE_INTERFACE)->method_id)));
18
  }
19 20 21 22 23
}

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

38 39 40
/**
 * Implements hook_language_types_info_alter().
 */
41
function language_test_language_types_info_alter(array &$language_types) {
42
  if (state()->get('language_test.content_language_type')) {
43 44 45 46 47 48 49
    unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
  }
}

/**
 * Implements hook_language_negotiation_info().
 */
50
function language_test_language_negotiation_info() {
51
  if (state()->get('language_test.language_negotiation_info')) {
52 53
    $info = array(
      'callbacks' => array(
54
        'negotiation' => 'language_test_language_negotiation_method',
55
      ),
56
      'file' => drupal_get_path('module', 'language_test') .'/language_test.module',
57
      'weight' => -10,
58
      'description' => t('This is a test language negotiation method.'),
59
    );
60 61

    return array(
62
      'test_language_negotiation_method' => array(
63 64 65
        'name' => t('Test'),
        'types' => array(LANGUAGE_TYPE_CONTENT, 'test_language_type', 'fixed_test_language_type'),
      ) + $info,
66
      'test_language_negotiation_method_ts' => array(
67 68 69 70 71 72 73 74 75 76
        'name' => t('Type-specific test'),
        'types' => array('test_language_type'),
      ) + $info,
    );
  }
}

/**
 * Implements hook_language_negotiation_info_alter().
 */
77
function language_test_language_negotiation_info_alter(array &$negotiation_info) {
78
  if (state()->get('language_test.language_negotiation_info_alter')) {
79
    unset($negotiation_info[LANGUAGE_NEGOTIATION_INTERFACE]);
80 81
  }
}
82 83 84 85

/**
 * Store the last negotiated languages.
 */
86
function language_test_store_language_negotiation() {
87
  $last = array();
88
  foreach (language_types_get_all() as $type) {
89
    $last[$type] = language($type)->langcode;
90
  }
91
  state()->set('language_test.language_negotiation_last', $last);
92 93 94
}

/**
95
 * Provides a test language negotiation method.
96
 */
97
function language_test_language_negotiation_method($languages) {
98 99
  return 'it';
}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

/**
 * Implements hook_menu().
 */
function language_test_menu() {
  $items = array();

  $items['language_test/subrequest'] = array(
    'page callback' => 'language_test_subrequest',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Page callback. Uses a subrequest to retrieve the 'user' page.
 */
function language_test_subrequest() {
  return drupal_container()->get('http_kernel')->handle(Request::create('/user'), HttpKernelInterface::SUB_REQUEST);
}