improvements-2.x-dev/modules/improvements_views/improvements_views.module
modules/improvements_views/improvements_views.module
<?php use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\Element; use Drupal\field\FieldStorageConfigInterface; use Drupal\improvements_views\Plugin\views\argument_default\ExtendedQueryParameter; use Drupal\improvements_views\Plugin\views\display_extender\ImprovementsDisplayExtender; use Drupal\improvements_views\Plugin\views\filter\ExtendedDateFilter; use Drupal\improvements_views\Plugin\views\filter\ExtendedNumericFilter; use Drupal\views\ViewExecutable; use Drupal\views\Views; use Drupal\views_ui\ViewUI; /** * Implements hook_views_plugins_filter_alter(). */ function improvements_views_views_plugins_filter_alter(array &$plugins): void { $plugins['numeric']['class'] = ExtendedNumericFilter::class; $plugins['date']['class'] = ExtendedDateFilter::class; } /** * Implements hook_views_plugins_argument_default_alter(). */ function improvements_views_views_plugins_argument_default_alter(array &$plugins): void { $plugins['query_parameter']['class'] = ExtendedQueryParameter::class; } /** * Implements hook_field_views_data_alter(). */ function improvements_views_field_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage): void { // Add Views contextual filter "with children" for term fields if ($field_storage->getType() == 'entity_reference' && $field_storage->getSetting('target_type') == 'taxonomy_term') { foreach ($data as $table_name => $table_data) { foreach ($table_data as $field_name => $field_data) { if (isset($field_data['argument']) && $field_data['argument']['id'] == 'entity_target_id' && $field_name != 'delta') { $data[$table_name][$field_name . '_with_children'] = $field_data; $field = &$data[$table_name][$field_name . '_with_children']; $field['title'] .= ' (' . t('With children') . ')'; $field['argument']['id'] = 'term_with_children'; unset($field['filter']); unset($field['sort']); } } } } } /** * Implements hook_views_data(). */ function improvements_views_views_data(): array { $data['views']['block_plugin_area'] = [ 'title' => t('Block by plugin id'), 'help' => t('Displays block by plugin id.'), 'area' => [ 'id' => 'block_plugin_area', ], ]; return $data; } /** * Implements hook_views_data_alter(). */ function improvements_views_views_data_alter(array &$data): void { if (isset($data['node_field_data'])) { $data['node_field_data']['uid_is_current_user'] = [ 'title' => t('Author is current user'), 'title short' => t('Author is current user'), 'real field' => 'uid', 'sort' => [ 'id' => 'is_current_user', ], ]; } if (isset($data['comment_field_data'])) { $data['comment_field_data']['uid_is_current_user'] = [ 'title' => t('Author is current user'), 'title short' => t('Author is current user'), 'real field' => 'uid', 'sort' => [ 'id' => 'is_current_user', ], ]; } } /** * Implements hook_token_info(). */ function improvements_views_token_info(): array { $info = []; $info['tokens']['view']['total-rows-in-current-page'] = [ 'name' => t('Total rows in current page'), 'description' => t('Total rows in current page'), ]; return $info; } /** * Implements hook_tokens(). */ function improvements_views_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata): array { $replacements = []; // [view:*] if ($type == 'view') { foreach ($tokens as $name => $original) { if ($name == 'total-rows-in-current-page') { $view_executable = $data['view']; /** @var ViewExecutable $view_executable */ $replacements[$original] = count($view_executable->result); } } } return $replacements; } /** * Preprocess function for views-view.html.twig. */ function improvements_views_preprocess_views_view(array &$vars): void { $view_executable = $vars['view']; /** @var ViewExecutable $view_executable */ $view_display_extenders = $view_executable->getDisplay()->getExtenders(); if (!isset($vars['content_attributes'])) { $vars['content_attributes'] = []; } // Add css-class to content wrapper if (isset($view_display_extenders['improvements_display_extender'])) { $improvements_display_extender = $view_display_extenders['improvements_display_extender']; /** @var ImprovementsDisplayExtender $improvements_display_extender */ if (!empty($improvements_display_extender->options['content_css_class'])) { $view_content_css_class = $improvements_display_extender->options['content_css_class']; // Replace [view:*] tokens if (str_contains($view_content_css_class, '[view:')) { $view_content_css_class = \Drupal::token()->replace($view_content_css_class, [ 'view' => $view_executable, ]); } $vars['content_attributes']['class'][] = $view_content_css_class; } } } /** * Implements hook_views_ui_display_top_alter(). */ function improvements_views_views_ui_display_top_alter(array &$build, ViewUI $view, string $display_id): void { foreach (Element::children($build['tabs']) as $key) { $display = $view->getDisplay($key); $build['tabs'][$key]['#link']['title'] = '(' . $display['display_plugin'] . ') ' . $build['tabs'][$key]['#link']['title']; } } /** * Implements hook_page_ROUTE_NAME_result_alter(): entity.view.collection. */ function improvements_views_page_entity_view_collection_result_alter(array &$result): void { // Add autofocus to filter input if (isset($result['filters']['text'])) { $result['filters']['text']["#attributes"]['autofocus'] = 'autofocus'; } } /** * Preprocess function for html.html.twig. */ function improvements_views_views_preprocess_html(array &$vars): void { $current_route = \Drupal::routeMatch()->getRouteObject(); $current_route_defaults = $current_route->getDefaults(); if (isset($current_route_defaults['_controller']) && $current_route_defaults['_controller'] == 'Drupal\views\Routing\ViewPageController::handle') { $view = Views::getView($current_route_defaults['view_id']); $view->setDisplay($current_route_defaults['display_id']); $view_display_extenders = $view->getDisplay()->getExtenders(); if (isset($view_display_extenders['improvements_display_extender'])) { $improvements_display_extender = $view_display_extenders['improvements_display_extender']; /** @var ImprovementsDisplayExtender $improvements_display_extender */ if (!empty($improvements_display_extender->options['page_body_css_class'])) { $vars['attributes']['class'] = array_merge( $vars['attributes']['class'] ?? [], explode(' ', $improvements_display_extender->options['page_body_css_class']) ); } } } }