Example #1
0
 /**
  * Provide the default form for setting options.
  */
 public function buildOptionsForm(&$form, FormStateInterface $form_state)
 {
     parent::buildOptionsForm($form, $form_state);
     $pager_text = $this->displayHandler->getPagerText();
     $form['items_per_page'] = array('#title' => $pager_text['items per page title'], '#type' => 'textfield', '#description' => $pager_text['items per page description'], '#default_value' => $this->options['items_per_page']);
     $form['offset'] = array('#type' => 'textfield', '#title' => $this->t('Offset (number of items to skip)'), '#description' => $this->t('For example, set this to 3 and the first 3 items will not be displayed.'), '#default_value' => $this->options['offset']);
 }
Example #2
0
 /**
  * Tests the executeCountQuery method with a set offset.
  *
  * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::executeCountQuery()
  */
 public function testExecuteCountQueryWithOffset()
 {
     $statement = $this->getMock('\\Drupal\\Tests\\views\\Unit\\Plugin\\pager\\TestStatementInterface');
     $statement->expects($this->once())->method('fetchField')->will($this->returnValue(3));
     $query = $this->getMockBuilder('\\Drupal\\Core\\Database\\Query\\Select')->disableOriginalConstructor()->getMock();
     $query->expects($this->once())->method('execute')->will($this->returnValue($statement));
     $this->pager->setOffset(2);
     $this->assertEquals(1, $this->pager->executeCountQuery($query));
 }
Example #3
0
 /**
  * Provide the default form for setting options.
  */
 public function buildOptionsForm(&$form, FormStateInterface $form_state)
 {
     parent::buildOptionsForm($form, $form_state);
     $pager_text = $this->displayHandler->getPagerText();
     $form['items_per_page'] = array('#title' => $pager_text['items per page title'], '#type' => 'number', '#description' => $pager_text['items per page description'], '#default_value' => $this->options['items_per_page']);
     $form['offset'] = array('#type' => 'number', '#title' => $this->t('Offset (number of items to skip)'), '#description' => $this->t('For example, set this to 3 and the first 3 items will not be displayed.'), '#default_value' => $this->options['offset']);
     $form['id'] = array('#type' => 'number', '#title' => $this->t('Pager ID'), '#description' => $this->t("Unless you're experiencing problems with pagers related to this view, you should leave this at 0. If using multiple pagers on one page you may need to set this number to a higher value so as not to conflict within the ?page= array. Large values will add a lot of commas to your URLs, so avoid if possible."), '#default_value' => $this->options['id']);
     $form['total_pages'] = array('#type' => 'number', '#title' => $this->t('Number of pages'), '#description' => $this->t('The total number of pages. Leave empty to show all pages.'), '#default_value' => $this->options['total_pages']);
     $form['tags'] = array('#type' => 'details', '#open' => TRUE, '#tree' => TRUE, '#title' => $this->t('Pager link labels'), '#input' => TRUE);
     $form['tags']['previous'] = array('#type' => 'textfield', '#title' => $this->t('Previous page link text'), '#default_value' => $this->options['tags']['previous']);
     $form['tags']['next'] = array('#type' => 'textfield', '#title' => $this->t('Next page link text'), '#default_value' => $this->options['tags']['next']);
     $form['expose'] = array('#type' => 'details', '#open' => TRUE, '#tree' => TRUE, '#title' => $this->t('Exposed options'), '#input' => TRUE, '#description' => $this->t('Exposing this options allows users to define their values in a exposed form when view is displayed'));
     $form['expose']['items_per_page'] = array('#type' => 'checkbox', '#title' => $this->t('Expose items per page'), '#description' => $this->t('When checked, users can determine how many items per page show in a view'), '#default_value' => $this->options['expose']['items_per_page']);
     $form['expose']['items_per_page_label'] = array('#type' => 'textfield', '#title' => $this->t('Items per page label'), '#required' => TRUE, '#description' => $this->t('Label to use in the exposed items per page form element.'), '#default_value' => $this->options['expose']['items_per_page_label'], '#states' => array('invisible' => array('input[name="pager_options[expose][items_per_page]"]' => array('checked' => FALSE))));
     $form['expose']['items_per_page_options'] = array('#type' => 'textfield', '#title' => $this->t('Exposed items per page options'), '#required' => TRUE, '#description' => $this->t('Set between which values the user can choose when determining the items per page. Separated by comma.'), '#default_value' => $this->options['expose']['items_per_page_options'], '#states' => array('invisible' => array('input[name="pager_options[expose][items_per_page]"]' => array('checked' => FALSE))));
     $form['expose']['items_per_page_options_all'] = array('#type' => 'checkbox', '#title' => $this->t('Include all items option'), '#description' => $this->t('If checked, an extra item will be included to items per page to display all items'), '#default_value' => $this->options['expose']['items_per_page_options_all']);
     $form['expose']['items_per_page_options_all_label'] = array('#type' => 'textfield', '#title' => $this->t('All items label'), '#description' => $this->t('Which label will be used to display all items'), '#default_value' => $this->options['expose']['items_per_page_options_all_label'], '#states' => array('invisible' => array('input[name="pager_options[expose][items_per_page_options_all]"]' => array('checked' => FALSE))));
     $form['expose']['offset'] = array('#type' => 'checkbox', '#title' => $this->t('Expose Offset'), '#description' => $this->t('When checked, users can determine how many items should be skipped at the beginning.'), '#default_value' => $this->options['expose']['offset']);
     $form['expose']['offset_label'] = array('#type' => 'textfield', '#title' => $this->t('Offset label'), '#required' => TRUE, '#description' => $this->t('Label to use in the exposed offset form element.'), '#default_value' => $this->options['expose']['offset_label'], '#states' => array('invisible' => array('input[name="pager_options[expose][offset]"]' => array('checked' => FALSE))));
 }
