You are here

public function ViewsAccordion::preRender in Views Accordion 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/views/style/ViewsAccordion.php \Drupal\views_accordion\Plugin\views\style\ViewsAccordion::preRender()

Allow the style to do stuff before each row is rendered.

Parameters

$result: The full array of results from the query.

Overrides StylePluginBase::preRender

File

src/Plugin/views/style/ViewsAccordion.php, line 217

Class

ViewsAccordion
Style plugin to render each item in an ordered or unordered list.

Namespace

Drupal\views_accordion\Plugin\views\style

Code

public function preRender($result) {

  // No need do anything if we we have only one result and disableifone is
  // active.
  if ($this->options['disableifone'] == '1') {
    if (count($result) < 2) {
      return;
    }
  }
  $view_settings = [];
  $header_class = 'views-accordion-header';

  // This is used for triggering the creation of the accordions.
  // We append the dom_id so that multiple nested views with accordions work.
  $accordion_header_class = 'js-' . $header_class . '-' . $this->view->dom_id;
  $view_settings['usegroupheader'] = FALSE;
  foreach ($this->options['grouping'] as $group) {
    $view_settings['usegroupheader'] = $group['use-grouping-header'] == 1;

    // @TODO handle multiple grouping.
    break;
  }

  // Find out about the header field options.
  $fields = array_values($this->displayHandler
    ->getOption('fields'));

  // Add header class to first not-excluded field.
  foreach ($fields as $field) {
    if (!isset($field['exclude']) || $field['exclude'] == 0) {

      // Make sure we are using a div for markup at least.
      if (empty($field['element_wrapper_type'])) {
        $this->view->field[$field['id']]->options['element_wrapper_type'] = 'div';
      }

      // Setup our wrapper class if not using group header.
      if (!$view_settings['usegroupheader']) {
        $header_wrapper_class = $header_class . ' ' . $accordion_header_class;

        // If the user configured its own class, set that up with our own
        // class.
        if (!empty($field['element_wrapper_class'])) {
          $header_wrapper_class = $field['element_wrapper_class'] . ' ' . $header_wrapper_class;
        }

        // Setup the view to use our processed wrapper class.
        $this->view->field[$field['id']]->options['element_wrapper_class'] = $header_wrapper_class;
      }
      break;
    }
  }
  $this->view->element['#attached']['library'][] = 'views_accordion/views_accordion.accordion';

  // Add the appropiate effect library if necessary.
  $effect = $this->options['animated'];
  if ($effect !== 'none' && $effect !== 'swing' && $effect !== 'linear') {

    // jquery.ui.effects.core provides the easing effects.
    // It would be possible to integrate and load any other libraries here.
    $this->view->element['#attached']['library'][] = 'core/jquery.ui.effects.core';
  }

  // Prepare the JS settings.
  // We do it here so we don't have it run once every group.
  $view_settings['collapsible'] = $this->options['collapsible'];
  if ($this->options['row-start-open'] == 'random') {
    $view_settings['rowstartopen'] = 'random';
  }
  else {
    $view_settings['rowstartopen'] = $this->options['row-start-open'] == 'none' ? FALSE : (int) $this->options['row-start-open'];
  }
  $view_settings['animated'] = $this->options['animated'] == 'none' ? FALSE : $this->options['animated'];
  $view_settings['duration'] = $this->options['animated'] == 'none' ? FALSE : $this->options['animation_time'];
  $view_settings['heightStyle'] = $this->options['heightStyle'];
  $view_settings['event'] = $this->options['event'];
  $view_settings['useHeaderIcons'] = $this->options['use_header_icons'];
  if ($this->options['use_header_icons']) {
    $view_settings['iconHeader'] = $this->options['icon_header'];
    $view_settings['iconActiveHeader'] = $this->options['icon_active_header'];
  }

  // The view display selector.
  // Set in stable & classy themes.
  $view_settings['display'] = '.js-view-dom-id-' . $this->view->dom_id;

  // The accordion header selector.
  $view_settings['header'] = '.' . $accordion_header_class;
  if ($view_settings['usegroupheader']) {

    // @TODO we cannot set a class for the grouping h3 apparently...
    $view_settings['header'] = '.js-views-accordion-group-header';
  }
  $this->view->element['#attached']['drupalSettings']['views_accordion'] = [
    $this->view->dom_id => $view_settings,
  ];
}