views_collapsible_list-8.x-1.x-dev/views_collapsible_list.module
views_collapsible_list.module
<?php
/**
* @file
* Hook implementations for the Views Collapsible List module.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function views_collapsible_list_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.views_collapsible_list':
$text = file_get_contents(dirname(__FILE__) . "/README.md");
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . $text . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
return NULL;
}
/**
* Prepares variables for Views collapsible list templates.
*
* Default template: views-view-collapsible-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A View object.
*/
function template_preprocess_views_view_collapsible_list(array &$variables) {
// We inherit from the normal list style, so we also need all those variables
// populated.
template_preprocess_views_view_list($variables);
// Add the client side portion that makes it work.
$variables['view']->element['#attached']['library'][] = 'views_collapsible_list/collapse';
$style_options = $variables['view']->style_plugin->options;
$fields = isset($style_options['collapsible_fields']) ? array_values(array_filter($style_options['collapsible_fields'])) : [];
$fields = array_map(function ($a) {
return '.views-field-' . Html::cleanCssIdentifier($a);
}, $fields);
$variables['view']->element['#attached']['drupalSettings']['viewsCollapsibleList']['fields'] = $fields;
// Create a unique class name for each SUN section on the list to
// collapse/expand each section individually.
$time = new DateTime();
$variables['section_class'] = 'btn--' . ($time->getTimestamp() + (rand(1, 9999)));
}
