You are here

views_fieldset.module in Views fieldset style plugin 8

Same filename and directory in other branches
  1. 7 views_fieldset.module

Views fieldset style plugin code.

File

views_fieldset.module
View source
<?php

/**
 * @file
 * Views fieldset style plugin code.
 */

/**
 * Implements hook_preprocess_HOOK() for theme_views_view_fieldset().
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Form\FormState;
function template_preprocess_views_view_fieldset(&$variables) {

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $rows = $variables['rows'];
  $style = $view->style_plugin;
  $options = $style->options;
  $group = $view->storage
    ->id() . '-' . $view->current_display;
  foreach ($rows as $id => $row) {
    $html_id = Html::getUniqueId($group . '-row-' . ($id + 1));
    $classes = array();
    if ($row_class = $view->style_plugin
      ->getRowClass($id)) {
      $classes[] = $row_class;
    }
    $field = $options['title'];
    $title = '';
    if (isset($view->field[$field])) {
      $title = $style
        ->getField($id, $field);
      if ($view->field[$field]->options['label']) {
        $title = $view->field[$field]->options['label'] . ': ' . $title;
      }
      $title = strip_tags(htmlspecialchars_decode($title));
    }
    $field = $options['description'];
    $description = '';
    if (isset($view->field[$field])) {
      $description = $style
        ->getField($id, $field);
      if ($view->field[$field]->options['label']) {
        $description = $view->field[$field]->options['label'] . ': ' . $description;
      }
      $description = strip_tags(htmlspecialchars_decode($description));
    }
    $element = array(
      '#title' => $title,
      '#type' => 'fieldset',
      '#children' => $row,
      '#parents' => array(),
      '#description' => $description,
      '#attributes' => array(
        'class' => $classes,
        'id' => $html_id,
      ),
    );
    $form = array();
    $form_state = new FormState();
    $element = RenderElement::processGroup($element, $form_state, $form);
    $variables['fieldsets'][] = $element;
  }
}