1 views.block.inc ViewsBlock::prepare()

Do any processing prior to getTitle() and getContent() being called.

The contexts will have been set on the object at this point, just prior to the block being rendered. This method is useful to save duplicate processing if both getTitle() and getContent() need to use the same data.

Overrides Block::prepare

File

core/modules/views/includes/views.block.inc, line 74

Class

ViewsBlock
Displays a view listing as a block, with overriding options.

Code

function prepare() {
  parent::prepare();

  list($view_name, $display_id) = explode('-', $this->delta);

  if ($view = views_get_view($view_name)) {
    if (!$view->access($display_id)) {
      $view->destroy();
      return;
    }
    $this->setDefaultsFromView($view);

    $view->set_display($display_id);
    $view->display_handler->set_override_options($this->settings);

    $args = array();
    $arguments = $view->display_handler->get_option('arguments');

    foreach ($view->display_handler->get_argument_input() as $id => $argument) {

      switch ($argument['type']) {
        case 'layout':
          foreach ($this->contexts as $context) {
            $args[] = $context->argument;
          }
          break;

        case 'fixed':
          $args[] = $argument['fixed'];
          break;

        case 'user':
          $args[] = (isset($this->settings['arguments'][$id]) && strlen($this->settings['arguments'][$id])) ? $this->settings['arguments'][$id] : NULL;
          break;

        case 'wildcard':
          // Put in the wildcard.
          $args[] = isset($arguments[$id]['wildcard']) ? $arguments[$id]['wildcard'] : '*';
          break;

        case 'url':
          if (isset($argument['position'])) {
            $arg = (int) $argument['position'];
            $args[] = arg($arg);
          }
          break;

        case 'none':
        default:
          // Let Views handle a NULL (or missing) argument.
          $args[] = NULL;
          break;
      }
    }

    // Remove any trailing NULL arguments as these are non-args:
    while (count($args) && end($args) === NULL) {
      array_pop($args);
    }

    $view->set_arguments($args);
    $allow = $view->display_handler->get_option('allow');

    if ($allow['path_override'] && !empty($this->settings['path'])) {
      $view->override_path = $this->settings['path'];
    }
    elseif ($path = $view->display_handler->get_option('inherit_path')) {
      $view->override_path = $_GET['q'];
    }

    // More link.
    if ($allow['more_link']) {
      if (empty($this->settings['more_link'])) {
        $view->display_handler->set_option('use_more', FALSE);
      }
      else {
        $view->display_handler->set_option('use_more', TRUE);
        // Make sure the view runs the count query so we know whether or not
        // the more link applies.
        $view->get_total_rows = TRUE;
      }
    }

    if ($allow['items_per_page'] && isset($this->settings['items_per_page'])) {
      $view->set_items_per_page($this->settings['items_per_page']);
    }

    if ($allow['offset']) {
      $view->set_offset($this->settings['offset']);
    }

    if ($allow['use_pager']) {
      // Only set use_pager if they differ, this way we can avoid overwriting the
      // pager type that Views uses.
      $pager = $view->display_handler->get_option('pager');
      if ($this->settings['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
        $pager['type'] = 'full';
      }
      elseif (!$this->settings['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
        $pager['type'] = $view->get_items_per_page() || !empty($pager['options']['items_per_page']) ? 'some' : 'none';
      }

      if ($this->settings['use_pager']) {
        if (!isset($pager['options']['id']) || (isset($this->settings['pager_id']) && $pager['options']['id'] != $this->settings['pager_id'])) {
          $pager['options']['id'] = (int) $this->settings['pager_id'];
        }
      }

      $view->display_handler->set_option('pager', $pager);
    }

    if ($allow['fields_override']) {
      if ($this->settings['fields_override']) {
        $fields = $view->get_items('field');
        foreach ($fields as $field => $field_display) {
          $fields[$field]['exclude'] = empty($this->settings['fields_override'][$field]);
        }
        $view->display_handler->set_option('fields', $fields);

      }
    }

    if ($allow['exposed_form'] && !empty($this->settings['exposed'])) {
      foreach ($this->settings['exposed'] as $filter_name => $filter_value) {
        if (!is_array($filter_value)) {
          $this->settings['exposed'][$filter_name] = $filter_value;
        }
      }
      $view->set_exposed_input($this->settings['exposed']);
    }

    $output = $view->execute_display($display_id);
    if ($output) {
      if (is_string($output['content'])) {
        $output['content'] = array(
          '#markup' => $output['content'],
        );
      }

      if (($allow['link_to_view'] && !empty($this->settings['link_to_view'])) || 
        (!$allow['link_to_view'] && $view->display_handler->get_option('link_to_view'))) {
        $output['path'] = $view->get_url();
      }

      views_add_contextual_links($output['content'], 'block', $view, $display_id);

      $this->viewOutput = $output;
    }

    $view->destroy();
  }
}