views_jump_menu.module in Views Jump Menu 8
Contains views_jump_menu.module.
File
views_jump_menu.moduleView source
<?php
/**
* @file
* Contains views_jump_menu.module.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
/**
* Implements hook_help().
*/
function views_jump_menu_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.views_jump_menu':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Displays Views results as a Select list that links to an entity when clicked.') . '</p>';
return $output;
}
return '';
}
/**
* Implements hook_theme().
*/
function views_jump_menu_theme($existing, $type, $theme, $path) {
return [
'views_jump_menu' => [
'variables' => [
'view' => NULL,
'rows' => NULL,
],
],
];
}
/**
* Gets classes for the jump menu select element.
*
* @param array $options
* Options passed in for the select element.
*
* @return array
* A cleaned array of class names.
*/
function _views_jump_menu_get_select_classes(array $options) {
$class = $options['class'] ?: '';
$class = explode(' ', $class);
$class[] = 'ViewsJumpMenu';
$class[] = 'js-viewsJumpMenu';
return array_map('\\Drupal\\Component\\Utility\\Html::cleanCssIdentifier', $class);
}
/**
* Implements template_preprocess_views_jump_menu().
*/
function views_jump_menu_preprocess_views_jump_menu(&$variables) {
/** @var Drupal\views\ViewExecutable $view */
$view = $variables['view'];
/** @var Drupal\views\ResultRow[] $rows */
$rows = $variables['rows'];
$options = $view->style_plugin->options;
if ($options['wrapper_class']) {
$variables['attributes']['class'][] = $options['wrapper_class'];
}
// Generate an HTML ID for this view. We need this so that multiple jump menus
// on the same page can have independent drupalSettings.
$view_id = Html::getUniqueId(implode('-', [
$view
->id(),
$view->current_display,
'jump_menu',
]));
// Pass the setting about whether to open links in a new window to the front-
// end.
$variables['#attached']['drupalSettings']['viewsJumpMenu'][$view_id]['new_window'] = $options['new_window'] ?? FALSE;
$select = [
'select_text' => $options['select_text'],
'select_label' => $options['select_label'],
'attributes' => new Attribute([
'class' => _views_jump_menu_get_select_classes($options),
'title' => $options['select_text'],
'id' => $view_id,
]),
'options' => [],
];
if (!empty($options['label_field'])) {
/**
* @var string $id
* @var Drupal\views\ResultRow $row
*/
foreach ($rows as $id => $row) {
$view->row_index = $row->index;
// Make sure each field handler has cached the right contents before
// rendering them in the select field.
foreach ([
'label_field',
'url_field',
] as $fieldType) {
/** @var \Drupal\views\Plugin\views\field\FieldHandlerInterface $handler */
foreach ($view->field[$options[$fieldType]]->displayHandler
->getHandlers('field') as $handler) {
$handler
->advancedRender($row);
}
}
$select['options'][$id] = [
'label' => Html::decodeEntities(strip_tags($view->field[$options['label_field']]
->advancedRender($row))),
'url' => $view->field[$options['url_field']]
->advancedRender($row),
];
}
}
$variables['select'] = $select;
}
Functions
Name | Description |
---|---|
views_jump_menu_help | Implements hook_help(). |
views_jump_menu_preprocess_views_jump_menu | Implements template_preprocess_views_jump_menu(). |
views_jump_menu_theme | Implements hook_theme(). |
_views_jump_menu_get_select_classes | Gets classes for the jump menu select element. |