LanguageTestController.php 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\language_test\Controller\LanguageTestController.
 */

namespace Drupal\language_test\Controller;

10
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12 13
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
14 15 16 17

/**
 * Controller routines for language_test routes.
 */
18
class LanguageTestController implements ContainerInjectionInterface {
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  /**
   * The HTTP kernel service.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * Constructs a new LanguageTestController object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
   *   An HTTP kernel.
   */
  public function __construct(HttpKernelInterface $httpKernel) {
    $this->httpKernel = $httpKernel;
  }

37
  /**
38
   * {@inheritdoc}
39 40
   */
  public static function create(ContainerInterface $container) {
41
    return new static($container->get('http_kernel'));
42 43 44 45 46
  }

  /**
   * Returns links to the current page with different langcodes.
   *
47
   * Using #type 'link' causes these links to be rendered with l().
48
   */
49
  public function typeLinkActiveClass() {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    // We assume that 'en' and 'fr' have been configured.
    $languages = language_list();
    return array(
      'no_language' => array(
        '#type' => 'link',
        '#title' => t('Link to the current path with no langcode provided.'),
        '#href' => current_path(),
        '#options' => array(
          'attributes' => array(
            'id' => 'no_lang_link',
          ),
        ),
      ),
      'fr' => array(
        '#type' => 'link',
        '#title' => t('Link to a French version of the current path.'),
        '#href' => current_path(),
        '#options' => array(
          'language' => $languages['fr'],
          'attributes' => array(
            'id' => 'fr_link',
          ),
        ),
      ),
      'en' => array(
        '#type' => 'link',
        '#title' => t('Link to an English version of the current path.'),
        '#href' => current_path(),
        '#options' => array(
          'language' => $languages['en'],
          'attributes' => array(
            'id' => 'en_link',
          ),
        ),
      ),
    );
  }

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  /**
   * Uses a sub request to retrieve the 'user' page.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The kernels response to the sub request.
   */
  public function testSubRequest() {
    $request = Request::createFromGlobals();
    $server = $request->server->all();
    if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
      // We need this for when the test is executed by run-tests.sh.
      // @todo Remove this once run-tests.sh has been converted to use a Request
      //   object.
      $server['SCRIPT_FILENAME'] = $server['SCRIPT_NAME'];
      $base_path = ltrim($server['REQUEST_URI'], '/');
    }
    else {
      $base_path = $request->getBasePath();
    }
    $sub_request = Request::create($base_path . '/user', 'GET', $request->query->all(), $request->cookies->all(), array(), $server);
    return $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
  }

111
}