You are here

class views_plugin_display_system in Administration Views 6

Same name and namespace in other branches
  1. 7 plugins/views_plugin_display_system.inc \views_plugin_display_system

Plugin to handle replacement of existing system paths.

Hierarchy

Expanded class hierarchy of views_plugin_display_system

1 string reference to 'views_plugin_display_system'
admin_views_views_plugins in ./admin_views.views.inc
Implements hook_views_plugins().

File

plugins/views_plugin_display_system.inc, line 13
System display plugin.

View source
class views_plugin_display_system extends views_plugin_display {

  /**
   * The system display requires a path.
   */
  function has_path() {
    return TRUE;
  }

  // @todo Needed?
  function uses_breadcrumb() {
    return TRUE;
  }
  function option_definition() {
    $options = parent::option_definition();
    $options['path'] = array(
      'default' => '',
    );
    return $options;
  }

  /**
   * Add this display's path information to Drupal's menu system.
   */
  function execute_hook_menu($callbacks) {
    $items = array();

    // Replace % with the link to our standard views argument loader
    // views_arg_load().
    $bits = explode('/', $this
      ->get_option('path'));
    $page_arguments = array(
      $this->view->name,
      $this->display->id,
    );

    // Replace % with %views_arg for menu autoloading and add to the
    // page arguments so the argument actually comes through.
    foreach ($bits as $pos => $bit) {
      if ($bit == '%') {
        $bits[$pos] = '%views_arg';
        $page_arguments[] = $pos;
      }
    }
    $path = implode('/', $bits);

    // Views API 2 vs. 3; attempt to support both.
    if (method_exists($this, 'get_access_plugin')) {
      $access_plugin = $this
        ->get_access_plugin();
    }
    else {
      $access_plugin = $this
        ->get_plugin('access');
    }
    if (!isset($access_plugin)) {
      $access_plugin = views_get_plugin('access', 'none');
    }
    if (!$path) {
      return $items;
    }
    $items[$path] = array(
      // default views page entry
      'page callback' => 'views_page',
      'page arguments' => $page_arguments,
      // Default access check (per display)
      'access callback' => 'views_access',
      'access arguments' => array(
        $access_plugin
          ->get_access_callback(),
      ),
      // Identify URL embedded arguments and correlate them to a handler
      'load arguments' => array(
        $this->view->name,
        $this->display->id,
        '%index',
      ),
    );

    // Grep all callbacks for router items below the target path.
    $children = preg_grep('@' . preg_quote($path, '@') . '/.+@', array_keys($callbacks));
    foreach ($children as $child_path) {

      // Only inherit properties from parent, if the child is not a default
      // local task.
      if (!isset($callbacks[$child_path]['type']) || $callbacks[$child_path]['type'] != MENU_DEFAULT_LOCAL_TASK) {

        // Add child to our stack of items to replace.
        $items[$child_path] = $callbacks[$child_path];

        // Ensure that the parent path really exists and inherit its
        // properties to the child (not replacing existing properties).
        if (isset($callbacks[$path])) {
          $items[$child_path] += $callbacks[$path];
        }
      }
    }

    // If the path really exists, copy over its remaining properties.
    if (isset($callbacks[$path])) {
      $items[$path] += $callbacks[$path];
    }
    return $items;
  }

  /**
   * Build and render the page view.
   *
   * Since we replace an existing page, we need to invoke views_set_page_view().
   * Also set the page title, because original page callbacks might do this.
   */
  function execute() {
    views_set_page_view($this);

    // Prior to this being called, the $view should already be set to this
    // display, and arguments should be set on the view.
    $this->view
      ->build();
    if (!empty($this->view->build_info['fail'])) {
      return drupal_not_found();
    }

    // @todo Needed?
    $this->view
      ->get_breadcrumb(TRUE);
    drupal_set_title(filter_xss_admin($this->view
      ->get_title()));

    // @todo This is super-likely the totally wrong place to add our CSS; better
    //   suggestions and patches welcome.
    drupal_add_css(drupal_get_path('module', 'admin_views') . '/admin_views.css');
    return $this->view
      ->render();
  }

  /**
   * Provide the summary for page options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    parent::options_summary($categories, $options);
    $categories['system'] = array(
      'title' => t('System path settings'),
    );
    $path = $this
      ->get_option('path');
    if (empty($path)) {
      $path = t('None');
    }
    else {
      $path = truncate_utf8($path, 17, FALSE, TRUE);
    }
    $options['path'] = array(
      'category' => 'system',
      'title' => t('Path'),
      'value' => check_plain($path),
    );
  }

  /**
   * Provide the default form for setting options.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    switch ($form_state['section']) {
      case 'path':
        $form['#title'] .= t('An existing menu path this view replaces');
        $form['path'] = array(
          '#type' => 'textfield',
          '#description' => t('This view replaces this path on your site. You may use "%" for dynamic arguments. For example: "node/%/feed".'),
          '#default_value' => $this
            ->get_option('path'),
          '#field_prefix' => '<span dir="ltr">' . url(NULL, array(
            'absolute' => TRUE,
          )) . (variable_get('clean_url', 0) ? '' : '?q='),
          '#field_suffix' => '</span>&lrm;',
          '#attributes' => array(
            'dir' => 'ltr',
          ),
        );
        break;
    }
  }
  function options_validate(&$form, &$form_state) {
    parent::options_validate($form, $form_state);
    switch ($form_state['section']) {
      case 'path':
        if (strpos($form_state['values']['path'], '%') === 0) {
          form_error($form['path'], t('"%" may not be used for the first segment of a path.'));
        }

        // Automatically remove '/' from path.
        $form_state['values']['path'] = trim($form_state['values']['path'], '/');
        break;
    }
  }
  function options_submit(&$form, &$form_state) {
    parent::options_submit($form, $form_state);
    switch ($form_state['section']) {
      case 'path':
        $this
          ->set_option('path', $form_state['values']['path']);
        break;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_plugin_display_system::execute function Build and render the page view.
views_plugin_display_system::execute_hook_menu function Add this display's path information to Drupal's menu system.
views_plugin_display_system::has_path function The system display requires a path.
views_plugin_display_system::options_form function Provide the default form for setting options.
views_plugin_display_system::options_submit function
views_plugin_display_system::options_summary function Provide the summary for page options in the views UI.
views_plugin_display_system::options_validate function
views_plugin_display_system::option_definition function
views_plugin_display_system::uses_breadcrumb function