class CommonTestController

Same name in other branches
  1. 9 core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php \Drupal\common_test\Controller\CommonTestController
  2. 8.9.x core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php \Drupal\common_test\Controller\CommonTestController
  3. 10 core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php \Drupal\common_test\Controller\CommonTestController

Controller routines for common_test routes.

Hierarchy

Expanded class hierarchy of CommonTestController

File

core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php, line 16

Namespace

Drupal\common_test\Controller
View source
class CommonTestController {
    use StringTranslationTrait;
    
    /**
     * Returns links to the current page, with and without query strings.
     *
     * Using #type 'link' causes these links to be rendered with the link
     * generator.
     */
    public function typeLinkActiveClass() {
        return [
            'no_query' => [
                '#type' => 'link',
                '#title' => $this->t('Link with no query string'),
                '#url' => Url::fromRoute('<current>'),
                '#options' => [
                    'set_active_class' => TRUE,
                ],
            ],
            'with_query' => [
                '#type' => 'link',
                '#title' => $this->t('Link with a query string'),
                '#url' => Url::fromRoute('<current>'),
                '#options' => [
                    'query' => [
                        'foo' => 'bar',
                        'one' => 'two',
                    ],
                    'set_active_class' => TRUE,
                ],
            ],
            'with_query_reversed' => [
                '#type' => 'link',
                '#title' => $this->t('Link with the same query string in reverse order'),
                '#url' => Url::fromRoute('<current>'),
                '#options' => [
                    'query' => [
                        'one' => 'two',
                        'foo' => 'bar',
                    ],
                    'set_active_class' => TRUE,
                ],
            ],
        ];
    }
    
    /**
     * Adds a JavaScript file and a CSS file with a query string appended.
     *
     * @return string
     *   An empty string.
     */
    public function jsAndCssQuerystring() {
        $module_extension_list = \Drupal::service('extension.list.module');
        assert($module_extension_list instanceof ExtensionList);
        $attached = [
            '#attached' => [
                'library' => [
                    'node/drupal.node',
                ],
                'css' => [
                    $module_extension_list->getPath('node') . '/css/node.admin.css' => [],
                    // A relative URI may have a query string.
'/' . $module_extension_list->getPath('node') . '/node-fake.css?arg1=value1&arg2=value2' => [],
                ],
            ],
        ];
        return \Drupal::service('renderer')->renderRoot($attached);
    }
    
    /**
     * Prints a destination query parameter.
     *
     * @return \Symfony\Component\HttpFoundation\Response
     *   A new Response object containing a string with the destination query
     *   parameter.
     */
    public function destination() {
        $destination = \Drupal::destination()->getAsArray();
        $output = "The destination: " . Html::escape($destination['destination']);
        return new Response($output);
    }
    
    /**
     * Returns a response with early rendering in common_test_page_attachments.
     *
     * @return \Symfony\Component\HttpFoundation\Response
     *   A new Response object.
     */
    public function attachments() {
        \Drupal::state()->set('common_test.hook_page_attachments.early_rendering', TRUE);
        $build = [
            '#title' => 'A title',
            'content' => [
                '#markup' => 'Some content',
            ],
        ];
        return \Drupal::service('main_content_renderer.html')->renderResponse($build, \Drupal::requestStack()->getCurrentRequest(), \Drupal::routeMatch());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
CommonTestController::attachments public function Returns a response with early rendering in common_test_page_attachments.
CommonTestController::destination public function Prints a destination query parameter.
CommonTestController::jsAndCssQuerystring public function Adds a JavaScript file and a CSS file with a query string appended.
CommonTestController::typeLinkActiveClass public function Returns links to the current page, with and without query strings.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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. 1

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