You are here

public function views_plugin_display::init in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 plugins/views_plugin_display.inc \views_plugin_display::init()
  2. 6.2 plugins/views_plugin_display.inc \views_plugin_display::init()
1 call to views_plugin_display::init()
views_plugin_display_feed::init in plugins/views_plugin_display_feed.inc
1 method overrides views_plugin_display::init()
views_plugin_display_feed::init in plugins/views_plugin_display_feed.inc

File

plugins/views_plugin_display.inc, line 51
Definition of views_plugin_display.

Class

views_plugin_display
The default display plugin handler. Display plugins handle options and basic mechanisms for different output methods.

Code

public function init(&$view, &$display, $options = NULL) {
  $this->view =& $view;
  $this->display =& $display;

  // Load extenders as soon as possible.
  $this->extender = array();
  $extenders = views_get_enabled_display_extenders();

  // If you update to the dev version the registry might not be loaded yet.
  if (!empty($extenders) && class_exists('views_plugin_display_extender')) {
    foreach ($extenders as $extender) {
      $plugin = views_get_plugin('display_extender', $extender);
      if ($plugin) {
        $plugin
          ->init($this->view, $this);
        $this->extender[$extender] = $plugin;
      }
      else {
        vpr('Invalid display extender @extender', array(
          '@extender' => $extender,
        ));
      }
    }
  }

  // Track changes that the user should know about.
  $changed = FALSE;

  // Make some modifications.
  if (!isset($options) && isset($display->display_options)) {
    $options = $display->display_options;
  }
  if ($this
    ->is_default_display() && isset($options['defaults'])) {
    unset($options['defaults']);
  }

  // Cache for unpack_options, but not if we are in the ui.
  static $unpack_options = array();
  if (empty($view->editing)) {
    $cid = 'unpack_options:' . md5(serialize(array(
      $this->options,
      $options,
    )));
    if (empty($unpack_options[$cid])) {
      $cache = views_cache_get($cid, TRUE);
      if (!empty($cache->data)) {
        $this->options = $cache->data;
      }
      else {
        $this
          ->unpack_options($this->options, $options);
        views_cache_set($cid, $this->options, TRUE);
      }
      $unpack_options[$cid] = $this->options;
    }
    else {
      $this->options = $unpack_options[$cid];
    }
  }
  else {
    $this
      ->unpack_options($this->options, $options);
  }

  // Translate changed settings.
  $items_per_page = $this
    ->get_option('items_per_page');
  $offset = $this
    ->get_option('offset');
  $use_pager = $this
    ->get_option('use_pager');
  $pager = $this
    ->get_option('pager');

  // Check if the pager options were already converted.
  // The pager settings of a Views 2.x view specifying 10 items with an
  // offset of 0 and no pager is the same as of a Views 3.x view with
  // default settings. In this case, the only way to determine which case we
  // are dealing with is checking the API version but that's only available
  // for exported Views as it's not stored in the database.
  // If you would like to change this code, really take care that you thought
  // of every possibility.
  // @todo Provide a way to convert the database views as well.
  if (!empty($items_per_page) && $items_per_page != 10 || !empty($offset) || !empty($use_pager) || !empty($view->api_version) && $view->api_version == 2) {

    // Find out the right pager type.
    // If the view "use pager" it's a normal/full pager.
    if ($use_pager) {
      $type = 'full';
    }
    else {
      $type = $items_per_page ? 'some' : 'none';
    }

    // Setup the pager options.
    $pager = array(
      'type' => $type,
      'options' => array(
        'offset' => intval($offset),
      ),
    );
    if ($items_per_page) {
      $pager['options']['items_per_page'] = $items_per_page;
    }

    // Setup the pager element.
    if ($id = $this
      ->get_option('pager_element')) {
      $pager['options']['id'] = $id;
    }

    // Unset the previous options. After edit and save the view they will be
    // erased.
    $this
      ->set_option('items_per_page', NULL);
    $this
      ->set_option('offset', NULL);
    $this
      ->set_option('use_pager', NULL);
    $this
      ->set_option('pager', $pager);
    $changed = TRUE;
  }

  // Plugable headers, footer and empty texts are not compatible with
  // previous version of views. This code converts old values into a
  // configured handler for each area.
  foreach (array(
    'header',
    'footer',
    'empty',
  ) as $area) {
    $converted = FALSE;
    if (isset($this->options[$area]) && !is_array($this->options[$area])) {
      if (!empty($this->options[$area])) {
        $content = $this
          ->get_option($area);
        if (!empty($content) && !is_array($content)) {
          $format = $this
            ->get_option($area . '_format');
          $options = array(
            'id' => 'area',
            'table' => 'views',
            'field' => 'area',
            'label' => '',
            'relationship' => 'none',
            'group_type' => 'group',
            'content' => $content,
            'format' => !empty($format) ? $format : filter_default_format(),
          );
          if ($area != 'empty' && ($empty = $this
            ->get_option($area . '_empty'))) {
            $options['empty'] = $empty;
          }
          $this
            ->set_option($area, array(
            'text' => $options,
          ));
          $converted = TRUE;
          $changed = TRUE;
        }
      }

      // Ensure that options are at least an empty array.
      if (!$converted) {
        $this
          ->set_option($area, array());
      }
    }
  }

  // Convert distinct setting from display to query settings.
  $distinct = $this
    ->get_option('distinct');
  if (!empty($distinct)) {
    $query_settings = $this
      ->get_option('query');
    $query_settings['options']['distinct'] = $distinct;
    $this
      ->set_option('query', $query_settings);

    // Clear the values.
    $this
      ->set_option('distinct', NULL);
    $changed = TRUE;
  }

  // Convert field language settings.
  $query_options = $this
    ->get_option('query');
  if (isset($query_options['options']['field_language'])) {
    $this
      ->set_option('field_language', $query_options['options']['field_language']);
    unset($query_options['options']['field_language']);
    $changed = TRUE;
  }
  if (isset($query_options['options']['field_language_add_to_query'])) {
    $this
      ->set_option('field_language_add_to_query', $query_options['options']['field_language_add_to_query']);
    unset($query_options['options']['field_language_add_to_query']);
    $changed = TRUE;
  }
  $this
    ->set_option('query', $query_options);

  // Convert filter groups.
  $filter_groups = $this
    ->get_option('filter_groups');

  // Only convert if it wasn't converted yet, which is the case if there is a
  // '0' group.
  if (isset($filter_groups['groups'][0])) {

    // Update filter groups.
    $filter_groups['groups'] = views_array_key_plus($filter_groups['groups']);
    $this
      ->set_option('filter_groups', $filter_groups);

    // Update the filter group on each filter.
    $filters = $this
      ->get_option('filters');
    foreach ($filters as &$filter) {
      if (isset($filter['group'])) {
        $filter['group']++;
      }
      else {
        $filter['group'] = 1;
      }
    }
    $this
      ->set_option('filters', $filters);
    $changed = TRUE;
  }

  // Filter groups were allowed to be rewritten without its filters, so
  // before this update the view was using the default values. To be sure that
  // the existing view isn't broken, don't use this overridden values but copy
  // them from the default display. Only do this if the filters are overridden
  // but the filter_groups are not marked as so.
  if (!$this
    ->is_default_display() && !$this->options['defaults']['filters'] && $this->options['defaults']['filter_groups']) {

    // Set filter_groups to be overridden and save the value in the
    // display_options as well.
    $this->options['defaults']['filter_groups'] = FALSE;
    $this->display->display_options['defaults']['filter_groups'] = $this->options['defaults']['filter_groups'];

    // Copy the filter_groups from the default, and add them to the
    // display_options as well. $this->default_display is not initialized at
    // this point.
    $this->options['filter_groups'] = $this->view->display['default']->handler->options['filter_groups'];
    $this->display->display_options['filter_groups'] = $this->options['filter_groups'];
    $changed = TRUE;
  }

  // Mark the view as changed so the user has a chance to save it.
  if ($changed) {
    $this->view->changed = TRUE;
  }
}