You are here

class LanguageTestController in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/language/tests/language_test/src/Controller/LanguageTestController.php \Drupal\language_test\Controller\LanguageTestController

Controller routines for language_test routes.

Hierarchy

Expanded class hierarchy of LanguageTestController

File

core/modules/language/tests/language_test/src/Controller/LanguageTestController.php, line 22
Contains \Drupal\language_test\Controller\LanguageTestController.

Namespace

Drupal\language_test\Controller
View source
class LanguageTestController implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The HTTP kernel service.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

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

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('http_kernel'), $container
      ->get('language_manager'));
  }

  /**
   * Route entity upcasting test helper.
   *
   * @param \Drupal\language\ConfigurableLanguageInterface $language
   *   The ConfigurableLanguage object from the route.
   *
   * @return string
   *   Testing feedback based on (translated) entity title.
   */
  public function testEntity(ConfigurableLanguageInterface $configurable_language) {
    return array(
      '#markup' => $this
        ->t('Loaded %label.', array(
        '%label' => $configurable_language
          ->label(),
      )),
    );
  }

  /**
   * Returns links to the current page with different langcodes.
   *
   * Using #type 'link' causes these links to be rendered with the link
   * generator.
   */
  public function typeLinkActiveClass() {

    // We assume that 'en' and 'fr' have been configured.
    $languages = $this->languageManager
      ->getLanguages();
    return array(
      'no_language' => array(
        '#type' => 'link',
        '#title' => t('Link to the current path with no langcode provided.'),
        '#url' => Url::fromRoute('<current>'),
        '#options' => array(
          'attributes' => array(
            'id' => 'no_lang_link',
          ),
          'set_active_class' => TRUE,
        ),
      ),
      'fr' => array(
        '#type' => 'link',
        '#title' => t('Link to a French version of the current path.'),
        '#url' => Url::fromRoute('<current>'),
        '#options' => array(
          'language' => $languages['fr'],
          'attributes' => array(
            'id' => 'fr_link',
          ),
          'set_active_class' => TRUE,
        ),
      ),
      'en' => array(
        '#type' => 'link',
        '#title' => t('Link to an English version of the current path.'),
        '#url' => Url::fromRoute('<current>'),
        '#options' => array(
          'language' => $languages['en'],
          'attributes' => array(
            'id' => 'en_link',
          ),
          'set_active_class' => TRUE,
        ),
      ),
    );
  }

  /**
   * 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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageTestController::$httpKernel protected property The HTTP kernel service.
LanguageTestController::$languageManager protected property The language manager service.
LanguageTestController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LanguageTestController::testEntity public function Route entity upcasting test helper.
LanguageTestController::testSubRequest public function Uses a sub request to retrieve the 'user' page.
LanguageTestController::typeLinkActiveClass public function Returns links to the current page with different langcodes.
LanguageTestController::__construct public function Constructs a new LanguageTestController object.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.