visualn-8.x-1.x-dev/modules/visualn_views/visualn_views.module
modules/visualn_views/visualn_views.module
<?php
/**
* @file
* Contains visualn_views.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\visualn\Entity\VisualNStyle;
use Drupal\Core\Template\Attribute;
use Drupal\views\ViewExecutable;
/**
* Implements hook_help().
*/
function visualn_views_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the visualn_views module.
case 'help.page.visualn_views':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Enables VisualN support for Views') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_views_pre_build().
*/
function visualn_views_views_pre_build(ViewExecutable $view) {
// @todo: check display style type
// enable exposed form for the view even if there are no exposed handlers
// @see ViewExecutable::build() and visualn_form_alter()
// @todo: this check is requried sometimes, e.g. for full entities view
// though it is strange (?)
if (!empty($view->style_plugin)) {
$options = $view->style_plugin->options;
if (isset($options['expose_keys_mapping']) && $options['expose_keys_mapping'] == 1) {
$view->display_handler->has_exposed = TRUE;
}
}
}
/**
* Implements hook_form_alter().
*/
function visualn_views_form_alter(&$form, $form_state, $form_id) {
// @todo: maybe use display extender
// @todo: this is temporary solution
// @todo: check style plugin type, should be visualn
if ($form_id == 'views_exposed_form') {
$displayHandler = $form_state->getStorage()['view']->getDisplay();
$style_plugin = $form_state->getStorage()['view']->style_plugin;
$options = $style_plugin->options;
if (isset($options['expose_keys_mapping']) && $options['expose_keys_mapping'] == 1) {
// get visualn style drawer keys and prepare mappings form
$visualn_style_id = $options['visualn_style_id'];
if ($visualn_style_id) {
$visualn_style = \Drupal::service('entity_type.manager')->getStorage('visualn_style')->load($visualn_style_id);
$drawer_plugin = $visualn_style->getDrawerPlugin();
$drawer_config = $options['drawer_config'];
$drawer_plugin->setConfiguration($drawer_config);
$data_keys = $drawer_plugin->dataKeys();
if (!empty($data_keys)) {
// @todo: form key should be unique to avoid conflicts with exposed handlers keys
$form['drawer_fields'] = [
'#type' => 'table',
'#header' => [t('Data key'), t('Field')],
];
$field_names = $displayHandler->getFieldLabels();
foreach ($data_keys as $data_key) {
$form['drawer_fields'][$data_key]['label'] = [
'#plain_text' => $data_key,
];
$form['drawer_fields'][$data_key]['field'] = [
'#type' => 'select',
'#options' => $field_names,
'#default_value' => isset($options['drawer_fields'][$data_key]) ? $options['drawer_fields'][$data_key] : '',
];
}
}
}
}
}
}
//function template_preprocess_views_view_visualn(&$variables) {}
