class LanguageTestController

Same name in other branches
  1. 9 core/modules/language/tests/language_test/src/Controller/LanguageTestController.php \Drupal\language_test\Controller\LanguageTestController
  2. 10 core/modules/language/tests/language_test/src/Controller/LanguageTestController.php \Drupal\language_test\Controller\LanguageTestController
  3. 11.x 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 17

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 $configurable_language
     *   The ConfigurableLanguage object from the route.
     *
     * @return string
     *   Testing feedback based on (translated) entity title.
     */
    public function testEntity(ConfigurableLanguageInterface $configurable_language) {
        return [
            '#markup' => $this->t('Loaded %label.', [
                '%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 [
            'no_language' => [
                '#type' => 'link',
                '#title' => t('Link to the current path with no langcode provided.'),
                '#url' => Url::fromRoute('<current>'),
                '#options' => [
                    'attributes' => [
                        'id' => 'no_lang_link',
                    ],
                    'set_active_class' => TRUE,
                ],
            ],
            'fr' => [
                '#type' => 'link',
                '#title' => t('Link to a French version of the current path.'),
                '#url' => Url::fromRoute('<current>'),
                '#options' => [
                    'language' => $languages['fr'],
                    'attributes' => [
                        'id' => 'fr_link',
                    ],
                    'set_active_class' => TRUE,
                ],
            ],
            'en' => [
                '#type' => 'link',
                '#title' => t('Link to an English version of the current path.'),
                '#url' => Url::fromRoute('<current>'),
                '#options' => [
                    'language' => $languages['en'],
                    'attributes' => [
                        '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(), [], $server);
        return $this->httpKernel
            ->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title 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 &#039;user&#039; 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.

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