You are here

views_plugin_display_i18n_page.inc in i18n page views 6

Contains the i18n page display plugin.

File

views_plugin_display_i18n_page.inc
View source
<?php

/**
 * @file
 * Contains the i18n page display plugin.
 */

/**
 * The plugin that handles a full page.
 *
 * @ingroup views_display_plugins
 */
class views_plugin_display_i18n_page extends views_plugin_display {

  /**
   * The page display has a path.
   */
  function has_path() {
    return TRUE;
  }
  function uses_breadcrumb() {
    return TRUE;
  }
  function option_definition() {
    $options = parent::option_definition();
    $languages = locale_language_list();
    foreach ($languages as $key => $value) {
      $options['path_' . $key] = 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 -- which lives in views.module
    $languages = locale_language_list();
    foreach ($languages as $key => $value) {
      $bits = explode('/', $this
        ->get_option('path_' . $key));
      $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);
      if (method_exists($this, 'get_access_plugin')) {

        // views 2.x
        $access_plugin = $this
          ->get_access_plugin();
      }
      elseif (method_exists($this, 'get_plugin')) {

        // views 3.x
        $access_plugin = $this
          ->get_plugin('access');
      }
      if (!isset($access_plugin)) {
        $access_plugin = views_get_plugin('access', 'none');
      }
      if ($path) {
        $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',
          ),
        );
        $menu = array(
          'type' => 'none',
        );
        $items[$path]['type'] = MENU_CALLBACK;
      }
    }
    return $items;
  }

  /**
   * The display page handler returns a normal view, but it also does
   * a drupal_set_title for the page, and does a views_set_page_view
   * on the view.
   */
  function execute() {

    // Let the world know that this is the page view we're using.
    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();
    }
    $this->view
      ->get_breadcrumb(TRUE);

    // And the title, which is much easier.
    drupal_set_title(filter_xss_admin($this->view
      ->get_title()));

    // And now render the view.
    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) {

    // It is very important to call the parent function here:
    parent::options_summary($categories, $options);
    $categories['page'] = array(
      'title' => t('Page settings'),
    );
    $languages = locale_language_list();
    foreach ($languages as $key => $value) {
      $path = strip_tags($this
        ->get_option('path_' . $key));
      if (empty($path)) {
        $path = t('None');
      }
      if (strlen($path) > 16) {
        $path = substr($path, 0, 16) . '...';
      }
      $options['path_' . $key] = array(
        'category' => 'page',
        'title' => t('Path in %idiom', array(
          '%idiom' => $value,
        )),
        'value' => $path,
      );
    }
  }

  /**
   * Provide the default form for setting options.
   */
  function options_form(&$form, &$form_state) {

    // It is very important to call the parent function here:
    parent::options_form($form, $form_state);
    $key = $form_state['section'];
    if (stristr($key, 'path_') === FALSE) {
      return;
    }
    $form['#title'] .= t('The menu path or URL of this view');
    $form['#help_topic'] = 'path';
    $form[$key] = array(
      '#type' => 'textfield',
      '#description' => t('This view will be displayed by visiting this path on your site. You may use "%" in your URL to represent values that will be used for arguments: For example, "node/%/feed".'),
      '#default_value' => $this
        ->get_option($key),
      '#field_prefix' => '<span dir="ltr">' . url(NULL, array(
        'absolute' => TRUE,
      )) . (variable_get('clean_url', 0) ? '' : '?q='),
      '#field_suffix' => '</span>&lrm;',
      '#attributes' => array(
        'dir' => 'ltr',
      ),
    );
  }
  function options_validate(&$form, &$form_state) {

    // It is very important to call the parent function here:
    parent::options_validate($form, $form_state);
    $key = $form_state['section'];
    if (stristr($key, 'path_') === FALSE) {
      return;
    }
    if (strpos($form_state['values'][$key], '$arg') !== FALSE) {
      form_error($form[$key], t('"$arg" is no longer supported. Use % instead.'));
    }
    if (strpos($form_state['values'][$key], '%') === 0) {
      form_error($form[$key], t('"%" may not be used for the first segment of a path.'));
    }

    // automatically remove '/' from path.
    $form_state['values'][$key] = trim($form_state['values'][$key], '/');
  }
  function options_submit(&$form, &$form_state) {

    // It is very important to call the parent function here:
    parent::options_submit($form, $form_state);
    $key = $form_state['section'];
    $this
      ->set_option($key, $form_state['values'][$key]);
  }
  function validate() {
    $errors = parent::validate();
    $languages = locale_language_list();
    foreach ($languages as $key => $value) {
      if (!$this
        ->get_option('path_' . $key)) {
        return $errors;
      }
    }
  }

}

Classes

Namesort descending Description
views_plugin_display_i18n_page The plugin that handles a full page.