/** * {@inheritdoc} */ protected function getGallery() { $this->view->setDisplay($this->displayName); $this->view->setArguments($this->viewArgs); $rendered_view = $this->view->render(); // Make sure that the Juicebox is actually built. if (!empty($rendered_view['#rows']['#gallery']) && $rendered_view['#rows']['#gallery'] instanceof JuiceboxGalleryInterface && $rendered_view['#rows']['#gallery']->getId()) { return $rendered_view['#rows']['#gallery']; } throw new \Exception(t('Cannot build Juicebox XML for view-based gallery.')); }
/** * Constructs a \Drupal\views\Plugin\Block\ViewsBlockBase object. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\views\ViewExecutableFactory $executable_factory * The view executable factory. * @param \Drupal\Core\Entity\EntityStorageInterface $storage * The views storage. * @param \Drupal\Core\Session\AccountInterface $user * The current user. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, ViewExecutableFactory $executable_factory, EntityStorageInterface $storage, AccountInterface $user) { $this->pluginId = $plugin_id; $delta = $this->getDerivativeId(); list($name, $this->displayID) = explode('-', $delta, 2); // Load the view. $view = $storage->load($name); $this->view = $executable_factory->get($view); $this->displaySet = $this->view->setDisplay($this->displayID); $this->user = $user; parent::__construct($configuration, $plugin_id, $plugin_definition); }
/** * Generates a grid and asserts that it is displaying correctly. * * @param \Drupal\views\ViewExecutable $view * The executable to prepare. * @param string $alignment * The alignment of the grid to test. * @param int $columns * The number of columns in the grid to test. */ protected function assertGrid(ViewExecutable $view, $alignment, $columns) { $view->setDisplay('default'); $view->initStyle(); $view->initHandlers(); $view->initQuery(); $view->style_plugin->options['alignment'] = $alignment; $view->style_plugin->options['columns'] = $columns; $this->executeView($view); $output = $view->preview(); $output = drupal_render($output); $this->setRawContent($output); if (!in_array($alignment, $this->alignmentsTested)) { $result = $this->xpath('//div[contains(@class, "views-view-grid") and contains(@class, :alignment) and contains(@class, :columns)]', array(':alignment' => $alignment, ':columns' => 'cols-' . $columns)); $this->assertTrue(count($result), ucfirst($alignment) . " grid markup detected."); $this->alignmentsTested[] = $alignment; } $width = '0'; switch ($columns) { case 5: $width = '20'; break; case 4: $width = '25'; break; case 3: $width = '33.3333'; break; case 2: $width = '50'; break; case 1: $width = '100'; break; } // Ensure last column exists. $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns) and starts-with(@style, :width)]', array(':columns' => 'col-' . $columns, ':width' => 'width: ' . $width)); $this->assertTrue(count($result), ucfirst($alignment) . " {$columns} column grid: last column exists and automatic width calculated correctly."); // Ensure no extra columns were generated. $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns)]', array(':columns' => 'col-' . ($columns + 1))); $this->assertFalse(count($result), ucfirst($alignment) . " {$columns} column grid: no extraneous columns exist."); // Ensure tokens are being replaced in custom row/column classes. $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, "name-John")]'); $this->assertTrue(count($result), ucfirst($alignment) . " {$columns} column grid: Token replacement verified in custom column classes."); $result = $this->xpath('//div[contains(@class, "views-row") and contains(@class, "age-25")]'); $this->assertTrue(count($result), ucfirst($alignment) . " {$columns} column grid: Token replacement verified in custom row classes."); }
/** * Executes a view with debugging. * * @param \Drupal\views\ViewExecutable $view * The view object. * @param array $args * (optional) An array of the view arguments to use for the view. */ protected function executeView(ViewExecutable $view, $args = array()) { // A view does not really work outside of a request scope, due to many // dependencies like the current user. $view->setDisplay(); $view->preExecute($args); $view->execute(); $verbose_message = '<pre>Executed view: ' . (string) $view->build_info['query'] . '</pre>'; if ($view->build_info['query'] instanceof SelectInterface) { $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>'; } $this->verbose($verbose_message); }
public function renderPreview($display_id, $args = array()) { // Save the current path so it can be restored before returning from this function. $old_q = current_path(); // Determine where the query and performance statistics should be output. $config = \Drupal::config('views.settings'); $show_query = $config->get('ui.show.sql_query.enabled'); $show_info = $config->get('ui.show.preview_information'); $show_location = $config->get('ui.show.sql_query.where'); $show_stats = $config->get('ui.show.performance_statistics'); if ($show_stats) { $show_stats = $config->get('ui.show.sql_query.where'); } $combined = $show_query && $show_stats; $rows = array('query' => array(), 'statistics' => array()); $output = ''; $errors = $this->executable->validate(); $this->executable->destroy(); if (empty($errors)) { $this->ajax = TRUE; $this->executable->live_preview = TRUE; // AJAX happens via HTTP POST but everything expects exposed data to // be in GET. Copy stuff but remove ajax-framework specific keys. // If we're clicking on links in a preview, though, we could actually // have some input in the query parameters, so we merge request() and // query() to ensure we get it all. $exposed_input = array_merge(\Drupal::request()->request->all(), \Drupal::request()->query->all()); foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', 'ajax_html_ids', 'ajax_page_state', 'form_id', 'form_build_id', 'form_token') as $key) { if (isset($exposed_input[$key])) { unset($exposed_input[$key]); } } $this->executable->setExposedInput($exposed_input); if (!$this->executable->setDisplay($display_id)) { return t('Invalid display id @display', array('@display' => $display_id)); } $this->executable->setArguments($args); // Store the current view URL for later use: if ($this->executable->display_handler->getOption('path')) { $path = $this->executable->getUrl(); } // Make view links come back to preview. $this->override_path = 'admin/structure/views/view/' . $this->id() . '/preview/' . $display_id; // Also override the current path so we get the pager. $original_path = current_path(); $q = _current_path($this->override_path); if ($args) { $q .= '/' . implode('/', $args); _current_path($q); } // Suppress contextual links of entities within the result set during a // Preview. // @todo We'll want to add contextual links specific to editing the View, so // the suppression may need to be moved deeper into the Preview pipeline. views_ui_contextual_links_suppress_push(); $show_additional_queries = $config->get('ui.show.additional_queries'); Timer::start('entity.view.preview_form'); if ($show_additional_queries) { $this->startQueryCapture(); } // Execute/get the view preview. $preview = $this->executable->preview($display_id, $args); $preview = drupal_render($preview); if ($show_additional_queries) { $this->endQueryCapture(); } $this->render_time = Timer::stop('entity.view.preview_form'); views_ui_contextual_links_suppress_pop(); // Reset variables. unset($this->override_path); _current_path($original_path); // Prepare the query information and statistics to show either above or // below the view preview. if ($show_info || $show_query || $show_stats) { // Get information from the preview for display. if (!empty($this->executable->build_info['query'])) { if ($show_query) { $query_string = $this->executable->build_info['query']; // Only the sql default class has a method getArguments. $quoted = array(); if ($this->executable->query instanceof Sql) { $quoted = $query_string->getArguments(); $connection = Database::getConnection(); foreach ($quoted as $key => $val) { if (is_array($val)) { $quoted[$key] = implode(', ', array_map(array($connection, 'quote'), $val)); } else { $quoted[$key] = $connection->quote($val); } } } $rows['query'][] = array(array('data' => array('#type' => 'inline_template', '#template' => "<strong>{% trans 'Query' %}</strong>")), array('data' => array('#type' => 'inline_template', '#template' => '<pre>{{ query }}</pre>', '#context' => array('query' => strtr($query_string, $quoted))))); if (!empty($this->additionalQueries)) { $queries = '<strong>' . t('These queries were run during view rendering:') . '</strong>'; foreach ($this->additionalQueries as $query) { if ($queries) { $queries .= "\n"; } $query_string = strtr($query['query'], $query['args']); $queries .= t('[@time ms] @query', array('@time' => round($query['time'] * 100000, 1) / 100000.0, '@query' => $query_string)); } $rows['query'][] = array(array('data' => array('#type' => 'inline_template', '#template' => "<strong>{% trans 'Other queries' %}</strong>")), SafeMarkup::set('<pre>' . $queries . '</pre>')); } } if ($show_info) { $rows['query'][] = array(array('data' => array('#type' => 'inline_template', '#template' => "<strong>{% trans 'Title' %}</strong>")), Xss::filterAdmin($this->executable->getTitle())); if (isset($path)) { $path = _l($path, $path); } else { $path = t('This display has no path.'); } $rows['query'][] = array(SafeMarkup::set('<strong>' . t('Path') . '</strong>'), $path); } if ($show_stats) { $rows['statistics'][] = array('<strong>' . t('Query build time') . '</strong>', t('@time ms', array('@time' => intval($this->executable->build_time * 100000) / 100))); $rows['statistics'][] = array('<strong>' . t('Query execute time') . '</strong>', t('@time ms', array('@time' => intval($this->executable->execute_time * 100000) / 100))); $rows['statistics'][] = array('<strong>' . t('View render time') . '</strong>', t('@time ms', array('@time' => intval($this->executable->render_time * 100000) / 100))); } \Drupal::moduleHandler()->alter('views_preview_info', $rows, $this->executable); } else { // No query was run. Display that information in place of either the // query or the performance statistics, whichever comes first. if ($combined || $show_location === 'above') { $rows['query'] = array(array(SafeMarkup::set('<strong>' . t('Query') . '</strong>'), t('No query was run'))); } else { $rows['statistics'] = array(array(SafeMarkup::set('<strong>' . t('Query') . '</strong>'), t('No query was run'))); } } } } else { foreach ($errors as $display_errors) { foreach ($display_errors as $error) { drupal_set_message($error, 'error'); } } $preview = t('Unable to preview due to validation errors.'); } // Assemble the preview, the query info, and the query statistics in the // requested order. $table = array('#type' => 'table', '#prefix' => '<div class="views-query-info">', '#suffix' => '</div>'); if ($show_location === 'above' || $show_location === 'below') { if ($combined) { $table['#rows'] = array_merge($rows['query'], $rows['statistics']); } else { $table['#rows'] = $rows['query']; } } elseif ($show_stats === 'above' || $show_stats === 'below') { $table['#rows'] = $rows['statistics']; } if ($show_location === 'above' || $show_stats === 'above') { $output .= drupal_render($table) . $preview; } elseif ($show_location === 'below' || $show_stats === 'below') { $output .= $preview . drupal_render($table); } _current_path($old_q); return $output; }
/** * Attach to another view. */ public function attachTo(ViewExecutable $view, $display_id) { $displays = $this->getOption('displays'); if (empty($displays[$display_id])) { return; } if (!$this->access()) { return; } $args = $this->getOption('inherit_arguments') ? $this->view->args : array(); $view->setArguments($args); $view->setDisplay($this->display['id']); if ($this->getOption('inherit_pager')) { $view->display_handler->usesPager = $this->view->displayHandlers->get($display_id)->usesPager(); $view->display_handler->setOption('pager', $this->view->displayHandlers->get($display_id)->getOption('pager')); } $attachment = $view->executeDisplay($this->display['id'], $args); switch ($this->getOption('attachment_position')) { case 'before': $this->view->attachment_before[] = $attachment; break; case 'after': $this->view->attachment_after[] = $attachment; break; case 'both': $this->view->attachment_before[] = $attachment; $this->view->attachment_after[] = $attachment; break; } }
/** * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::attachTo(). */ public function attachTo(ViewExecutable $clone, $display_id) { $displays = $this->getOption('displays'); if (empty($displays[$display_id])) { return; } // Defer to the feed style; it may put in meta information, and/or // attach a feed icon. $clone->setDisplay($this->display['id']); $clone->buildTitle(); if ($plugin = $clone->display_handler->getPlugin('style')) { $plugin->attachTo($display_id, $this->getPath(), $clone->getTitle()); } // Clean up. $clone->destroy(); unset($clone); }
/** * Executes a view with debugging. * * @param \Drupal\views\ViewExecutable $view * The view object. * @param array $args * (optional) An array of the view arguments to use for the view. */ protected function executeView($view, array $args = array()) { $view->setDisplay(); $view->preExecute($args); $view->execute(); $verbose_message = '<pre>Executed view: ' . (string) $view->build_info['query'] . '</pre>'; if ($view->build_info['query'] instanceof SelectInterface) { $verbose_message .= '<pre>Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '</pre>'; } $this->verbose($verbose_message); }
/** * Prepares a view executable by initializing everything which is needed. * * @param \Drupal\views\ViewExecutable $view * The executable to prepare. */ protected function prepareView(ViewExecutable $view) { $view->setDisplay(); $view->initStyle(); $view->initHandlers(); $view->initQuery(); }
/** * {@inheritdoc} */ public function attachTo(ViewExecutable $clone, $display_id, array &$build) { $displays = $this->getOption('displays'); if (empty($displays[$display_id])) { return; } // Defer to the feed style; it may put in meta information, and/or // attach a feed icon. $clone->setArguments($this->view->args); $clone->setDisplay($this->display['id']); $clone->buildTitle(); if ($plugin = $clone->display_handler->getPlugin('style')) { $plugin->attachTo($build, $display_id, $clone->getUrl(), $clone->getTitle()); foreach ($clone->feedIcons as $feed_icon) { $this->view->feedIcons[] = $feed_icon; } } // Clean up. $clone->destroy(); unset($clone); }