function ThemingPageController::list

Same name in other branches
  1. 3.x modules/theming_example/src/Controller/ThemingPageController.php \Drupal\theming_example\Controller\ThemingPageController::list()

The list page callback.

An example page where the output is supplied as an array which is themed into a list and styled with css.

In this case we'll use the core-provided theme_item_list as a #theme_wrapper. Any theme need only override theme_item_list to change the behavior.

1 string reference to 'ThemingPageController::list'
theming_example.routing.yml in modules/theming_example/theming_example.routing.yml
modules/theming_example/theming_example.routing.yml

File

modules/theming_example/src/Controller/ThemingPageController.php, line 62

Class

ThemingPageController
Controller for theming example routes.

Namespace

Drupal\theming_example\Controller

Code

public function list() {
    $items = [
        $this->t('First item'),
        $this->t('Second item'),
        $this->t('Third item'),
        $this->t('Fourth item'),
    ];
    // First we'll create a render array that simply uses theme_item_list.
    $title = $this->t("A list returned to be rendered using theme('item_list')");
    $build['render_version'] = [
        // We use #theme here instead of #theme_wrappers because theme_item_list()
        // is the classic type of theme function that does not just assume a
        // render array, but instead has its own properties (#type, #title, #items).
'#theme' => 'item_list',
        // '#type' => 'ul',  // The default type is 'ul'
        // We can easily make sure that a css or js file is present using #attached.
'#attached' => [
            'library' => [
                'theming_example/list',
            ],
        ],
        '#title' => $title,
        '#items' => $items,
        '#attributes' => [
            'class' => [
                'render-version-list',
            ],
        ],
    ];
    // Now we'll create a render array which uses our own list formatter,
    // theme('theming_example_list').
    $title = $this->t("The same list rendered by theme('theming_example_list')");
    $build['our_theme_function'] = [
        '#theme' => 'theming_example_list',
        '#attached' => [
            'library' => [
                'theming_example/list',
            ],
        ],
        '#title' => $title,
        '#items' => $items,
    ];
    return $build;
}