Chris@0: storage->id(); Chris@0: Chris@0: $variables['css_name'] = Html::cleanCssIdentifier($id); Chris@0: $variables['id'] = $id; Chris@0: $variables['display_id'] = $view->current_display; Chris@0: // Override the title to be empty by default. For example, if viewing a page Chris@0: // view, 'title' will already be populated in $variables. This can still be Chris@0: // overridden to use a title when needed. See views_ui_preprocess_views_view() Chris@0: // for an example of this. Chris@0: $variables['title'] = ''; Chris@0: Chris@0: $css_class = $view->display_handler->getOption('css_class'); Chris@0: if (!empty($css_class)) { Chris@17: // Views uses its own sanitization method. This is preserved to keep Chris@17: // backwards compatibility. Chris@17: // @todo https://www.drupal.org/project/drupal/issues/2977950 Decide what to Chris@17: // do with the backwards compatibility layer. Chris@17: $bc_classes = explode(' ', preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class)); Chris@17: // Sanitize the classes using the classes using the proper API. Chris@17: $sanitized_classes = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', explode(' ', $css_class)); Chris@17: $view_classes = array_unique(array_merge($bc_classes, $sanitized_classes)); Chris@17: // Merge the view display classes into any existing classes if they exist. Chris@17: $variables['attributes']['class'] = !empty($variables['attributes']['class']) ? array_merge($variables['attributes']['class'], $view_classes) : $view_classes; Chris@17: $variables['css_class'] = implode(' ', $view_classes); Chris@0: } Chris@0: Chris@0: // contextual_preprocess() only works on render elements, and since this theme Chris@0: // hook is not for a render element, contextual_preprocess() falls back to the Chris@0: // first argument and checks if that is a render element. The first element is Chris@0: // view_array. However, view_array does not get set anywhere, but since we do Chris@0: // have access to the View object, we can also access the View object's Chris@0: // element, which is a render element that does have #contextual_links set if Chris@0: // the display supports it. Doing this allows contextual_preprocess() to Chris@0: // access this theme hook's render element, and therefore allows this template Chris@0: // to have contextual links. Chris@0: // @see views_theme() Chris@0: $variables['view_array'] = $variables['view']->element; Chris@0: Chris@0: // Attachments are always updated with the outer view, never by themselves, Chris@0: // so they do not have dom ids. Chris@0: if (empty($view->is_attachment)) { Chris@0: // Our JavaScript needs to have some means to find the HTML belonging to Chris@0: // this view. Chris@0: // Chris@0: // It is true that the DIV wrapper has classes denoting the name of the view Chris@0: // and its display ID, but this is not enough to unequivocally match a view Chris@0: // with its HTML, because one view may appear several times on the page. So Chris@0: // we set up a hash with the current time, $dom_id, to issue a "unique" Chris@0: // identifier for each view. This identifier is written to both Chris@0: // drupalSettings and the DIV wrapper. Chris@0: $variables['dom_id'] = $view->dom_id; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views fields templates. Chris@0: * Chris@0: * Default template: views-view-fields.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: The view object. Chris@0: * - options: An array of options. Each option contains: Chris@0: * - inline: An array that contains the fields that are to be Chris@0: * displayed inline. Chris@0: * - default_field_elements: If default field wrapper Chris@0: * elements are to be provided. Chris@0: * - hide_empty: Whether the field is to be hidden if empty. Chris@0: * - element_default_classes: If the default classes are to be added. Chris@0: * - separator: A string to be placed between inline fields to keep them Chris@0: * visually distinct. Chris@0: * - row: An array containing information about the current row. Chris@0: */ Chris@0: function template_preprocess_views_view_fields(&$variables) { Chris@0: $view = $variables['view']; Chris@0: Chris@0: // Loop through the fields for this view. Chris@0: $previous_inline = FALSE; Chris@0: // Ensure it's at least an empty array. Chris@0: $variables['fields'] = []; Chris@0: /** @var \Drupal\views\ResultRow $row */ Chris@0: $row = $variables['row']; Chris@0: foreach ($view->field as $id => $field) { Chris@0: // render this even if set to exclude so it can be used elsewhere. Chris@0: $field_output = $view->style_plugin->getField($row->index, $id); Chris@0: $empty = $field->isValueEmpty($field_output, $field->options['empty_zero']); Chris@0: if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($variables['options']['hide_empty'])))) { Chris@0: $object = new stdClass(); Chris@0: $object->handler = $view->field[$id]; Chris@0: $object->inline = !empty($variables['options']['inline'][$id]); Chris@0: // Set up default value of the flag that indicates whether to display a Chris@0: // colon after the label. Chris@0: $object->has_label_colon = FALSE; Chris@0: Chris@0: $object->element_type = $object->handler->elementType(TRUE, !$variables['options']['default_field_elements'], $object->inline); Chris@0: if ($object->element_type) { Chris@0: $attributes = []; Chris@0: if ($object->handler->options['element_default_classes']) { Chris@0: $attributes['class'][] = 'field-content'; Chris@0: } Chris@0: Chris@0: if ($classes = $object->handler->elementClasses($row->index)) { Chris@0: $attributes['class'][] = $classes; Chris@0: } Chris@0: $object->element_attributes = new Attribute($attributes); Chris@0: } Chris@0: Chris@0: $object->content = $field_output; Chris@0: if (isset($view->field[$id]->field_alias) && isset($row->{$view->field[$id]->field_alias})) { Chris@0: $object->raw = $row->{$view->field[$id]->field_alias}; Chris@0: } Chris@0: else { Chris@0: // Make sure it exists to reduce NOTICE. Chris@0: $object->raw = NULL; Chris@0: } Chris@0: Chris@0: if (!empty($variables['options']['separator']) && $previous_inline && $object->inline && $object->content) { Chris@0: $object->separator = Xss::filterAdmin($variables['options']['separator']); Chris@0: } Chris@0: Chris@0: $object->class = Html::cleanCssIdentifier($id); Chris@0: Chris@0: $previous_inline = $object->inline; Chris@0: // Set up field wrapper element. Chris@0: $object->wrapper_element = $object->handler->elementWrapperType(TRUE, TRUE); Chris@0: if ($object->wrapper_element === '' && $variables['options']['default_field_elements']) { Chris@0: $object->wrapper_element = $object->inline ? 'span' : 'div'; Chris@0: } Chris@0: Chris@0: // Set up field wrapper attributes if field wrapper was set. Chris@0: if ($object->wrapper_element) { Chris@0: $attributes = []; Chris@0: if ($object->handler->options['element_default_classes']) { Chris@0: $attributes['class'][] = 'views-field'; Chris@0: $attributes['class'][] = 'views-field-' . $object->class; Chris@0: } Chris@0: Chris@0: if ($classes = $object->handler->elementWrapperClasses($row->index)) { Chris@0: $attributes['class'][] = $classes; Chris@0: } Chris@0: $object->wrapper_attributes = new Attribute($attributes); Chris@0: } Chris@0: Chris@0: // Set up field label Chris@0: $object->label = $view->field[$id]->label(); Chris@0: Chris@0: // Set up field label wrapper and its attributes. Chris@0: if ($object->label) { Chris@0: // Add a colon in a label suffix. Chris@0: if ($object->handler->options['element_label_colon']) { Chris@0: $object->label_suffix = ': '; Chris@0: $object->has_label_colon = TRUE; Chris@0: } Chris@0: Chris@0: // Set up label HTML element. Chris@0: $object->label_element = $object->handler->elementLabelType(TRUE, !$variables['options']['default_field_elements']); Chris@0: Chris@0: // Set up label attributes. Chris@0: if ($object->label_element) { Chris@0: $attributes = []; Chris@0: if ($object->handler->options['element_default_classes']) { Chris@0: $attributes['class'][] = 'views-label'; Chris@0: $attributes['class'][] = 'views-label-' . $object->class; Chris@0: } Chris@0: Chris@0: // Set up field label. Chris@0: $element_label_class = $object->handler->elementLabelClasses($row->index); Chris@0: if ($element_label_class) { Chris@0: $attributes['class'][] = $element_label_class; Chris@0: } Chris@0: $object->label_attributes = new Attribute($attributes); Chris@0: } Chris@0: } Chris@0: Chris@0: $variables['fields'][$id] = $object; Chris@0: } Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views single grouping templates. Chris@0: * Chris@0: * Default template: views-view-grouping.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: The view object. Chris@0: * - rows: The rows returned from the view. Chris@0: * - grouping_level: Integer indicating the hierarchical level of the Chris@0: * grouping. Chris@0: * - content: The content to be grouped. Chris@0: * - title: The group heading. Chris@0: */ Chris@0: function template_preprocess_views_view_grouping(&$variables) { Chris@0: $variables['content'] = $variables['view']->style_plugin->renderGroupingSets($variables['rows'], $variables['grouping_level']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views field templates. Chris@0: * Chris@0: * Default template: views-view-field.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - field: The field handler object for the current field. Chris@0: * - row: Object representing the raw result of the SQL query for the current Chris@0: * field. Chris@0: * - view: Instance of the ViewExecutable object for the parent view. Chris@0: */ Chris@0: function template_preprocess_views_view_field(&$variables) { Chris@0: $variables['output'] = $variables['field']->advancedRender($variables['row']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views summary templates. Chris@0: * Chris@0: * The summary prints a single record from a row, with fields. Chris@0: * Chris@0: * Default template: views-view-summary.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: A ViewExecutable object. Chris@0: * - rows: The raw row data. Chris@0: */ Chris@0: function template_preprocess_views_view_summary(&$variables) { Chris@0: /** @var \Drupal\views\ViewExecutable $view */ Chris@0: $view = $variables['view']; Chris@0: $argument = $view->argument[$view->build_info['summary_level']]; Chris@0: Chris@0: $url_options = []; Chris@0: Chris@0: if (!empty($view->exposed_raw_input)) { Chris@0: $url_options['query'] = $view->exposed_raw_input; Chris@0: } Chris@0: Chris@0: $active_urls = [ Chris@0: // Force system path. Chris@18: Url::fromRoute('', [], ['alias' => TRUE])->toString(), Chris@0: // Force system path. Chris@0: Url::fromRouteMatch(\Drupal::routeMatch())->setOption('alias', TRUE)->toString(), Chris@0: // Could be an alias. Chris@18: Url::fromRoute('')->toString(), Chris@0: // Could be an alias. Chris@0: Url::fromRouteMatch(\Drupal::routeMatch())->toString(), Chris@0: ]; Chris@0: $active_urls = array_combine($active_urls, $active_urls); Chris@0: Chris@0: // Collect all arguments foreach row, to be able to alter them for example Chris@0: // by the validator. This is not done per single argument value, because this Chris@0: // could cause performance problems. Chris@0: $row_args = []; Chris@0: Chris@0: foreach ($variables['rows'] as $id => $row) { Chris@0: $row_args[$id] = $argument->summaryArgument($row); Chris@0: } Chris@0: $argument->processSummaryArguments($row_args); Chris@0: Chris@0: foreach ($variables['rows'] as $id => $row) { Chris@0: $variables['rows'][$id]->attributes = []; Chris@0: $variables['rows'][$id]->link = $argument->summaryName($row); Chris@0: $args = $view->args; Chris@0: $args[$argument->position] = $row_args[$id]; Chris@0: Chris@0: if (!empty($argument->options['summary_options']['base_path'])) { Chris@0: $base_path = $argument->options['summary_options']['base_path']; Chris@0: $tokens = $view->getDisplay()->getArgumentsTokens(); Chris@0: $base_path = $argument->globalTokenReplace($base_path, $tokens); Chris@0: // @todo Views should expect and store a leading /. See: Chris@0: // https://www.drupal.org/node/2423913 Chris@0: $url = Url::fromUserInput('/' . $base_path); Chris@0: try { Chris@0: /** @var \Symfony\Component\Routing\Route $route */ Chris@0: $route_name = $url->getRouteName(); Chris@0: $route = \Drupal::service('router.route_provider')->getRouteByName($route_name); Chris@0: Chris@0: $route_variables = $route->compile()->getVariables(); Chris@0: $parameters = $url->getRouteParameters(); Chris@0: Chris@0: foreach ($route_variables as $variable_name) { Chris@0: $parameters[$variable_name] = array_shift($args); Chris@0: } Chris@0: Chris@0: $url->setRouteParameters($parameters); Chris@0: } Chris@0: catch (Exception $e) { Chris@0: // If the given route doesn't exist, default to Chris@0: $url = Url::fromRoute(''); Chris@0: } Chris@0: } Chris@0: else { Chris@0: $url = $view->getUrl($args)->setOptions($url_options); Chris@0: } Chris@0: $variables['rows'][$id]->url = $url->toString(); Chris@0: $variables['rows'][$id]->count = intval($row->{$argument->count_alias}); Chris@0: $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes); Chris@0: $variables['rows'][$id]->active = isset($active_urls[$variables['rows'][$id]->url]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for unformatted summary view templates. Chris@0: * Chris@0: * Default template: views-view-summary-unformatted.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: A ViewExecutable object. Chris@0: * - rows: The raw row data. Chris@0: * - options: An array of options. Each option contains: Chris@0: * - separator: A string to be placed between inline fields to keep them Chris@0: * visually distinct. Chris@0: */ Chris@0: function template_preprocess_views_view_summary_unformatted(&$variables) { Chris@0: /** @var \Drupal\views\ViewExecutable $view */ Chris@0: $view = $variables['view']; Chris@0: $argument = $view->argument[$view->build_info['summary_level']]; Chris@0: Chris@0: $url_options = []; Chris@0: Chris@0: if (!empty($view->exposed_raw_input)) { Chris@0: $url_options['query'] = $view->exposed_raw_input; Chris@0: } Chris@0: Chris@0: $count = 0; Chris@0: $active_urls = [ Chris@0: // Force system path. Chris@18: Url::fromRoute('', [], ['alias' => TRUE])->toString(), Chris@0: // Could be an alias. Chris@18: Url::fromRoute('')->toString(), Chris@0: ]; Chris@0: $active_urls = array_combine($active_urls, $active_urls); Chris@0: Chris@0: // Collect all arguments for each row, to be able to alter them for example Chris@0: // by the validator. This is not done per single argument value, because Chris@0: // this could cause performance problems. Chris@0: $row_args = []; Chris@0: foreach ($variables['rows'] as $id => $row) { Chris@0: $row_args[$id] = $argument->summaryArgument($row); Chris@0: } Chris@0: $argument->processSummaryArguments($row_args); Chris@0: Chris@0: foreach ($variables['rows'] as $id => $row) { Chris@0: // Only false on first time. Chris@0: if ($count++) { Chris@0: $variables['rows'][$id]->separator = Xss::filterAdmin($variables['options']['separator']); Chris@0: } Chris@0: $variables['rows'][$id]->attributes = []; Chris@0: $variables['rows'][$id]->link = $argument->summaryName($row); Chris@0: $args = $view->args; Chris@0: $args[$argument->position] = $row_args[$id]; Chris@0: Chris@0: if (!empty($argument->options['summary_options']['base_path'])) { Chris@0: $base_path = $argument->options['summary_options']['base_path']; Chris@0: $tokens = $view->getDisplay()->getArgumentsTokens(); Chris@0: $base_path = $argument->globalTokenReplace($base_path, $tokens); Chris@0: // @todo Views should expect and store a leading /. See: Chris@0: // https://www.drupal.org/node/2423913 Chris@0: $url = Url::fromUserInput('/' . $base_path); Chris@0: try { Chris@0: /** @var \Symfony\Component\Routing\Route $route */ Chris@0: $route = \Drupal::service('router.route_provider')->getRouteByName($url->getRouteName()); Chris@0: $route_variables = $route->compile()->getVariables(); Chris@0: $parameters = $url->getRouteParameters(); Chris@0: Chris@0: foreach ($route_variables as $variable_name) { Chris@0: $parameters[$variable_name] = array_shift($args); Chris@0: } Chris@0: Chris@0: $url->setRouteParameters($parameters); Chris@0: } Chris@0: catch (Exception $e) { Chris@0: // If the given route doesn't exist, default to Chris@0: $url = Url::fromRoute(''); Chris@0: } Chris@0: } Chris@0: else { Chris@0: $url = $view->getUrl($args)->setOptions($url_options); Chris@0: } Chris@0: $variables['rows'][$id]->url = $url->toString(); Chris@0: $variables['rows'][$id]->count = intval($row->{$argument->count_alias}); Chris@0: $variables['rows'][$id]->active = isset($active_urls[$variables['rows'][$id]->url]); Chris@0: $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views table templates. Chris@0: * Chris@0: * Default template: views-view-table.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: A ViewExecutable object. Chris@0: * - rows: The raw row data. Chris@0: */ Chris@0: function template_preprocess_views_view_table(&$variables) { Chris@0: $view = $variables['view']; Chris@0: Chris@0: // We need the raw data for this grouping, which is passed in Chris@0: // as $variables['rows']. Chris@0: // However, the template also needs to use for the rendered fields. We Chris@0: // therefore swap the raw data out to a new variable and reset $variables['rows'] Chris@0: // so that it can get rebuilt. Chris@0: // Store rows so that they may be used by further preprocess functions. Chris@0: $result = $variables['result'] = $variables['rows']; Chris@0: $variables['rows'] = []; Chris@0: $variables['header'] = []; Chris@0: Chris@0: $options = $view->style_plugin->options; Chris@0: $handler = $view->style_plugin; Chris@0: Chris@0: $fields = &$view->field; Chris@0: $columns = $handler->sanitizeColumns($options['columns'], $fields); Chris@0: Chris@0: $active = !empty($handler->active) ? $handler->active : ''; Chris@0: $order = !empty($handler->order) ? $handler->order : 'asc'; Chris@0: Chris@0: // A boolean variable which stores whether the table has a responsive class. Chris@0: $responsive = FALSE; Chris@0: Chris@0: // For the actual site we want to not render full URLs, because this would Chris@0: // make pagers cacheable per URL, which is problematic in blocks, for example. Chris@0: // For the actual live preview though the javascript relies on properly Chris@0: // working URLs. Chris@0: $route_name = !empty($view->live_preview) ? '' : ''; Chris@0: Chris@18: $query = TableSort::getQueryParameters(\Drupal::request()); Chris@0: if (isset($view->exposed_raw_input)) { Chris@0: $query += $view->exposed_raw_input; Chris@0: } Chris@0: Chris@0: // A boolean to store whether the table's header has any labels. Chris@0: $has_header_labels = FALSE; Chris@0: foreach ($columns as $field => $column) { Chris@0: // Create a second variable so we can easily find what fields we have and Chris@0: // what the CSS classes should be. Chris@0: $variables['fields'][$field] = Html::cleanCssIdentifier($field); Chris@0: if ($active == $field) { Chris@0: $variables['fields'][$field] .= ' is-active'; Chris@0: } Chris@0: Chris@0: // Render the header labels. Chris@0: if ($field == $column && empty($fields[$field]->options['exclude'])) { Chris@0: $label = !empty($fields[$field]) ? $fields[$field]->label() : ''; Chris@0: if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) { Chris@0: $variables['header'][$field]['content'] = $label; Chris@0: } Chris@0: else { Chris@0: $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc'; Chris@0: Chris@0: if ($active == $field) { Chris@0: $initial = ($order == 'asc') ? 'desc' : 'asc'; Chris@0: } Chris@0: Chris@0: $title = t('sort by @s', ['@s' => $label]); Chris@0: if ($active == $field) { Chris@0: $variables['header'][$field]['sort_indicator'] = [ Chris@0: '#theme' => 'tablesort_indicator', Chris@0: '#style' => $initial, Chris@0: ]; Chris@0: } Chris@0: Chris@0: $query['order'] = $field; Chris@0: $query['sort'] = $initial; Chris@0: $link_options = [ Chris@0: 'query' => $query, Chris@0: ]; Chris@0: $url = new Url($route_name, [], $link_options); Chris@0: $variables['header'][$field]['url'] = $url->toString(); Chris@0: $variables['header'][$field]['content'] = $label; Chris@0: $variables['header'][$field]['title'] = $title; Chris@0: } Chris@0: Chris@0: $variables['header'][$field]['default_classes'] = $fields[$field]->options['element_default_classes']; Chris@0: // Set up the header label class. Chris@0: $variables['header'][$field]['attributes'] = []; Chris@0: $class = $fields[$field]->elementLabelClasses(0); Chris@0: if ($class) { Chris@0: $variables['header'][$field]['attributes']['class'][] = $class; Chris@0: } Chris@0: // Add responsive header classes. Chris@0: if (!empty($options['info'][$field]['responsive'])) { Chris@0: $variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive']; Chris@0: $responsive = TRUE; Chris@0: } Chris@0: // Add a CSS align class to each field if one was set. Chris@0: if (!empty($options['info'][$field]['align'])) { Chris@0: $variables['header'][$field]['attributes']['class'][] = Html::cleanCssIdentifier($options['info'][$field]['align']); Chris@0: } Chris@0: // Add a header label wrapper if one was selected. Chris@0: if ($variables['header'][$field]['content']) { Chris@0: $element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE); Chris@0: if ($element_label_type) { Chris@0: $variables['header'][$field]['wrapper_element'] = $element_label_type; Chris@0: } Chris@0: // Improves accessibility of complex tables. Chris@0: $variables['header'][$field]['attributes']['id'] = Html::getUniqueId('view-' . $field . '-table-column'); Chris@0: } Chris@0: // Check if header label is not empty. Chris@0: if (!empty($variables['header'][$field]['content'])) { Chris@0: $has_header_labels = TRUE; Chris@0: } Chris@0: Chris@0: $variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']); Chris@0: } Chris@0: Chris@0: // Add a CSS align class to each field if one was set. Chris@0: if (!empty($options['info'][$field]['align'])) { Chris@0: $variables['fields'][$field] .= ' ' . Html::cleanCssIdentifier($options['info'][$field]['align']); Chris@0: } Chris@0: Chris@0: // Render each field into its appropriate column. Chris@0: foreach ($result as $num => $row) { Chris@0: Chris@0: // Skip building the attributes and content if the field is to be excluded Chris@0: // from the display. Chris@0: if (!empty($fields[$field]->options['exclude'])) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: // Reference to the column in the loop to make the code easier to read. Chris@0: $column_reference =& $variables['rows'][$num]['columns'][$column]; Chris@0: Chris@0: $column_reference['default_classes'] = $fields[$field]->options['element_default_classes']; Chris@0: Chris@0: // Set the field key to the column so it can be used for adding classes Chris@0: // in a template. Chris@0: $column_reference['fields'][] = $variables['fields'][$field]; Chris@0: Chris@0: // Add field classes. Chris@0: if (!isset($column_reference['attributes'])) { Chris@0: $column_reference['attributes'] = []; Chris@0: } Chris@0: Chris@0: if ($classes = $fields[$field]->elementClasses($num)) { Chris@0: $column_reference['attributes']['class'][] = $classes; Chris@0: } Chris@0: Chris@0: // Add responsive header classes. Chris@0: if (!empty($options['info'][$field]['responsive'])) { Chris@0: $column_reference['attributes']['class'][] = $options['info'][$field]['responsive']; Chris@0: } Chris@0: Chris@0: // Improves accessibility of complex tables. Chris@0: if (isset($variables['header'][$field]['attributes']['id'])) { Chris@0: $column_reference['attributes']['headers'] = [$variables['header'][$field]['attributes']['id']]; Chris@0: } Chris@0: Chris@0: if (!empty($fields[$field])) { Chris@0: $field_output = $handler->getField($num, $field); Chris@0: $column_reference['wrapper_element'] = $fields[$field]->elementType(TRUE, TRUE); Chris@0: if (!isset($column_reference['content'])) { Chris@0: $column_reference['content'] = []; Chris@0: } Chris@0: Chris@0: // Only bother with separators and stuff if the field shows up. Chris@0: // Place the field into the column, along with an optional separator. Chris@0: if (trim($field_output) != '') { Chris@0: if (!empty($column_reference['content']) && !empty($options['info'][$column]['separator'])) { Chris@0: $column_reference['content'][] = [ Chris@0: 'separator' => ['#markup' => $options['info'][$column]['separator']], Chris@17: 'field_output' => ['#markup' => $field_output], Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $column_reference['content'][] = [ Chris@17: 'field_output' => ['#markup' => $field_output], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: $column_reference['attributes'] = new Attribute($column_reference['attributes']); Chris@0: } Chris@0: Chris@0: // Remove columns if the "empty_column" option is checked and the Chris@0: // field is empty. Chris@0: if (!empty($options['info'][$field]['empty_column'])) { Chris@0: $empty = TRUE; Chris@0: foreach ($variables['rows'] as $columns) { Chris@0: $empty &= empty($columns['columns'][$column]['content']); Chris@0: } Chris@0: if ($empty) { Chris@0: foreach ($variables['rows'] as &$column_items) { Chris@0: unset($column_items['columns'][$column]); Chris@0: } Chris@0: unset($variables['header'][$column]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Hide table header if all labels are empty. Chris@0: if (!$has_header_labels) { Chris@0: $variables['header'] = []; Chris@0: } Chris@0: Chris@0: foreach ($variables['rows'] as $num => $row) { Chris@0: $variables['rows'][$num]['attributes'] = []; Chris@0: if ($row_class = $handler->getRowClass($num)) { Chris@0: $variables['rows'][$num]['attributes']['class'][] = $row_class; Chris@0: } Chris@0: $variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']); Chris@0: } Chris@0: Chris@0: if (empty($variables['rows']) && !empty($options['empty_table'])) { Chris@0: $build = $view->display_handler->renderArea('empty'); Chris@0: $variables['rows'][0]['columns'][0]['content'][0]['field_output'] = $build; Chris@0: $variables['rows'][0]['attributes'] = new Attribute(['class' => ['odd']]); Chris@0: // Calculate the amounts of rows with output. Chris@0: $variables['rows'][0]['columns'][0]['attributes'] = new Attribute([ Chris@0: 'colspan' => count($variables['header']), Chris@0: 'class' => ['views-empty'], Chris@0: ]); Chris@0: } Chris@0: Chris@0: $variables['sticky'] = FALSE; Chris@0: if (!empty($options['sticky'])) { Chris@0: $variables['view']->element['#attached']['library'][] = 'core/drupal.tableheader'; Chris@0: $variables['sticky'] = TRUE; Chris@0: } Chris@0: Chris@0: // Add the caption to the list if set. Chris@0: if (!empty($handler->options['caption'])) { Chris@0: $variables['caption'] = ['#markup' => $handler->options['caption']]; Chris@0: $variables['caption_needed'] = TRUE; Chris@0: } Chris@0: elseif (!empty($variables['title'])) { Chris@0: $variables['caption'] = ['#markup' => $variables['title']]; Chris@0: $variables['caption_needed'] = TRUE; Chris@0: } Chris@0: else { Chris@0: $variables['caption'] = ''; Chris@0: $variables['caption_needed'] = FALSE; Chris@0: } Chris@0: Chris@0: $variables['summary'] = $handler->options['summary']; Chris@0: $variables['description'] = $handler->options['description']; Chris@0: $variables['caption_needed'] |= !empty($variables['summary']) || !empty($variables['description']); Chris@0: Chris@0: $variables['responsive'] = FALSE; Chris@0: // If the table has headers and it should react responsively to columns hidden Chris@0: // with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM Chris@0: // and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors. Chris@0: if (isset($variables['header']) && $responsive) { Chris@0: $variables['view']->element['#attached']['library'][] = 'core/drupal.tableresponsive'; Chris@0: // Add 'responsive-enabled' class to the table to identify it for JS. Chris@0: // This is needed to target tables constructed by this function. Chris@0: $variables['responsive'] = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views grid style templates. Chris@0: * Chris@0: * Default template: views-view-grid.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: The view object. Chris@0: * - rows: An array of row items. Each row is an array of content. Chris@0: */ Chris@0: function template_preprocess_views_view_grid(&$variables) { Chris@0: $options = $variables['options'] = $variables['view']->style_plugin->options; Chris@0: $horizontal = ($options['alignment'] === 'horizontal'); Chris@0: Chris@0: $col = 0; Chris@0: $row = 0; Chris@0: $items = []; Chris@0: $remainders = count($variables['rows']) % $options['columns']; Chris@0: $num_rows = floor(count($variables['rows']) / $options['columns']); Chris@0: Chris@0: // Iterate over each rendered views result row. Chris@0: foreach ($variables['rows'] as $result_index => $item) { Chris@0: Chris@0: // Add the item. Chris@0: if ($horizontal) { Chris@0: $items[$row]['content'][$col]['content'] = $item; Chris@0: } Chris@0: else { Chris@0: $items[$col]['content'][$row]['content'] = $item; Chris@0: } Chris@0: Chris@0: // Create attributes for rows. Chris@0: if (!$horizontal || ($horizontal && empty($items[$row]['attributes']))) { Chris@0: $row_attributes = ['class' => []]; Chris@0: // Add custom row classes. Chris@0: $row_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'row'))); Chris@0: if (!empty($row_class)) { Chris@0: $row_attributes['class'] = array_merge($row_attributes['class'], $row_class); Chris@0: } Chris@0: // Add row attributes to the item. Chris@0: if ($horizontal) { Chris@0: $items[$row]['attributes'] = new Attribute($row_attributes); Chris@0: } Chris@0: else { Chris@0: $items[$col]['content'][$row]['attributes'] = new Attribute($row_attributes); Chris@0: } Chris@0: } Chris@0: Chris@0: // Create attributes for columns. Chris@0: if ($horizontal || (!$horizontal && empty($items[$col]['attributes']))) { Chris@0: $col_attributes = ['class' => []]; Chris@0: // Add default views column classes. Chris@0: // Add custom column classes. Chris@0: $col_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'col'))); Chris@0: if (!empty($col_class)) { Chris@0: $col_attributes['class'] = array_merge($col_attributes['class'], $col_class); Chris@0: } Chris@0: // Add automatic width for columns. Chris@0: if ($options['automatic_width']) { Chris@0: $col_attributes['style'] = 'width: ' . (100 / $options['columns']) . '%;'; Chris@0: } Chris@0: // Add column attributes to the item. Chris@0: if ($horizontal) { Chris@0: $items[$row]['content'][$col]['attributes'] = new Attribute($col_attributes); Chris@0: } Chris@0: else { Chris@0: $items[$col]['attributes'] = new Attribute($col_attributes); Chris@0: } Chris@0: } Chris@0: Chris@0: // Increase, decrease or reset appropriate integers. Chris@0: if ($horizontal) { Chris@0: if ($col == 0 && $col != ($options['columns'] - 1)) { Chris@0: $col++; Chris@0: } Chris@0: elseif ($col >= ($options['columns'] - 1)) { Chris@0: $col = 0; Chris@0: $row++; Chris@0: } Chris@0: else { Chris@0: $col++; Chris@0: } Chris@0: } Chris@0: else { Chris@0: $row++; Chris@0: if (!$remainders && $row == $num_rows) { Chris@0: $row = 0; Chris@0: $col++; Chris@0: } Chris@0: elseif ($remainders && $row == $num_rows + 1) { Chris@0: $row = 0; Chris@0: $col++; Chris@0: $remainders--; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Add items to the variables array. Chris@0: $variables['items'] = $items; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views unformatted rows templates. Chris@0: * Chris@0: * Default template: views-view-unformatted.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: The view object. Chris@0: * - rows: An array of row items. Each row is an array of content. Chris@0: */ Chris@0: function template_preprocess_views_view_unformatted(&$variables) { Chris@0: $view = $variables['view']; Chris@0: $rows = $variables['rows']; Chris@0: $style = $view->style_plugin; Chris@0: $options = $style->options; Chris@0: Chris@0: $variables['default_row_class'] = !empty($options['default_row_class']); Chris@0: foreach ($rows as $id => $row) { Chris@0: $variables['rows'][$id] = []; Chris@0: $variables['rows'][$id]['content'] = $row; Chris@0: $variables['rows'][$id]['attributes'] = new Attribute(); Chris@0: if ($row_class = $view->style_plugin->getRowClass($id)) { Chris@0: $variables['rows'][$id]['attributes']->addClass($row_class); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for Views HTML list templates. Chris@0: * Chris@0: * Default template: views-view-list.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: A View object. Chris@0: */ Chris@0: function template_preprocess_views_view_list(&$variables) { Chris@17: $handler = $variables['view']->style_plugin; Chris@0: Chris@0: // Fetch classes from handler options. Chris@0: if ($handler->options['class']) { Chris@0: $class = explode(' ', $handler->options['class']); Chris@0: $class = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', $class); Chris@0: Chris@0: // Initialize a new attribute class for $class. Chris@0: $variables['list']['attributes'] = new Attribute(['class' => $class]); Chris@0: } Chris@0: Chris@0: // Fetch wrapper classes from handler options. Chris@0: if ($handler->options['wrapper_class']) { Chris@0: $wrapper_class = explode(' ', $handler->options['wrapper_class']); Chris@0: $variables['attributes']['class'] = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', $wrapper_class); Chris@0: } Chris@0: Chris@0: $variables['list']['type'] = $handler->options['type']; Chris@0: Chris@0: template_preprocess_views_view_unformatted($variables); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for RSS feed templates. Chris@0: * Chris@0: * Default template: views-view-rss.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: A ViewExecutable object. Chris@0: * - rows: The raw row data. Chris@0: */ Chris@0: function template_preprocess_views_view_rss(&$variables) { Chris@17: $view = $variables['view']; Chris@0: $items = $variables['rows']; Chris@0: $style = $view->style_plugin; Chris@0: Chris@0: $config = \Drupal::config('system.site'); Chris@0: Chris@0: // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description. Chris@0: // We strip all HTML tags, but need to prevent double encoding from properly Chris@0: // escaped source data (such as & becoming &amp;). Chris@0: $variables['description'] = Html::decodeEntities(strip_tags($style->getDescription())); Chris@0: Chris@0: if ($view->display_handler->getOption('sitename_title')) { Chris@0: $title = $config->get('name'); Chris@0: if ($slogan = $config->get('slogan')) { Chris@0: $title .= ' - ' . $slogan; Chris@0: } Chris@0: } Chris@0: else { Chris@0: $title = $view->getTitle(); Chris@0: } Chris@0: $variables['title'] = $title; Chris@0: Chris@0: // Figure out which display which has a path we're using for this feed. If Chris@0: // there isn't one, use the global $base_url Chris@0: $link_display_id = $view->display_handler->getLinkDisplay(); Chris@0: /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase $display */ Chris@0: if ($link_display_id && ($display = $view->displayHandlers->get($link_display_id)) && $display->isEnabled()) { Chris@0: $url = $view->getUrl(NULL, $link_display_id); Chris@0: } Chris@0: Chris@0: /** @var \Drupal\Core\Url $url */ Chris@0: if (!empty($url)) { Chris@0: $url_options = ['absolute' => TRUE]; Chris@0: if (!empty($view->exposed_raw_input)) { Chris@0: $url_options['query'] = $view->exposed_raw_input; Chris@0: } Chris@0: Chris@0: // Compare the link to the default home page; if it's the default home page, Chris@0: // just use $base_url. Chris@0: $url_string = $url->setOptions($url_options)->toString(); Chris@0: if ($url_string === Url::fromUserInput($config->get('page.front'))->toString()) { Chris@0: $url_string = Url::fromRoute('')->setAbsolute()->toString(); Chris@0: } Chris@0: Chris@0: $variables['link'] = $url_string; Chris@0: } Chris@0: Chris@0: $variables['langcode'] = \Drupal::languageManager()->getCurrentLanguage()->getId(); Chris@0: $variables['namespaces'] = new Attribute($style->namespaces); Chris@0: $variables['items'] = $items; Chris@0: $variables['channel_elements'] = \Drupal::service('renderer')->render($style->channel_elements); Chris@0: Chris@0: // During live preview we don't want to output the header since the contents Chris@0: // of the feed are being displayed inside a normal HTML page. Chris@0: if (empty($variables['view']->live_preview)) { Chris@0: $variables['view']->getResponse()->headers->set('Content-Type', 'application/rss+xml; charset=utf-8'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views RSS item templates. Chris@0: * Chris@0: * Default template: views-view-row-rss.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - row: The raw results rows. Chris@0: */ Chris@0: function template_preprocess_views_view_row_rss(&$variables) { Chris@0: $item = $variables['row']; Chris@0: $variables['title'] = $item->title; Chris@0: $variables['link'] = $item->link; Chris@0: Chris@0: // The description is the only place where we should find HTML. Chris@0: // @see https://validator.w3.org/feed/docs/rss2.html#hrelementsOfLtitemgt Chris@0: // If we have a render array, render it here and pass the result to the Chris@0: // template, letting Twig autoescape it. Chris@0: if (isset($item->description) && is_array($item->description)) { Chris@0: $variables['description'] = (string) \Drupal::service('renderer')->render($item->description); Chris@0: } Chris@0: Chris@0: $variables['item_elements'] = []; Chris@0: foreach ($item->elements as $element) { Chris@0: if (isset($element['attributes']) && is_array($element['attributes'])) { Chris@0: $element['attributes'] = new Attribute($element['attributes']); Chris@0: } Chris@0: $variables['item_elements'][] = $element; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for OPML feed templates. Chris@0: * Chris@0: * Default template: views-view-opml.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - view: A ViewExecutable object. Chris@0: * - rows: The raw row data. Chris@0: */ Chris@0: function template_preprocess_views_view_opml(&$variables) { Chris@17: $view = $variables['view']; Chris@0: $items = $variables['rows']; Chris@0: Chris@0: $config = \Drupal::config('system.site'); Chris@0: Chris@0: if ($view->display_handler->getOption('sitename_title')) { Chris@0: $title = $config->get('name'); Chris@0: if ($slogan = $config->get('slogan')) { Chris@0: $title .= ' - ' . $slogan; Chris@0: } Chris@0: } Chris@0: else { Chris@0: $title = $view->getTitle(); Chris@0: } Chris@0: $variables['title'] = $title; Chris@0: $variables['items'] = $items; Chris@0: $variables['updated'] = gmdate(DATE_RFC2822, REQUEST_TIME); Chris@0: Chris@0: // During live preview we don't want to output the header since the contents Chris@0: // of the feed are being displayed inside a normal HTML page. Chris@0: if (empty($variables['view']->live_preview)) { Chris@0: $variables['view']->getResponse()->headers->set('Content-Type', 'text/xml; charset=utf-8'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views OPML item templates. Chris@0: * Chris@0: * Default template: views-view-row-opml.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - row: The raw results rows. Chris@0: */ Chris@0: function template_preprocess_views_view_row_opml(&$variables) { Chris@0: $item = $variables['row']; Chris@0: Chris@0: $variables['attributes'] = new Attribute($item); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views exposed form templates. Chris@0: * Chris@0: * Default template: views-exposed-form.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - form: A render element representing the form. Chris@0: */ Chris@0: function template_preprocess_views_exposed_form(&$variables) { Chris@0: $form = &$variables['form']; Chris@0: Chris@0: if (!empty($form['q'])) { Chris@0: $variables['q'] = $form['q']; Chris@0: } Chris@0: Chris@0: foreach ($form['#info'] as $info) { Chris@0: if (!empty($info['label'])) { Chris@0: $form[$info['value']]['#title'] = $info['label']; Chris@0: } Chris@0: if (!empty($info['description'])) { Chris@0: $form[$info['value']]['#description'] = $info['description']; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for views mini-pager templates. Chris@0: * Chris@0: * Default template: views-mini-pager.html.twig. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - tags: Provides link text for the next/previous links. Chris@0: * - element: The pager's id. Chris@0: * - parameters: Any extra GET parameters that should be retained, such as Chris@0: * exposed input. Chris@0: */ Chris@0: function template_preprocess_views_mini_pager(&$variables) { Chris@0: global $pager_page_array, $pager_total; Chris@0: Chris@0: $tags = &$variables['tags']; Chris@0: $element = $variables['element']; Chris@0: $parameters = $variables['parameters']; Chris@0: Chris@0: // Current is the page we are currently paged to. Chris@0: $variables['items']['current'] = $pager_page_array[$element] + 1; Chris@0: Chris@0: if ($pager_total[$element] > 1 && $pager_page_array[$element] > 0) { Chris@0: $options = [ Chris@0: 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1), Chris@0: ]; Chris@18: $variables['items']['previous']['href'] = Url::fromRoute('', [], $options)->toString(); Chris@0: if (isset($tags[1])) { Chris@0: $variables['items']['previous']['text'] = $tags[1]; Chris@0: } Chris@0: $variables['items']['previous']['attributes'] = new Attribute(); Chris@0: } Chris@0: Chris@0: if ($pager_page_array[$element] < ($pager_total[$element] - 1)) { Chris@0: $options = [ Chris@0: 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1), Chris@0: ]; Chris@18: $variables['items']['next']['href'] = Url::fromRoute('', [], $options)->toString(); Chris@0: if (isset($tags[3])) { Chris@0: $variables['items']['next']['text'] = $tags[3]; Chris@0: } Chris@0: $variables['items']['next']['attributes'] = new Attribute(); Chris@0: } Chris@0: Chris@0: // This is based on the entire current query string. We need to ensure Chris@0: // cacheability is affected accordingly. Chris@0: $variables['#cache']['contexts'][] = 'url.query_args'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @defgroup views_templates Views template files Chris@0: * @{ Chris@0: * Describes various views templates & overriding options. Chris@0: * Chris@0: * All views templates can be overridden with a variety of names, using Chris@0: * the view, the display ID of the view, the display type of the view, Chris@0: * or some combination thereof. Chris@0: * Chris@0: * For each view, there will be a minimum of two templates used. The first Chris@0: * is used for all views: views-view.html.twig. Chris@0: * Chris@0: * The second template is determined by the style selected for the view. Note Chris@0: * that certain aspects of the view can also change which style is used; for Chris@0: * example, arguments which provide a summary view might change the style to Chris@0: * one of the special summary styles. Chris@0: * Chris@0: * The default style for all views is views-view-unformatted.html.twig. Chris@0: * Chris@0: * Many styles will then farm out the actual display of each row to a row Chris@0: * style; the default row style is views-view-fields.html.twig. Chris@0: * Chris@0: * Here is an example of all the templates that will be tried in the following Chris@0: * case: Chris@0: * Chris@0: * View, named foobar. Style: unformatted. Row style: Fields. Display: Page. Chris@0: * Chris@0: * - views-view--foobar--page.html.twig Chris@0: * - views-view--page.html.twig Chris@0: * - views-view--foobar.html.twig Chris@0: * - views-view.html.twig Chris@0: * Chris@0: * - views-view-unformatted--foobar--page.html.twig Chris@0: * - views-view-unformatted--page.html.twig Chris@0: * - views-view-unformatted--foobar.html.twig Chris@0: * - views-view-unformatted.html.twig Chris@0: * Chris@0: * - views-view-fields--foobar--page.html.twig Chris@0: * - views-view-fields--page.html.twig Chris@0: * - views-view-fields--foobar.html.twig Chris@0: * - views-view-fields.html.twig Chris@0: * Chris@0: * Important! When adding a new template to your theme, be sure to flush the Chris@0: * theme registry cache! Chris@0: * Chris@0: * @ingroup views_overview Chris@0: * @see \Drupal\views\ViewExecutable::buildThemeFunctions() Chris@0: * @} Chris@0: */