function template_preprocess_uikit_view_accordion in UIkit Components 8.2
Same name and namespace in other branches
- 8.3 uikit_views/includes/uikit_views.theme.inc \template_preprocess_uikit_view_accordion()
- 7.3 uikit_views/templates/uikit_views.theme.inc \template_preprocess_uikit_view_accordion()
- 7.2 uikit_views/templates/uikit_views.theme.inc \template_preprocess_uikit_view_accordion()
Prepares variables for UIkit Accordion templates.
Default template: uikit-view-accordion.html.twig.
Parameters
array $variables: An associative array containing:
- view: A ViewExecutable object.
- rows: The raw row data.
1 string reference to 'template_preprocess_uikit_view_accordion'
- UIkitViews::getThemeHooks in uikit_views/
src/ UIkitViews.php - Returns the theme hook definition information for UIkit Views.
File
- uikit_views/
includes/ uikit_views.theme.inc, line 22 - Preprocessors and helper functions to make theming easier.
Code
function template_preprocess_uikit_view_accordion(array &$variables) {
$variables['#attached']['library'][] = UIkit::getUIkitComponent('accordion');
$view = $variables['view'];
$options = $view->style_plugin->options;
$title_field = $options['title_field'];
$accordion_data = [];
$items = [];
if ($title_field) {
foreach ($variables['rows'] as $result_index => $item) {
$title = $view->style_plugin
->getFieldValue($result_index, $title_field);
$items[$result_index]['content'] = $item;
$items[$result_index]['title'] = $title;
}
}
// Set items array for twig template.
$variables['items'] = $items;
// Create attributes for accordion.
$accordion_data[] = 'showfirst: ' . ($options['showfirst'] ? 'true' : 'false');
$accordion_data[] = 'collapse: ' . ($options['collapse'] ? 'true' : 'false');
$accordion_data[] = 'animate: ' . ($options['animate'] ? 'true' : 'false');
$accordion_data[] = "easing: '" . $options['easing'] . "'";
$accordion_data[] = 'duration: ' . $options['duration'];
$accordion_data[] = "toggle: '" . $options['toggle'] . "'";
$accordion_data[] = "containers: '" . $options['containers'] . "'";
$accordion_data[] = "clsactive: '" . $options['clsactive'] . "'";
$data_accordion = '{' . implode(', ', $accordion_data) . '}';
$accordion_attributes = new Attribute();
$accordion_attributes
->addClass('uk-accordion');
$accordion_attributes
->setAttribute('data-uk-accordion', $data_accordion);
// Set accordion attributes for twig template.
$variables['accordion_attributes'] = $accordion_attributes;
// Set the accordion toggle and container elements.
$selectors = '/[.#\\[\\]]/i';
$variables['accordion_toggle'] = preg_replace($selectors, '', $options['toggle']);
$variables['accordion_container'] = preg_replace($selectors, '', $options['containers']);
}