You are here

public function JsExampleController::getJsAccordionImplementation in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/js_example/src/Controller/JsExampleController.php \Drupal\js_example\Controller\JsExampleController::getJsAccordionImplementation()

Accordion page implementation.

We're allowing a twig template to define our content in this case, which isn't normally how things work, but it's easier to demonstrate the JavaScript this way.

Return value

array A renderable array.

1 string reference to 'JsExampleController::getJsAccordionImplementation'
js_example.routing.yml in js_example/js_example.routing.yml
js_example/js_example.routing.yml

File

js_example/src/Controller/JsExampleController.php, line 98

Class

JsExampleController
Controller for Hooks example description page.

Namespace

Drupal\js_example\Controller

Code

public function getJsAccordionImplementation() {
  $title = $this
    ->t('Click sections to expand or collapse:');

  // Build using our theme. This gives us content, which is not a good
  // practice, but which allows us to demonstrate adding JavaScript here.
  $build['myelement'] = [
    '#theme' => 'js_example_accordion',
    '#title' => $title,
  ];

  // Add our script. It is tiny, but this demonstrates how to add it. We pass
  // our module name followed by the internal library name declared in
  // libraries yml file.
  $build['myelement']['#attached']['library'][] = 'js_example/js_example.accordion';

  // Return the renderable array.
  return $build;
}