You are here

class views_lazy_load_plugin_display_extender in Views Lazy Load 7

Same name and namespace in other branches
  1. 8 includes/views/views_lazy_load_plugin_display_extender.inc \views_lazy_load_plugin_display_extender

@file A display extender plugin for lazy loading a view.

Hierarchy

Expanded class hierarchy of views_lazy_load_plugin_display_extender

1 string reference to 'views_lazy_load_plugin_display_extender'
views_lazy_load_views_plugins in includes/views/views_lazy_load.views.inc
Implements hook_views_plugins().

File

includes/views/views_lazy_load_plugin_display_extender.inc, line 7
A display extender plugin for lazy loading a view.

View source
class views_lazy_load_plugin_display_extender extends views_plugin_display_extender {

  /**
   * {@inheritdoc}
   */
  public function pre_execute() {
    if ($this
      ->isEnabled() && empty($this->view->live_preview)) {
      if (!$this
        ->isExcludedUserAgent()) {

        // Lazy loading requires an AJAX view.
        $this->view
          ->set_use_ajax(TRUE);

        // Marking the view as executed prevents the Search API querying.
        $this->view->executed = TRUE;

        // Add the loading text in the area plugin.
        $this
          ->addLoadingArea();

        // Add our JavaScript to the page with an array of dom_ids that can be
        // used client side to retrieve the view instance.
        $path = drupal_get_path('module', 'views_lazy_load');
        $settings[] = $this->view->dom_id;
        drupal_add_js(array(
          'views_lazy_load' => $settings,
        ), 'setting');
        drupal_add_js($path . '/js/views-lazy-load.js', array(
          'scope' => 'footer',
          'weight' => 10,
        ));
      }
      else {

        // Ensure that AJAX won't be used if a crawler is visiting the view
        $this->view
          ->set_use_ajax(FALSE);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  function options_definition_alter(&$options) {
    $options['views_lazy_load_enabled'] = array(
      'default' => FALSE,
    );
  }

  /**
   * Check the user agent string to see if it's one of our excluded agents.
   *
   * @return bool
   *   TRUE if the current user agent should be excluded otherwise FALSE.
   */
  function isExcludedUserAgent() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $excluded_user_agents = views_lazy_load_get_excluded_user_agents();
    return (bool) preg_match("/{$excluded_user_agents}/i", $user_agent);
  }

  /**
   * {@inheritdoc}
   */
  function options_form(&$form, &$form_state) {
    if ($form_state['section'] == 'views_lazy_load') {
      $form['#title'] .= t('Enable Views Lazy Load');
      $form['views_lazy_load_enabled'] = array(
        '#title' => t('Enabled'),
        '#description' => t('Enabling Views Lazy Load will cause the view to be loaded via AJAX after the initial page load. "Use AJAX" will be enabled for you.'),
        '#type' => 'checkbox',
        '#default_value' => $this
          ->isEnabled(),
      );
    }
  }

  /**
   * {@inheritdoc}
   */
  function options_validate(&$form, &$form_state) {
    if ($form_state['section'] === 'use_ajax') {
      if (empty($form_state['values']['use_ajax']) && $this
        ->isEnabled()) {
        form_set_error('use_ajax', 'You cannot disable AJAX when using Views Lazy Load');
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  function options_submit(&$form, &$form_state) {
    if ($form_state['section'] == 'views_lazy_load') {
      $views_lazy_enabled = $form_state['values']['views_lazy_load_enabled'];
      $this->display
        ->set_option('views_lazy_load_enabled', $views_lazy_enabled);

      // We enable use AJAX as that is required for VLL.
      $this->display
        ->set_option('use_ajax', TRUE);
    }
  }

  /**
   * {@inheritdoc}
   */
  function options_summary(&$categories, &$options) {
    $options['views_lazy_load'] = array(
      'category' => 'other',
      'title' => t('Views Lazy Loading'),
      'value' => $this
        ->isEnabled() ? t('Enabled') : t('Disabled'),
    );
  }

  /**
   * Gets the enabled status of lazy loading.
   *
   * @return bool
   *   TRUE if the lazy load setting is enabled otherwise FALSE.
   */
  protected function isEnabled() {

    // We check this query param which was set client side so we can reuse the
    // views_ajax() function which actually does quite a lot for us.
    return $this->display
      ->get_option('views_lazy_load_enabled') && !isset($_GET['views_lazy_load_disabled']);
  }

  /**
   * Add a loading div to the view while we're loading the results.
   */
  protected function addLoadingArea() {

    // Add a empty area plugin to this view display so it gets initialised.
    $display_id = $this->display->display->id;
    $handler_id = $this->view
      ->add_item($display_id, 'empty', 'views', 'area', array(
      'empty' => TRUE,
      'format' => 'full_html',
      'content' => theme('views_lazy_load_throbber'),
    ));

    // Now get the area plugin info back and initialise an area plugin.
    $empty_info = $this->display
      ->get_option('empty');
    $area = views_get_handler('views', 'area', 'area');
    $area
      ->init($this->view, $empty_info[$handler_id]);

    // We must add the area directly to the display_handler to make sure it gets
    // rendered.
    $this->display->handlers['empty'][$handler_id] = $area;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_lazy_load_plugin_display_extender::addLoadingArea protected function Add a loading div to the view while we're loading the results.
views_lazy_load_plugin_display_extender::isEnabled protected function Gets the enabled status of lazy loading.
views_lazy_load_plugin_display_extender::isExcludedUserAgent function Check the user agent string to see if it's one of our excluded agents.
views_lazy_load_plugin_display_extender::options_definition_alter function Provide a form to edit options for this plugin. Overrides views_plugin_display_extender::options_definition_alter
views_lazy_load_plugin_display_extender::options_form function Provide a form to edit options for this plugin. Overrides views_plugin_display_extender::options_form
views_lazy_load_plugin_display_extender::options_submit function Handle any special handling on the validate form. Overrides views_plugin_display_extender::options_submit
views_lazy_load_plugin_display_extender::options_summary function Provide the default summary for options in the views UI. Overrides views_plugin_display_extender::options_summary
views_lazy_load_plugin_display_extender::options_validate function Validate the options form. Overrides views_plugin_display_extender::options_validate
views_lazy_load_plugin_display_extender::pre_execute public function Set up any variables on the view prior to execution. Overrides views_plugin_display_extender::pre_execute
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::option_definition public function Information about options for all kinds of purposes will be held here. 13
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_display_extender::defaultable_sections public function Static member function to list which sections are defaultable and what items each section contains.
views_plugin_display_extender::init public function
views_plugin_display_extender::query public function Inject anything into the query that the display_extender handler needs. Overrides views_plugin::query