JsOnceTestController.php in Drupal 9
File
core/modules/system/tests/modules/js_once_test/src/Controller/JsOnceTestController.php
View source
<?php
namespace Drupal\js_once_test\Controller;
use Drupal\Core\Controller\ControllerBase;
class JsOnceTestController extends ControllerBase {
public function onceTest() {
$output = [
'#attached' => [
'library' => [
'core/once',
],
],
];
foreach (range(1, 5) as $item) {
$output['item' . $item] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => 'Item ' . $item,
'#attributes' => [
'data-drupal-item' => $item,
],
];
}
return $output;
}
public function onceBcTest() {
$output = [
'#attached' => [
'library' => [
'core/jquery.once',
],
],
];
foreach (range(1, 5) as $item) {
$output['item' . $item] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => 'Item ' . $item,
'#attributes' => [
'data-drupal-item' => $item,
],
];
}
return $output;
}
}