$renderer = \Drupal::
service('renderer'
);
$build['#attached'
]['library'
][] = 'ajax_test/order-css-command';
$assets = AttachedAssets::
createFromRenderArray($build);
$css_render_array =
$css_collection_renderer->
render($asset_resolver->
getCssAssets($assets, FALSE, \Drupal::
languageManager()->
getCurrentLanguage()));
$expected_commands[1
] =
new AddCssCommand(array_column($css_render_array, '#attributes'
));
$build['#attached'
]['library'
][] = 'ajax_test/order-header-js-command';
$build['#attached'
]['library'
][] = 'ajax_test/order-footer-js-command';
$assets = AttachedAssets::
createFromRenderArray($build);
[$js_assets_header,
$js_assets_footer] =
$asset_resolver->
getJsAssets($assets, FALSE, \Drupal::
languageManager()->
getCurrentLanguage());
$js_header_render_array =
$js_collection_renderer->
render($js_assets_header);
$js_footer_render_array =
$js_collection_renderer->
render($js_assets_footer);
$expected_commands[2
] =
new AddJsCommand(array_column($js_header_render_array, '#attributes'
), 'head'
);
$expected_commands[3
] =
new AddJsCommand(array_column($js_footer_render_array, '#attributes'
));
$expected_commands[4
] =
new HtmlCommand('body', 'Hello, world!'
);
// Verify AJAX command order — this should always be the order:
// 1. CSS files
// 2. JavaScript files in the header
// 3. JavaScript files in the footer
// 4. Any other AJAX commands, in whatever order they were added.
$commands =
$this->
drupalGetAjax('ajax-test/order'
);
$this->
assertCommand(array_slice($commands, 0, 1
),
$expected_commands[1
]->
render());
$this->
assertCommand(array_slice($commands, 1, 1
),
$expected_commands[2
]->
render());