Example #4
0
 /**
  * Tests a counter field on the second page.
  *
  * @param int $i
  *   The row index to test.
  *
  * @dataProvider providerRowIndexes
  */
 public function testCounterSecondPage($i)
 {
     $offset = 3;
     // Setup a pager on the second page.
     $this->pager->setOffset($offset);
     $items_per_page = 5;
     $this->pager->setItemsPerPage($items_per_page);
     $current_page = 1;
     $this->pager->setCurrentPage($current_page);
     $rand_start = rand(5, 10);
     $counter_handler = new Counter(array(), 'counter', $this->definition);
     $options = array('counter_start' => $rand_start);
     $counter_handler->init($this->view, $this->display, $options);
     $this->view->row_index = $i;
     $expected = $items_per_page + $offset + $rand_start + $i;
     $counter = $counter_handler->getValue($this->testData[$i]);
     $this->assertEquals($expected, $counter, 'The expected number matches with the counter number');
     $counter = $this->renderCounter($counter_handler, $this->testData[$i]);
     $this->assertEquals($expected, $counter, 'The expected number matches with the rendered number');
 }
Example #5
0
 /**
  * Render this view for a certain display.
  *
  * Note: You should better use just the preview function if you want to
  * render a view.
  *
  * @param string $display_id
  *   The machine name of the display, which should be rendered.
  *
  * @return string|null
  *   Return the output of the rendered view or NULL if something failed in the process.
  */
 public function render($display_id = NULL)
 {
     $this->execute($display_id);
     // Check to see if the build failed.
     if (!empty($this->build_info['fail'])) {
         return;
     }
     if (!empty($this->build_info['denied'])) {
         return;
     }
     $exposed_form = $this->display_handler->getPlugin('exposed_form');
     $exposed_form->preRender($this->result);
     $module_handler = \Drupal::moduleHandler();
     // @TODO In the longrun, it would be great to execute a view without
     //   the theme system at all. See https://www.drupal.org/node/2322623.
     $active_theme = \Drupal::theme()->getActiveTheme();
     $themes = array_keys($active_theme->getBaseThemes());
     $themes[] = $active_theme->getName();
     // Check for already-cached output.
     if (!empty($this->live_preview)) {
         $cache = FALSE;
     } else {
         /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
         $cache = $this->display_handler->getPlugin('cache');
     }
     // Run preRender for the pager as it might change the result.
     if (!empty($this->pager)) {
         $this->pager->preRender($this->result);
     }
     // Initialize the style plugin.
     $this->initStyle();
     if (!isset($this->response)) {
         // Set the response so other parts can alter it.
         $this->response = new Response('', 200);
     }
     // Give field handlers the opportunity to perform additional queries
     // using the entire resultset prior to rendering.
     if ($this->style_plugin->usesFields()) {
         foreach ($this->field as $id => $handler) {
             if (!empty($this->field[$id])) {
                 $this->field[$id]->preRender($this->result);
             }
         }
     }
     $this->style_plugin->preRender($this->result);
     // Let each area handler have access to the result set.
     $areas = array('header', 'footer');
     // Only call preRender() on the empty handlers if the result is empty.
     if (empty($this->result)) {
         $areas[] = 'empty';
     }
     foreach ($areas as $area) {
         foreach ($this->{$area} as $handler) {
             $handler->preRender($this->result);
         }
     }
     // Let modules modify the view just prior to rendering it.
     $module_handler->invokeAll('views_pre_render', array($this));
     // Let the themes play too, because pre render is a very themey thing.
     foreach ($themes as $theme_name) {
         $function = $theme_name . '_views_pre_render';
         if (function_exists($function)) {
             $function($this);
         }
     }
     $this->display_handler->output = $this->display_handler->render();
     $exposed_form->postRender($this->display_handler->output);
     if ($cache) {
         $cache->postRender($this->display_handler->output);
     }
     // Let modules modify the view output after it is rendered.
     $module_handler->invokeAll('views_post_render', array($this, &$this->display_handler->output, $cache));
     // Let the themes play too, because post render is a very themey thing.
     foreach ($themes as $theme_name) {
         $function = $theme_name . '_views_post_render';
         if (function_exists($function)) {
             $function($this);
         }
     }
     return $this->display_handler->output;
 }
