You are here

function charts_views_pre_view in Charts 5.0.x

Same name and namespace in other branches
  1. 8.4 charts.module \charts_views_pre_view()
  2. 8.3 charts.module \charts_views_pre_view()

Implements hook_views_pre_view().

File

./charts.module, line 69
Charts - Module.

Code

function charts_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  $hasFields = array_key_exists('fields', $view->display_handler->options);
  if ($hasFields) {
    $fields = $view->display_handler
      ->getOption('fields');
    $hasViewsFieldsOnOffHandler = FALSE;
    foreach ($fields as $field) {
      if (array_key_exists('plugin_id', $field)) {
        if ($field['plugin_id'] == 'field_exposed_chart_type') {
          $hasViewsFieldsOnOffHandler = TRUE;
        }
      }
    }
    if ($hasViewsFieldsOnOffHandler) {

      // Grab the type that has been submitted.
      $params = \Drupal::request()->query
        ->all();

      // This is for a GET request.
      // If the view is submitted through AJAX, like in view preview, it will be
      // a POST request. Merge the parameter arrays and we will get our values.
      $postParams = \Drupal::request()->request
        ->all();
      $params = array_merge($params, $postParams);
      foreach ($params as $key => $value) {
        if (strpos($key, 'ct') === 0) {
          $view->storage
            ->set('exposed_chart_type', $value);
        }
      }
      $view->element['#cache']['contexts'][] = 'url';
    }
  }
}