Example #6
0
 /**
  * Render this view for a certain display.
  *
  * Note: You should better use just the preview function if you want to
  * render a view.
  *
  * @param string $display_id
  *   The machine name of the display, which should be rendered.
  *
  * @return string|null
  *   Return the output of the rendered view or NULL if something failed in the process.
  */
 public function render($display_id = NULL)
 {
     $this->execute($display_id);
     // Check to see if the build failed.
     if (!empty($this->build_info['fail'])) {
         return;
     }
     if (!empty($this->build_info['denied'])) {
         return;
     }
     $exposed_form = $this->display_handler->getPlugin('exposed_form');
     $exposed_form->preRender($this->result);
     $module_handler = \Drupal::moduleHandler();
     // Check for already-cached output.
     if (!empty($this->live_preview)) {
         $cache = FALSE;
     } else {
         $cache = $this->display_handler->getPlugin('cache');
     }
     if ($cache && $cache->cacheGet('output')) {
     } else {
         if ($cache) {
             $cache->cacheStart();
         }
         // Run preRender for the pager as it might change the result.
         if (!empty($this->pager)) {
             $this->pager->preRender($this->result);
         }
         // Initialize the style plugin.
         $this->initStyle();
         if (!isset($this->response)) {
             // Set the response so other parts can alter it.
             $this->response = new Response('', 200);
         }
         // Give field handlers the opportunity to perform additional queries
         // using the entire resultset prior to rendering.
         if ($this->style_plugin->usesFields()) {
             foreach ($this->field as $id => $handler) {
                 if (!empty($this->field[$id])) {
                     $this->field[$id]->preRender($this->result);
                 }
             }
         }
         $this->style_plugin->preRender($this->result);
         // Let each area handler have access to the result set.
         $areas = array('header', 'footer');
         // Only call preRender() on the empty handlers if the result is empty.
         if (empty($this->result)) {
             $areas[] = 'empty';
         }
         foreach ($areas as $area) {
             foreach ($this->{$area} as $handler) {
                 $handler->preRender($this->result);
             }
         }
         // Let modules modify the view just prior to rendering it.
         $module_handler->invokeAll('views_pre_render', array($this));
         // Let the themes play too, because pre render is a very themey thing.
         if (isset($GLOBALS['base_theme_info']) && isset($GLOBALS['theme'])) {
             foreach ($GLOBALS['base_theme_info'] as $base) {
                 $module_handler->invoke($base->getName(), 'views_pre_render', array($this));
             }
             $module_handler->invoke($GLOBALS['theme'], 'views_pre_render', array($this));
         }
         $this->display_handler->output = $this->display_handler->render();
         if ($cache) {
             $cache->cacheSet('output');
         }
     }
     $exposed_form->postRender($this->display_handler->output);
     if ($cache) {
         $cache->postRender($this->display_handler->output);
     }
     // Let modules modify the view output after it is rendered.
     $module_handler->invokeAll('views_post_render', array($this, &$this->display_handler->output, $cache));
     // Let the themes play too, because post render is a very themey thing.
     if (isset($GLOBALS['base_theme_info']) && isset($GLOBALS['theme'])) {
         foreach ($GLOBALS['base_theme_info'] as $base) {
             $module_handler->invoke($base->getName(), 'views_post_render', array($this));
         }
         $module_handler->invoke($GLOBALS['theme'], 'views_post_render', array($this));
     }
     return $this->display_handler->output;
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 protected function defineOptions()
 {
     $options = parent::defineOptions();
     $options['exclude_display'] = array('default' => FALSE);
     return $options;
 }
Example #8
0
 /**
  * Provide the default form for setting options.
  */
 public function buildOptionsForm(&$form, &$form_state)
 {
     parent::buildOptionsForm($form, $form_state);
     $form['offset'] = array('#type' => 'textfield', '#title' => t('Offset (number of items to skip)'), '#description' => t('For example, set this to 3 and the first 3 items will not be displayed.'), '#default_value' => $this->options['offset']);
 }