Chris@0: getValues(). Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: public $exposed_data = []; Chris@0: Chris@0: /** Chris@0: * An array of input values from exposed forms. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $exposed_input = []; Chris@0: Chris@0: /** Chris@0: * Exposed widget input directly from the $form_state->getValues(). Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: public $exposed_raw_input = []; Chris@0: Chris@0: /** Chris@0: * Used to store views that were previously running if we recurse. Chris@0: * Chris@0: * @var \Drupal\views\ViewExecutable[] Chris@0: */ Chris@0: public $old_view = []; Chris@0: Chris@0: /** Chris@0: * To avoid recursion in views embedded into areas. Chris@0: * Chris@0: * @var \Drupal\views\ViewExecutable[] Chris@0: */ Chris@0: public $parent_views = []; Chris@0: Chris@0: /** Chris@0: * Whether this view is an attachment to another view. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: public $is_attachment = NULL; Chris@0: Chris@0: /** Chris@0: * Identifier of the current display. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: public $current_display; Chris@0: Chris@0: /** Chris@0: * Where the $query object will reside. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\query\QueryPluginBase Chris@0: */ Chris@0: public $query = NULL; Chris@0: Chris@0: /** Chris@0: * The used pager plugin used by the current executed view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\pager\PagerPluginBase Chris@0: */ Chris@0: public $pager = NULL; Chris@0: Chris@0: /** Chris@0: * The current used display plugin. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\display\DisplayPluginBase Chris@0: */ Chris@0: public $display_handler; Chris@0: Chris@0: /** Chris@0: * The list of used displays of the view. Chris@0: * Chris@0: * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase Chris@0: * objects. Chris@0: * Chris@0: * @var \Drupal\views\DisplayPluginCollection Chris@0: */ Chris@0: public $displayHandlers; Chris@0: Chris@0: /** Chris@0: * The current used style plugin. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\style\StylePluginBase Chris@0: */ Chris@0: public $style_plugin; Chris@0: Chris@0: /** Chris@0: * The current used row plugin, if the style plugin supports row plugins. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\row\RowPluginBase Chris@0: */ Chris@0: public $rowPlugin; Chris@0: Chris@0: /** Chris@0: * Stores the current active row while rendering. Chris@0: * Chris@0: * @var int Chris@0: */ Chris@0: public $row_index; Chris@0: Chris@0: /** Chris@0: * Allow to override the url of the current view. Chris@0: * Chris@0: * @var \Drupal\Core\Url Chris@0: */ Chris@0: public $override_url; Chris@0: Chris@0: /** Chris@0: * Allow to override the path used for generated urls. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: public $override_path = NULL; Chris@0: Chris@0: /** Chris@0: * Allow to override the used database which is used for this query. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: public $base_database = NULL; Chris@0: Chris@0: // Handlers which are active on this view. Chris@0: Chris@0: /** Chris@0: * Stores the field handlers which are initialized on this view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\field\FieldPluginBase[] Chris@0: */ Chris@0: public $field; Chris@0: Chris@0: /** Chris@0: * Stores the argument handlers which are initialized on this view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase[] Chris@0: */ Chris@0: public $argument; Chris@0: Chris@0: /** Chris@0: * Stores the sort handlers which are initialized on this view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\sort\SortPluginBase[] Chris@0: */ Chris@0: public $sort; Chris@0: Chris@0: /** Chris@0: * Stores the filter handlers which are initialized on this view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\filter\FilterPluginBase[] Chris@0: */ Chris@0: public $filter; Chris@0: Chris@0: /** Chris@0: * Stores the relationship handlers which are initialized on this view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\relationship\RelationshipPluginBase[] Chris@0: */ Chris@0: public $relationship; Chris@0: Chris@0: /** Chris@0: * Stores the area handlers for the header which are initialized on this view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\area\AreaPluginBase[] Chris@0: */ Chris@0: public $header; Chris@0: Chris@0: /** Chris@0: * Stores the area handlers for the footer which are initialized on this view. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\area\AreaPluginBase[] Chris@0: */ Chris@0: public $footer; Chris@0: Chris@0: /** Chris@0: * Stores the area handlers for the empty text which are initialized on this view. Chris@0: * Chris@0: * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects. Chris@0: * Chris@0: * @var \Drupal\views\Plugin\views\area\AreaPluginBase[] Chris@0: */ Chris@0: public $empty; Chris@0: Chris@0: /** Chris@0: * Stores the current response object. Chris@0: * Chris@0: * @var \Symfony\Component\HttpFoundation\Response Chris@0: */ Chris@0: protected $response = NULL; Chris@0: Chris@0: /** Chris@0: * Stores the current request object. Chris@0: * Chris@0: * @var \Symfony\Component\HttpFoundation\Request Chris@0: */ Chris@0: protected $request; Chris@0: Chris@0: /** Chris@14: * Does this view already have loaded its handlers. Chris@0: * Chris@0: * @todo Group with other static properties. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: public $inited; Chris@0: Chris@0: /** Chris@0: * The rendered output of the exposed form. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: public $exposed_widgets; Chris@0: Chris@0: /** Chris@0: * If this view has been previewed. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: public $preview; Chris@0: Chris@0: /** Chris@0: * Force the query to calculate the total number of results. Chris@0: * Chris@0: * @todo Move to the query. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: public $get_total_rows; Chris@0: Chris@0: /** Chris@0: * Indicates if the sorts have been built. Chris@0: * Chris@0: * @todo Group with other static properties. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: public $build_sort; Chris@0: Chris@0: /** Chris@0: * Stores the many-to-one tables for performance. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: public $many_to_one_tables; Chris@0: Chris@0: /** Chris@0: * A unique identifier which allows to update multiple views output via js. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: public $dom_id; Chris@0: Chris@0: /** Chris@0: * A render array container to store render related information. Chris@0: * Chris@0: * For example you can alter the array and attach some asset library or JS Chris@0: * settings via the #attached key. This is the required way to add custom Chris@0: * CSS or JS. Chris@0: * Chris@0: * @var array Chris@0: * Chris@0: * @see \Drupal\Core\Render\AttachmentsResponseProcessorInterface::processAttachments() Chris@0: */ Chris@0: public $element = [ Chris@0: '#attached' => [ Chris@0: 'library' => ['views/views.module'], Chris@0: 'drupalSettings' => [], Chris@0: ], Chris@0: '#cache' => [], Chris@0: ]; Chris@0: Chris@0: /** Chris@0: * The current user. Chris@0: * Chris@0: * @var \Drupal\Core\Session\AccountInterface Chris@0: */ Chris@0: protected $user; Chris@0: Chris@0: /** Chris@0: * Should the admin links be shown on the rendered view. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $showAdminLinks; Chris@0: Chris@0: /** Chris@0: * The views data. Chris@0: * Chris@0: * @var \Drupal\views\ViewsData Chris@0: */ Chris@0: protected $viewsData; Chris@0: Chris@0: /** Chris@0: * The route provider. Chris@0: * Chris@0: * @var \Drupal\Core\Routing\RouteProviderInterface Chris@0: */ Chris@0: protected $routeProvider; Chris@0: Chris@0: /** Chris@0: * The entity type of the base table, if available. Chris@0: * Chris@0: * @var \Drupal\Core\Entity\EntityTypeInterface|false Chris@0: */ Chris@0: protected $baseEntityType; Chris@0: Chris@0: /** Chris@0: * Holds all necessary data for proper unserialization. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $serializationData; Chris@0: Chris@0: /** Chris@0: * Constructs a new ViewExecutable object. Chris@0: * Chris@0: * @param \Drupal\views\ViewEntityInterface $storage Chris@0: * The view config entity the actual information is stored on. Chris@0: * @param \Drupal\Core\Session\AccountInterface $user Chris@0: * The current user. Chris@0: * @param \Drupal\views\ViewsData $views_data Chris@0: * The views data. Chris@0: * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider Chris@0: * The route provider. Chris@0: */ Chris@0: public function __construct(ViewEntityInterface $storage, AccountInterface $user, ViewsData $views_data, RouteProviderInterface $route_provider) { Chris@0: // Reference the storage and the executable to each other. Chris@0: $this->storage = $storage; Chris@0: $this->storage->set('executable', $this); Chris@0: $this->user = $user; Chris@0: $this->viewsData = $views_data; Chris@0: $this->routeProvider = $route_provider; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the identifier. Chris@0: * Chris@0: * @return string|null Chris@0: * The entity identifier, or NULL if the object does not yet have an Chris@0: * identifier. Chris@0: */ Chris@0: public function id() { Chris@0: return $this->storage->id(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Saves the view. Chris@0: */ Chris@0: public function save() { Chris@0: $this->storage->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the arguments for the view. Chris@0: * Chris@0: * @param array $args Chris@0: * The arguments passed to the view. Chris@0: */ Chris@0: public function setArguments(array $args) { Chris@0: // The array keys of the arguments will be incorrect if set by Chris@0: // views_embed_view() or \Drupal\views\ViewExecutable:preview(). Chris@0: $this->args = array_values($args); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Expands the list of used cache contexts for the view. Chris@0: * Chris@0: * @param string $cache_context Chris@0: * The additional cache context. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function addCacheContext($cache_context) { Chris@0: $this->element['#cache']['contexts'][] = $cache_context; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the current page for the pager. Chris@0: * Chris@0: * @param int $page Chris@0: * The current page. Chris@0: */ Chris@0: public function setCurrentPage($page) { Chris@0: $this->current_page = $page; Chris@0: Chris@0: // Calls like ::unserialize() might call this method without a proper $page. Chris@0: // Also check whether the element is pre rendered. At that point, the cache Chris@0: // keys cannot longer be manipulated. Chris@0: if ($page !== NULL && empty($this->element['#pre_rendered'])) { Chris@0: $this->element['#cache']['keys'][] = 'page:' . $page; Chris@0: } Chris@0: Chris@0: // If the pager is already initialized, pass it through to the pager. Chris@0: if (!empty($this->pager)) { Chris@0: return $this->pager->setCurrentPage($page); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current page from the pager. Chris@0: * Chris@0: * @return int Chris@0: * The current page. Chris@0: */ Chris@0: public function getCurrentPage() { Chris@0: // If the pager is already initialized, pass it through to the pager. Chris@0: if (!empty($this->pager)) { Chris@0: return $this->pager->getCurrentPage(); Chris@0: } Chris@0: Chris@0: if (isset($this->current_page)) { Chris@0: return $this->current_page; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the items per page from the pager. Chris@0: * Chris@0: * @return int Chris@0: * The items per page. Chris@0: */ Chris@0: public function getItemsPerPage() { Chris@0: // If the pager is already initialized, pass it through to the pager. Chris@0: if (!empty($this->pager)) { Chris@0: return $this->pager->getItemsPerPage(); Chris@0: } Chris@0: Chris@0: if (isset($this->items_per_page)) { Chris@0: return $this->items_per_page; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the items per page on the pager. Chris@0: * Chris@0: * @param int $items_per_page Chris@0: * The items per page. Chris@0: */ Chris@0: public function setItemsPerPage($items_per_page) { Chris@0: // Check whether the element is pre rendered. At that point, the cache keys Chris@0: // cannot longer be manipulated. Chris@0: if (empty($this->element['#pre_rendered'])) { Chris@0: $this->element['#cache']['keys'][] = 'items_per_page:' . $items_per_page; Chris@0: } Chris@0: $this->items_per_page = $items_per_page; Chris@0: Chris@0: // If the pager is already initialized, pass it through to the pager. Chris@0: if (!empty($this->pager)) { Chris@0: $this->pager->setItemsPerPage($items_per_page); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the pager offset from the pager. Chris@0: * Chris@0: * @return int Chris@0: * The pager offset. Chris@0: */ Chris@0: public function getOffset() { Chris@0: // If the pager is already initialized, pass it through to the pager. Chris@0: if (!empty($this->pager)) { Chris@0: return $this->pager->getOffset(); Chris@0: } Chris@0: Chris@0: if (isset($this->offset)) { Chris@0: return $this->offset; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the offset on the pager. Chris@0: * Chris@0: * @param int $offset Chris@0: * The pager offset. Chris@0: */ Chris@0: public function setOffset($offset) { Chris@0: // Check whether the element is pre rendered. At that point, the cache keys Chris@0: // cannot longer be manipulated. Chris@0: if (empty($this->element['#pre_rendered'])) { Chris@0: $this->element['#cache']['keys'][] = 'offset:' . $offset; Chris@0: } Chris@0: Chris@0: $this->offset = $offset; Chris@0: Chris@0: // If the pager is already initialized, pass it through to the pager. Chris@0: if (!empty($this->pager)) { Chris@0: $this->pager->setOffset($offset); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if the view uses a pager. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the view uses a pager, FALSE otherwise. Chris@0: */ Chris@0: public function usePager() { Chris@0: if (!empty($this->pager)) { Chris@0: return $this->pager->usePager(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets whether or not AJAX should be used. Chris@0: * Chris@0: * If AJAX is used, paging, table sorting, and exposed filters will be fetched Chris@0: * via an AJAX call rather than a page refresh. Chris@0: * Chris@0: * @param bool $ajax_enabled Chris@0: * TRUE if AJAX should be used, FALSE otherwise. Chris@0: */ Chris@0: public function setAjaxEnabled($ajax_enabled) { Chris@0: $this->ajaxEnabled = (bool) $ajax_enabled; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines whether or not AJAX should be used. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if AJAX is enabled, FALSE otherwise. Chris@0: */ Chris@0: public function ajaxEnabled() { Chris@0: return $this->ajaxEnabled; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the exposed filters input to an array. Chris@0: * Chris@0: * @param string[] $filters Chris@0: * The values taken from the view's exposed filters and sorts. Chris@0: */ Chris@0: public function setExposedInput($filters) { Chris@0: $this->exposed_input = $filters; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Figures out what the exposed input for this view is. Chris@0: * Chris@0: * They will be taken from \Drupal::request()->query or from Chris@0: * something previously set on the view. Chris@0: * Chris@0: * @return string[] Chris@0: * An array containing the exposed input values keyed by the filter and sort Chris@0: * name. Chris@0: * Chris@0: * @see self::setExposedInput() Chris@0: */ Chris@0: public function getExposedInput() { Chris@0: // Fill our input either from \Drupal::request()->query or from something Chris@0: // previously set on the view. Chris@0: if (empty($this->exposed_input)) { Chris@0: // Ensure that we can call the method at any point in time. Chris@0: $this->initDisplay(); Chris@0: Chris@0: $this->exposed_input = \Drupal::request()->query->all(); Chris@0: // unset items that are definitely not our input: Chris@0: foreach (['page', 'q'] as $key) { Chris@0: if (isset($this->exposed_input[$key])) { Chris@0: unset($this->exposed_input[$key]); Chris@0: } Chris@0: } Chris@0: Chris@0: // If we have no input at all, check for remembered input via session. Chris@0: Chris@0: // If filters are not overridden, store the 'remember' settings on the Chris@0: // default display. If they are, store them on this display. This way, Chris@0: // multiple displays in the same view can share the same filters and Chris@0: // remember settings. Chris@0: $display_id = ($this->display_handler->isDefaulted('filters')) ? 'default' : $this->current_display; Chris@0: Chris@0: if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->storage->id()][$display_id])) { Chris@0: $this->exposed_input = $_SESSION['views'][$this->storage->id()][$display_id]; Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->exposed_input; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the display for this view and initializes the display handler. Chris@0: * Chris@0: * @return true Chris@0: * Always returns TRUE. Chris@0: */ Chris@0: public function initDisplay() { Chris@0: if (isset($this->current_display)) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: // Initialize the display cache array. Chris@0: $this->displayHandlers = new DisplayPluginCollection($this, Views::pluginManager('display')); Chris@0: Chris@0: $this->current_display = 'default'; Chris@0: $this->display_handler = $this->displayHandlers->get('default'); Chris@0: Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the first display that is accessible to the user. Chris@0: * Chris@0: * @param array|string $displays Chris@0: * Either a single display id or an array of display ids. Chris@0: * Chris@0: * @return string Chris@0: * The first accessible display id, at least default. Chris@0: */ Chris@0: public function chooseDisplay($displays) { Chris@0: if (!is_array($displays)) { Chris@0: return $displays; Chris@0: } Chris@0: Chris@0: $this->initDisplay(); Chris@0: Chris@0: foreach ($displays as $display_id) { Chris@0: if ($this->displayHandlers->get($display_id)->access($this->user)) { Chris@0: return $display_id; Chris@0: } Chris@0: } Chris@0: Chris@0: return 'default'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current display plugin. Chris@0: * Chris@0: * @return \Drupal\views\Plugin\views\display\DisplayPluginBase Chris@0: * The current display plugin. Chris@0: */ Chris@0: public function getDisplay() { Chris@0: if (!isset($this->display_handler)) { Chris@0: $this->initDisplay(); Chris@0: } Chris@0: Chris@0: return $this->display_handler; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the current display. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The ID of the display to mark as current. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the display was correctly set, FALSE otherwise. Chris@0: */ Chris@0: public function setDisplay($display_id = NULL) { Chris@0: // If we have not already initialized the display, do so. Chris@0: if (!isset($this->current_display)) { Chris@0: // This will set the default display and instantiate the default display Chris@0: // plugin. Chris@0: $this->initDisplay(); Chris@0: } Chris@0: Chris@0: // If no display ID is passed, we either have initialized the default or Chris@0: // already have a display set. Chris@0: if (!isset($display_id)) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: $display_id = $this->chooseDisplay($display_id); Chris@0: Chris@0: // Ensure the requested display exists. Chris@0: if (!$this->displayHandlers->has($display_id)) { Chris@0: trigger_error(new FormattableMarkup('setDisplay() called with invalid display ID "@display".', ['@display' => $display_id]), E_USER_WARNING); Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // Reset if the display has changed. It could be called multiple times for Chris@0: // the same display, especially in the UI. Chris@0: if ($this->current_display != $display_id) { Chris@0: // Set the current display. Chris@0: $this->current_display = $display_id; Chris@0: Chris@0: // Reset the style and row plugins. Chris@0: $this->style_plugin = NULL; Chris@0: $this->plugin_name = NULL; Chris@0: $this->rowPlugin = NULL; Chris@0: } Chris@0: Chris@0: if ($display = $this->displayHandlers->get($display_id)) { Chris@0: // Set a shortcut. Chris@0: $this->display_handler = $display; Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a new display and a display handler instance for it. Chris@0: * Chris@0: * @param string $plugin_id Chris@0: * (optional) The plugin type from the Views plugin annotation. Defaults to Chris@0: * 'page'. Chris@0: * @param string $title Chris@0: * (optional) The title of the display. Defaults to NULL. Chris@0: * @param string $id Chris@0: * (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults Chris@0: * to NULL. Chris@0: * Chris@0: * @return \Drupal\views\Plugin\views\display\DisplayPluginBase Chris@0: * A new display plugin instance if executable is set, the new display ID Chris@0: * otherwise. Chris@0: */ Chris@0: public function newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) { Chris@0: $this->initDisplay(); Chris@0: Chris@0: $id = $this->storage->addDisplay($plugin_id, $title, $id); Chris@0: $this->displayHandlers->addInstanceId($id); Chris@0: Chris@0: $display = $this->displayHandlers->get($id); Chris@0: $display->newDisplay(); Chris@0: return $display; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current style plugin. Chris@0: * Chris@0: * @return \Drupal\views\Plugin\views\style\StylePluginBase Chris@0: * The current style plugin. Chris@0: */ Chris@0: public function getStyle() { Chris@0: if (!isset($this->style_plugin)) { Chris@0: $this->initStyle(); Chris@0: } Chris@0: Chris@0: return $this->style_plugin; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds and initializes the style plugin. Chris@0: * Chris@0: * Note that arguments may have changed which style plugin we use, so Chris@0: * check the view object first, then ask the display handler. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the style plugin was or could be initialized, FALSE otherwise. Chris@0: */ Chris@0: public function initStyle() { Chris@0: if (isset($this->style_plugin)) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: $this->style_plugin = $this->display_handler->getPlugin('style'); Chris@0: Chris@0: if (empty($this->style_plugin)) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Acquires and attaches all of the handlers. Chris@0: */ Chris@0: public function initHandlers() { Chris@0: $this->initDisplay(); Chris@0: if (empty($this->inited)) { Chris@0: foreach ($this::getHandlerTypes() as $key => $info) { Chris@0: $this->_initHandler($key, $info); Chris@0: } Chris@0: $this->inited = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current pager plugin. Chris@0: * Chris@0: * @return \Drupal\views\Plugin\views\pager\PagerPluginBase Chris@0: * The current pager plugin. Chris@0: */ Chris@0: public function getPager() { Chris@0: if (!isset($this->pager)) { Chris@0: $this->initPager(); Chris@0: } Chris@0: Chris@0: return $this->pager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the pager. Chris@0: * Chris@0: * Like style initialization, pager initialization is held until late to allow Chris@0: * for overrides. Chris@0: */ Chris@0: public function initPager() { Chris@0: if (!isset($this->pager)) { Chris@0: $this->pager = $this->display_handler->getPlugin('pager'); Chris@0: Chris@18: if ($this->usePager()) { Chris@0: $this->pager->setCurrentPage($this->current_page); Chris@0: } Chris@0: Chris@0: // These overrides may have been set earlier via $view->set_* Chris@0: // functions. Chris@0: if (isset($this->items_per_page)) { Chris@0: $this->pager->setItemsPerPage($this->items_per_page); Chris@0: } Chris@0: Chris@0: if (isset($this->offset)) { Chris@0: $this->pager->setOffset($this->offset); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Renders the pager, if necessary. Chris@0: * Chris@0: * @param string[] $exposed_input Chris@0: * The input values from the exposed forms and sorts of the view. Chris@0: * Chris@0: * @return array|string Chris@0: * The render array of the pager if it's set, blank string otherwise. Chris@0: */ Chris@0: public function renderPager($exposed_input) { Chris@18: if ($this->usePager()) { Chris@0: return $this->pager->render($exposed_input); Chris@0: } Chris@0: Chris@0: return ''; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a list of base tables to be used by the view. Chris@0: * Chris@0: * This is used primarily for the UI. The display must be already initialized. Chris@0: * Chris@0: * @return array Chris@0: * An array of base tables to be used by the view. Chris@0: */ Chris@0: public function getBaseTables() { Chris@0: $base_tables = [ Chris@0: $this->storage->get('base_table') => TRUE, Chris@0: '#global' => TRUE, Chris@0: ]; Chris@0: Chris@0: foreach ($this->display_handler->getHandlers('relationship') as $handler) { Chris@0: $base_tables[$handler->definition['base']] = TRUE; Chris@0: } Chris@0: return $base_tables; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the entity type of the base table, if available. Chris@0: * Chris@0: * @return \Drupal\Core\Entity\EntityType|false Chris@0: * The entity type of the base table, or FALSE if none exists. Chris@0: */ Chris@0: public function getBaseEntityType() { Chris@0: if (!isset($this->baseEntityType)) { Chris@0: $view_base_table = $this->storage->get('base_table'); Chris@0: $views_data = $this->viewsData->get($view_base_table); Chris@0: if (!empty($views_data['table']['entity type'])) { Chris@0: $entity_type_id = $views_data['table']['entity type']; Chris@0: $this->baseEntityType = \Drupal::entityTypeManager()->getDefinition($entity_type_id); Chris@0: } Chris@0: else { Chris@0: $this->baseEntityType = FALSE; Chris@0: } Chris@0: } Chris@0: return $this->baseEntityType; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs the preQuery() on all active handlers. Chris@0: */ Chris@0: protected function _preQuery() { Chris@0: foreach ($this::getHandlerTypes() as $key => $info) { Chris@0: $handlers = &$this->$key; Chris@0: $position = 0; Chris@0: foreach ($handlers as $id => $handler) { Chris@0: $handlers[$id]->position = $position; Chris@0: $handlers[$id]->preQuery(); Chris@0: $position++; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs the postExecute() on all active handlers. Chris@0: */ Chris@0: protected function _postExecute() { Chris@0: foreach ($this::getHandlerTypes() as $key => $info) { Chris@0: $handlers = &$this->$key; Chris@0: foreach ($handlers as $id => $handler) { Chris@0: $handlers[$id]->postExecute($this->result); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Attaches the views handler for the specific type. Chris@0: * Chris@0: * @param string $key Chris@0: * One of 'argument', 'field', 'sort', 'filter', 'relationship'. Chris@0: * @param array $info Chris@0: * An array of views handler types use in the view with additional Chris@0: * information about them. Chris@0: */ Chris@0: protected function _initHandler($key, $info) { Chris@0: // Load the requested items from the display onto the object. Chris@0: $this->$key = &$this->display_handler->getHandlers($key); Chris@0: Chris@0: // This reference deals with difficult PHP indirection. Chris@0: $handlers = &$this->$key; Chris@0: Chris@0: // Run through and test for accessibility. Chris@0: foreach ($handlers as $id => $handler) { Chris@0: if (!$handler->access($this->user)) { Chris@0: unset($handlers[$id]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds all the arguments. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the arguments were built successfully, FALSE otherwise. Chris@0: */ Chris@0: protected function _buildArguments() { Chris@0: // Initially, we want to build sorts and fields. This can change, though, Chris@0: // if we get a summary view. Chris@0: if (empty($this->argument)) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: // build arguments. Chris@0: $position = -1; Chris@0: $substitutions = []; Chris@0: $status = TRUE; Chris@0: Chris@0: // Get the title. Chris@0: $title = $this->display_handler->getOption('title'); Chris@0: Chris@0: // Iterate through each argument and process. Chris@0: foreach ($this->argument as $id => $arg) { Chris@0: $position++; Chris@0: $argument = $this->argument[$id]; Chris@0: Chris@0: if ($argument->broken()) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $argument->setRelationship(); Chris@0: Chris@0: $arg = isset($this->args[$position]) ? $this->args[$position] : NULL; Chris@0: $argument->position = $position; Chris@0: Chris@0: if (isset($arg) || $argument->hasDefaultArgument()) { Chris@0: if (!isset($arg)) { Chris@0: $arg = $argument->getDefaultArgument(); Chris@0: // make sure default args get put back. Chris@0: if (isset($arg)) { Chris@0: $this->args[$position] = $arg; Chris@0: } Chris@0: // remember that this argument was computed, not passed on the URL. Chris@0: $argument->is_default = TRUE; Chris@0: } Chris@0: Chris@16: // Set the argument, which ensures that the argument is valid and Chris@16: // possibly transforms the value. Chris@0: if (!$argument->setArgument($arg)) { Chris@0: $status = $argument->validateFail($arg); Chris@0: break; Chris@0: } Chris@0: Chris@0: if ($argument->isException()) { Chris@0: $arg_title = $argument->exceptionTitle(); Chris@0: } Chris@0: else { Chris@0: $arg_title = $argument->getTitle(); Chris@0: $argument->query($this->display_handler->useGroupBy()); Chris@0: } Chris@0: Chris@16: // Add this argument's substitution. Chris@0: $substitutions["{{ arguments.$id }}"] = $arg_title; Chris@16: // Since argument validator plugins can potentially transform the value, Chris@16: // use whatever value the argument handler now has, not the raw value. Chris@16: $substitutions["{{ raw_arguments.$id }}"] = strip_tags(Html::decodeEntities($argument->getValue())); Chris@0: Chris@0: // Test to see if we should use this argument's title Chris@0: if (!empty($argument->options['title_enable']) && !empty($argument->options['title'])) { Chris@0: $title = $argument->options['title']; Chris@0: } Chris@0: } Chris@0: else { Chris@0: // determine default condition and handle. Chris@0: $status = $argument->defaultAction(); Chris@0: break; Chris@0: } Chris@0: Chris@0: // Be safe with references and loops: Chris@0: unset($argument); Chris@0: } Chris@0: Chris@0: // set the title in the build info. Chris@0: if (!empty($title)) { Chris@0: $this->build_info['title'] = $title; Chris@0: } Chris@0: Chris@0: // Store the arguments for later use. Chris@0: $this->build_info['substitutions'] = $substitutions; Chris@0: Chris@0: return $status; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current query plugin. Chris@0: * Chris@0: * @return \Drupal\views\Plugin\views\query\QueryPluginBase Chris@0: * The current query plugin. Chris@0: */ Chris@0: public function getQuery() { Chris@0: if (!isset($this->query)) { Chris@0: $this->initQuery(); Chris@0: } Chris@0: Chris@0: return $this->query; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the query object for the view. Chris@0: * Chris@0: * @return true Chris@0: * Always returns TRUE. Chris@0: */ Chris@0: public function initQuery() { Chris@0: if (!empty($this->query)) { Chris@0: $class = get_class($this->query); Chris@0: if ($class && $class != 'stdClass') { Chris@0: // return if query is already initialized. Chris@0: return TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: // Create and initialize the query object. Chris@0: $views_data = Views::viewsData()->get($this->storage->get('base_table')); Chris@0: $this->storage->set('base_field', !empty($views_data['table']['base']['field']) ? $views_data['table']['base']['field'] : ''); Chris@0: if (!empty($views_data['table']['base']['database'])) { Chris@0: $this->base_database = $views_data['table']['base']['database']; Chris@0: } Chris@0: Chris@0: $this->query = $this->display_handler->getPlugin('query'); Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds the query for the view. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The display ID of the view. Chris@0: * Chris@0: * @return bool|null Chris@0: * TRUE if the view build process was successful, FALSE if setting the Chris@0: * display fails or NULL if the view has been built already. Chris@0: */ Chris@0: public function build($display_id = NULL) { Chris@0: if (!empty($this->built)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (empty($this->current_display) || $display_id) { Chris@0: if (!$this->setDisplay($display_id)) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: // Let modules modify the view just prior to building it. Chris@0: $module_handler = \Drupal::moduleHandler(); Chris@0: $module_handler->invokeAll('views_pre_build', [$this]); Chris@0: Chris@0: // Attempt to load from cache. Chris@0: // @todo Load a build_info from cache. Chris@0: Chris@0: $start = microtime(TRUE); Chris@0: // If that fails, let's build! Chris@0: $this->build_info = [ Chris@0: 'query' => '', Chris@0: 'count_query' => '', Chris@0: 'query_args' => [], Chris@0: ]; Chris@0: Chris@0: $this->initQuery(); Chris@0: Chris@0: // Call a module hook and see if it wants to present us with a Chris@0: // pre-built query or instruct us not to build the query for Chris@0: // some reason. Chris@0: // @todo: Implement this. Use the same mechanism Panels uses. Chris@0: Chris@0: // Run through our handlers and ensure they have necessary information. Chris@0: $this->initHandlers(); Chris@0: Chris@0: // Let the handlers interact with each other if they really want. Chris@0: $this->_preQuery(); Chris@0: Chris@0: if ($this->display_handler->usesExposed()) { Chris@0: /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */ Chris@0: $exposed_form = $this->display_handler->getPlugin('exposed_form'); Chris@0: $this->exposed_widgets = $exposed_form->renderExposedForm(); Chris@0: if (!empty($this->build_info['abort'])) { Chris@0: $this->built = TRUE; Chris@0: // Don't execute the query, $form_state, but rendering will still be executed to display the empty text. Chris@0: $this->executed = TRUE; Chris@0: return empty($this->build_info['fail']); Chris@0: } Chris@0: } Chris@0: Chris@0: // Build all the relationships first thing. Chris@0: $this->_build('relationship'); Chris@0: Chris@0: // Set the filtering groups. Chris@0: if (!empty($this->filter)) { Chris@0: $filter_groups = $this->display_handler->getOption('filter_groups'); Chris@0: if ($filter_groups) { Chris@0: $this->query->setGroupOperator($filter_groups['operator']); Chris@0: foreach ($filter_groups['groups'] as $id => $operator) { Chris@0: $this->query->setWhereGroup($operator, $id); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Build all the filters. Chris@0: $this->_build('filter'); Chris@0: Chris@0: $this->build_sort = TRUE; Chris@0: Chris@0: // Arguments can, in fact, cause this whole thing to abort. Chris@0: if (!$this->_buildArguments()) { Chris@0: $this->build_time = microtime(TRUE) - $start; Chris@0: $this->attachDisplays(); Chris@0: return $this->built; Chris@0: } Chris@0: Chris@0: // Initialize the style; arguments may have changed which style we use, Chris@0: // so waiting as long as possible is important. But we need to know Chris@0: // about the style when we go to build fields. Chris@0: if (!$this->initStyle()) { Chris@0: $this->build_info['fail'] = TRUE; Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: if ($this->style_plugin->usesFields()) { Chris@0: $this->_build('field'); Chris@0: } Chris@0: Chris@0: // Build our sort criteria if we were instructed to do so. Chris@0: if (!empty($this->build_sort)) { Chris@0: // Allow the style handler to deal with sorting. Chris@0: if ($this->style_plugin->buildSort()) { Chris@0: $this->_build('sort'); Chris@0: } Chris@0: // allow the plugin to build second sorts as well. Chris@0: $this->style_plugin->buildSortPost(); Chris@0: } Chris@0: Chris@0: // Allow area handlers to affect the query. Chris@0: $this->_build('header'); Chris@0: $this->_build('footer'); Chris@0: $this->_build('empty'); Chris@0: Chris@0: // Allow display handler to affect the query: Chris@0: $this->display_handler->query($this->display_handler->useGroupBy()); Chris@0: Chris@0: // Allow style handler to affect the query: Chris@0: $this->style_plugin->query($this->display_handler->useGroupBy()); Chris@0: Chris@0: // Allow exposed form to affect the query: Chris@0: if (isset($exposed_form)) { Chris@0: $exposed_form->query(); Chris@0: } Chris@0: Chris@0: if (\Drupal::config('views.settings')->get('sql_signature')) { Chris@0: $this->query->addSignature($this); Chris@0: } Chris@0: Chris@0: // Let modules modify the query just prior to finalizing it. Chris@0: $this->query->alter($this); Chris@0: Chris@0: // Only build the query if we weren't interrupted. Chris@0: if (empty($this->built)) { Chris@0: // Build the necessary info to execute the query. Chris@0: $this->query->build($this); Chris@0: } Chris@0: Chris@0: $this->built = TRUE; Chris@0: $this->build_time = microtime(TRUE) - $start; Chris@0: Chris@0: // Attach displays Chris@0: $this->attachDisplays(); Chris@0: Chris@0: // Let modules modify the view just after building it. Chris@0: $module_handler->invokeAll('views_post_build', [$this]); Chris@0: Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds an individual set of handlers. Chris@0: * Chris@0: * This is an internal method. Chris@0: * Chris@0: * @todo Some filter needs this function, even it is internal. Chris@0: * Chris@0: * @param string $key Chris@0: * The type of handlers (filter etc.) which should be iterated over to build Chris@0: * the relationship and query information. Chris@0: */ Chris@0: public function _build($key) { Chris@0: $handlers = &$this->$key; Chris@0: foreach ($handlers as $id => $data) { Chris@0: Chris@0: if (!empty($handlers[$id]) && is_object($handlers[$id])) { Chris@0: $multiple_exposed_input = [0 => NULL]; Chris@0: if ($handlers[$id]->multipleExposedInput()) { Chris@0: $multiple_exposed_input = $handlers[$id]->groupMultipleExposedInput($this->exposed_data); Chris@0: } Chris@0: foreach ($multiple_exposed_input as $group_id) { Chris@0: // Give this handler access to the exposed filter input. Chris@0: if (!empty($this->exposed_data)) { Chris@0: if ($handlers[$id]->isAGroup()) { Chris@0: $converted = $handlers[$id]->convertExposedInput($this->exposed_data, $group_id); Chris@0: $handlers[$id]->storeGroupInput($this->exposed_data, $converted); Chris@0: if (!$converted) { Chris@0: continue; Chris@0: } Chris@0: } Chris@0: $rc = $handlers[$id]->acceptExposedInput($this->exposed_data); Chris@0: $handlers[$id]->storeExposedInput($this->exposed_data, $rc); Chris@0: if (!$rc) { Chris@0: continue; Chris@0: } Chris@0: } Chris@0: $handlers[$id]->setRelationship(); Chris@0: $handlers[$id]->query($this->display_handler->useGroupBy()); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Executes the view's query. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The machine name of the display, which should be executed. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the view execution was successful, FALSE otherwise. For example, Chris@0: * an argument could stop the process. Chris@0: */ Chris@0: public function execute($display_id = NULL) { Chris@0: if (empty($this->built)) { Chris@0: if (!$this->build($display_id)) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: if (!empty($this->executed)) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: // Don't allow to use deactivated displays, but display them on the live preview. Chris@0: if (!$this->display_handler->isEnabled() && empty($this->live_preview)) { Chris@0: $this->build_info['fail'] = TRUE; Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // Let modules modify the view just prior to executing it. Chris@0: $module_handler = \Drupal::moduleHandler(); Chris@0: $module_handler->invokeAll('views_pre_execute', [$this]); Chris@0: Chris@0: // Check for already-cached results. Chris@0: /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */ Chris@0: if (!empty($this->live_preview)) { Chris@0: $cache = Views::pluginManager('cache')->createInstance('none'); Chris@0: } Chris@0: else { Chris@0: $cache = $this->display_handler->getPlugin('cache'); Chris@0: } Chris@0: Chris@0: if ($cache->cacheGet('results')) { Chris@18: if ($this->usePager()) { Chris@0: $this->pager->total_items = $this->total_rows; Chris@0: $this->pager->updatePageInfo(); Chris@0: } Chris@0: } Chris@0: else { Chris@0: $this->query->execute($this); Chris@0: // Enforce the array key rule as documented in Chris@0: // views_plugin_query::execute(). Chris@0: $this->result = array_values($this->result); Chris@0: $this->_postExecute(); Chris@0: $cache->cacheSet('results'); Chris@0: } Chris@0: Chris@0: // Let modules modify the view just after executing it. Chris@0: $module_handler->invokeAll('views_post_execute', [$this]); Chris@0: Chris@0: return $this->executed = TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Renders this view for a certain display. Chris@0: * Chris@0: * Note: You should better use just the preview function if you want to Chris@0: * render a view. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The machine name of the display, which should be rendered. Chris@0: * Chris@0: * @return array|null Chris@0: * A renderable array containing the view output or NULL if the build Chris@0: * process failed. Chris@0: */ Chris@0: public function render($display_id = NULL) { Chris@0: $this->execute($display_id); Chris@0: Chris@0: // Check to see if the build failed. Chris@0: if (!empty($this->build_info['fail'])) { Chris@0: return; Chris@0: } Chris@0: if (!empty($this->build_info['denied'])) { Chris@0: return; Chris@0: } Chris@0: Chris@0: /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */ Chris@0: $exposed_form = $this->display_handler->getPlugin('exposed_form'); Chris@0: $exposed_form->preRender($this->result); Chris@0: Chris@0: $module_handler = \Drupal::moduleHandler(); Chris@0: Chris@0: // @TODO In the longrun, it would be great to execute a view without Chris@0: // the theme system at all. See https://www.drupal.org/node/2322623. Chris@0: $active_theme = \Drupal::theme()->getActiveTheme(); Chris@18: $themes = array_keys($active_theme->getBaseThemeExtensions()); Chris@0: $themes[] = $active_theme->getName(); Chris@0: Chris@0: // Check for already-cached output. Chris@0: /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */ Chris@0: if (!empty($this->live_preview)) { Chris@0: $cache = Views::pluginManager('cache')->createInstance('none'); Chris@0: } Chris@0: else { Chris@0: $cache = $this->display_handler->getPlugin('cache'); Chris@0: } Chris@0: Chris@0: // Run preRender for the pager as it might change the result. Chris@0: if (!empty($this->pager)) { Chris@0: $this->pager->preRender($this->result); Chris@0: } Chris@0: Chris@0: // Initialize the style plugin. Chris@0: $this->initStyle(); Chris@0: Chris@0: if (!isset($this->response)) { Chris@0: // Set the response so other parts can alter it. Chris@0: $this->response = new Response('', 200); Chris@0: } Chris@0: Chris@0: // Give field handlers the opportunity to perform additional queries Chris@0: // using the entire resultset prior to rendering. Chris@0: if ($this->style_plugin->usesFields()) { Chris@0: foreach ($this->field as $id => $handler) { Chris@0: if (!empty($this->field[$id])) { Chris@0: $this->field[$id]->preRender($this->result); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $this->style_plugin->preRender($this->result); Chris@0: Chris@0: // Let each area handler have access to the result set. Chris@0: $areas = ['header', 'footer']; Chris@0: // Only call preRender() on the empty handlers if the result is empty. Chris@0: if (empty($this->result)) { Chris@0: $areas[] = 'empty'; Chris@0: } Chris@0: foreach ($areas as $area) { Chris@0: foreach ($this->{$area} as $handler) { Chris@0: $handler->preRender($this->result); Chris@0: } Chris@0: } Chris@0: Chris@0: // Let modules modify the view just prior to rendering it. Chris@0: $module_handler->invokeAll('views_pre_render', [$this]); Chris@0: Chris@17: // Let the themes play too, because prerender is a very themey thing. Chris@0: foreach ($themes as $theme_name) { Chris@0: $function = $theme_name . '_views_pre_render'; Chris@0: if (function_exists($function)) { Chris@0: $function($this); Chris@0: } Chris@0: } Chris@0: Chris@0: $this->display_handler->output = $this->display_handler->render(); Chris@0: Chris@0: $exposed_form->postRender($this->display_handler->output); Chris@0: Chris@0: $cache->postRender($this->display_handler->output); Chris@0: Chris@0: // Let modules modify the view output after it is rendered. Chris@0: $module_handler->invokeAll('views_post_render', [$this, &$this->display_handler->output, $cache]); Chris@0: Chris@0: // Let the themes play too, because post render is a very themey thing. Chris@0: foreach ($themes as $theme_name) { Chris@0: $function = $theme_name . '_views_post_render'; Chris@0: if (function_exists($function)) { Chris@0: $function($this, $this->display_handler->output, $cache); Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->display_handler->output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the cache tags associated with the executed view. Chris@0: * Chris@0: * Note: The cache plugin controls the used tags, so you can override it, if Chris@0: * needed. Chris@0: * Chris@0: * @return string[] Chris@0: * An array of cache tags. Chris@0: */ Chris@0: public function getCacheTags() { Chris@0: $this->initDisplay(); Chris@0: /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */ Chris@0: $cache = $this->display_handler->getPlugin('cache'); Chris@0: return $cache->getCacheTags(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds the render array outline for the given display. Chris@0: * Chris@0: * This render array has a #pre_render callback which will call Chris@0: * ::executeDisplay in order to actually execute the view and then build the Chris@0: * final render array structure. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The display ID. Chris@0: * @param array $args Chris@0: * An array of arguments passed along to the view. Chris@0: * @param bool $cache Chris@0: * (optional) Should the result be render cached. Chris@0: * Chris@0: * @return array|null Chris@0: * A renderable array with #type 'view' or NULL if the display ID was Chris@0: * invalid. Chris@0: */ Chris@0: public function buildRenderable($display_id = NULL, $args = [], $cache = TRUE) { Chris@0: // @todo Extract that into a generic method. Chris@0: if (empty($this->current_display) || $this->current_display != $this->chooseDisplay($display_id)) { Chris@0: if (!$this->setDisplay($display_id)) { Chris@0: return NULL; Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->display_handler->buildRenderable($args, $cache); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Executes the given display, with the given arguments. Chris@0: * Chris@0: * To be called externally by whatever mechanism invokes the view, Chris@0: * such as a page callback, hook_block, etc. Chris@0: * Chris@0: * This function should NOT be used by anything external as this Chris@0: * returns data in the format specified by the display. It can also Chris@0: * have other side effects that are only intended for the 'proper' Chris@0: * use of the display, such as setting page titles. Chris@0: * Chris@0: * If you simply want to view the display, use View::preview() instead. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The display ID of the view to be executed. Chris@0: * @param string[] $args Chris@0: * The arguments to be passed to the view. Chris@0: * Chris@0: * @return array|null Chris@0: * A renderable array containing the view output or NULL if the display ID Chris@0: * of the view to be executed doesn't exist. Chris@0: */ Chris@0: public function executeDisplay($display_id = NULL, $args = []) { Chris@0: if (empty($this->current_display) || $this->current_display != $this->chooseDisplay($display_id)) { Chris@0: if (!$this->setDisplay($display_id)) { Chris@0: return NULL; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->preExecute($args); Chris@0: Chris@0: // Execute the view Chris@0: $output = $this->display_handler->execute(); Chris@0: Chris@0: $this->postExecute(); Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Previews the given display, with the given arguments. Chris@0: * Chris@0: * To be called externally, probably by an AJAX handler of some flavor. Chris@0: * Can also be called when views are embedded, as this guarantees Chris@0: * normalized output. Chris@0: * Chris@0: * This function does not do any access checks on the view. It is the Chris@0: * responsibility of the caller to check $view->access() or implement other Chris@0: * access logic. To render the view normally with access checks, use Chris@0: * views_embed_view() instead. Chris@0: * Chris@0: * @return array|null Chris@0: * A renderable array containing the view output or NULL if the display ID Chris@0: * of the view to be executed doesn't exist. Chris@0: */ Chris@0: public function preview($display_id = NULL, $args = []) { Chris@0: if (empty($this->current_display) || ((!empty($display_id)) && $this->current_display != $display_id)) { Chris@0: if (!$this->setDisplay($display_id)) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->preview = TRUE; Chris@0: $this->preExecute($args); Chris@0: // Preview the view. Chris@0: $output = $this->display_handler->preview(); Chris@0: Chris@0: $this->postExecute(); Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs attachments and lets the display do what it needs to before running. Chris@0: * Chris@0: * @param array $args Chris@0: * An array of arguments from the URL that can be used by the view. Chris@0: */ Chris@0: public function preExecute($args = []) { Chris@0: $this->old_view[] = views_get_current_view(); Chris@0: views_set_current_view($this); Chris@0: $display_id = $this->current_display; Chris@0: Chris@0: // Prepare the view with the information we have, but only if we were Chris@0: // passed arguments, as they may have been set previously. Chris@0: if ($args) { Chris@0: $this->setArguments($args); Chris@0: } Chris@0: Chris@0: // Let modules modify the view just prior to executing it. Chris@0: \Drupal::moduleHandler()->invokeAll('views_pre_view', [$this, $display_id, &$this->args]); Chris@0: Chris@0: // Allow hook_views_pre_view() to set the dom_id, then ensure it is set. Chris@0: $this->dom_id = !empty($this->dom_id) ? $this->dom_id : hash('sha256', $this->storage->id() . REQUEST_TIME . mt_rand()); Chris@0: Chris@0: // Allow the display handler to set up for execution Chris@0: $this->display_handler->preExecute(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unsets the current view, mostly. Chris@0: */ Chris@0: public function postExecute() { Chris@0: // unset current view so we can be properly destructed later on. Chris@0: // Return the previous value in case we're an attachment. Chris@0: Chris@0: if ($this->old_view) { Chris@0: $old_view = array_pop($this->old_view); Chris@0: } Chris@0: Chris@0: views_set_current_view(isset($old_view) ? $old_view : FALSE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs attachment displays for the view. Chris@0: */ Chris@0: public function attachDisplays() { Chris@0: if (!empty($this->is_attachment)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!$this->display_handler->acceptAttachments()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $this->is_attachment = TRUE; Chris@0: // Find out which other displays attach to the current one. Chris@0: foreach ($this->display_handler->getAttachedDisplays() as $id) { Chris@0: $display_handler = $this->displayHandlers->get($id); Chris@0: // Only attach enabled attachments. Chris@0: if ($display_handler->isEnabled()) { Chris@0: $cloned_view = Views::executableFactory()->get($this->storage); Chris@0: $display_handler->attachTo($cloned_view, $this->current_display, $this->element); Chris@0: } Chris@0: } Chris@0: $this->is_attachment = FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if the given user has access to the view. Chris@0: * Chris@0: * Note that this sets the display handler if it hasn't been set. Chris@0: * Chris@0: * @param string $displays Chris@0: * The machine name of the display. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The user object. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the user has access to the view, FALSE otherwise. Chris@0: */ Chris@0: public function access($displays = NULL, $account = NULL) { Chris@0: // No one should have access to disabled views. Chris@0: if (!$this->storage->status()) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: if (!isset($this->current_display)) { Chris@0: $this->initDisplay(); Chris@0: } Chris@0: Chris@0: if (!$account) { Chris@0: $account = $this->user; Chris@0: } Chris@0: Chris@0: // We can't use choose_display() here because that function Chris@0: // calls this one. Chris@0: $displays = (array) $displays; Chris@0: foreach ($displays as $display_id) { Chris@0: if ($this->displayHandlers->has($display_id)) { Chris@0: if (($display = $this->displayHandlers->get($display_id)) && $display->access($account)) { Chris@0: return TRUE; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the used response object of the view. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Response $response Chris@0: * The response object which should be set. Chris@0: */ Chris@0: public function setResponse(Response $response) { Chris@0: $this->response = $response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the response object used by the view. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\Response Chris@0: * The response object of the view. Chris@0: */ Chris@0: public function getResponse() { Chris@0: if (!isset($this->response)) { Chris@0: $this->response = new Response(); Chris@0: } Chris@0: return $this->response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the request object. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The request object. Chris@0: */ Chris@0: public function setRequest(Request $request) { Chris@0: $this->request = $request; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the request object. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\Request Chris@0: * The request object. Chris@0: */ Chris@0: public function getRequest() { Chris@0: return $this->request; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the view's current title. Chris@0: * Chris@0: * This can change depending upon how it was built. Chris@0: * Chris@0: * @return string|false Chris@0: * The view title, FALSE if the display is not set. Chris@0: */ Chris@0: public function getTitle() { Chris@0: if (empty($this->display_handler)) { Chris@0: if (!$this->setDisplay('default')) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: // During building, we might find a title override. If so, use it. Chris@0: if (!empty($this->build_info['title'])) { Chris@0: $title = $this->build_info['title']; Chris@0: } Chris@0: else { Chris@0: $title = $this->display_handler->getOption('title'); Chris@0: } Chris@0: Chris@0: // Allow substitutions from the first row. Chris@0: if ($this->initStyle()) { Chris@0: $title = $this->style_plugin->tokenizeValue($title, 0); Chris@0: } Chris@0: return $title; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides the view's current title. Chris@0: * Chris@0: * The tokens in the title get's replaced before rendering. Chris@0: * Chris@0: * @return true Chris@0: * Always returns TRUE. Chris@0: */ Chris@0: public function setTitle($title) { Chris@0: $this->build_info['title'] = $title; Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Forces the view to build a title. Chris@0: */ Chris@0: public function buildTitle() { Chris@0: $this->initDisplay(); Chris@0: Chris@0: if (empty($this->built)) { Chris@0: $this->initQuery(); Chris@0: } Chris@0: Chris@0: $this->initHandlers(); Chris@0: Chris@0: $this->_buildArguments(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines whether you can link to the view or a particular display. Chris@0: * Chris@0: * Some displays (e.g. block displays) do not have their own route, but may Chris@0: * optionally provide a link to another display that does have a route. Chris@0: * Chris@0: * @param array $args Chris@0: * (optional) The arguments. Chris@0: * @param string $display_id Chris@0: * (optional) The display ID. The current display will be used by default. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the current display has a valid route available, FALSE otherwise. Chris@0: */ Chris@0: public function hasUrl($args = NULL, $display_id = NULL) { Chris@0: if (!empty($this->override_url)) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: // If the display has a valid route available (either its own or for a Chris@0: // linked display), then we can provide a URL for it. Chris@0: $display_handler = $this->displayHandlers->get($display_id ?: $this->current_display)->getRoutedDisplay(); Chris@0: if (!$display_handler instanceof DisplayRouterInterface) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // Look up the route name to make sure it exists. The name may exist, but Chris@0: // not be available yet in some instances when editing a view and doing Chris@0: // a live preview. Chris@0: $provider = \Drupal::service('router.route_provider'); Chris@0: try { Chris@0: $provider->getRouteByName($display_handler->getRouteName()); Chris@0: } Chris@0: catch (RouteNotFoundException $e) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the URL for the current view. Chris@0: * Chris@0: * This URL will be adjusted for arguments. Chris@0: * Chris@0: * @param array $args Chris@0: * (optional) Passed in arguments. Chris@0: * @param string $display_id Chris@0: * (optional) Specify the display ID to link to, fallback to the current ID. Chris@0: * Chris@0: * @return \Drupal\Core\Url Chris@0: * The URL of the current view. Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: * Thrown when the current view doesn't have a route available. Chris@0: */ Chris@0: public function getUrl($args = NULL, $display_id = NULL) { Chris@0: if (!empty($this->override_url)) { Chris@0: return $this->override_url; Chris@0: } Chris@0: Chris@0: $display_handler = $this->displayHandlers->get($display_id ?: $this->current_display)->getRoutedDisplay(); Chris@0: if (!$display_handler instanceof DisplayRouterInterface) { Chris@0: throw new \InvalidArgumentException('You cannot create a URL to a display without routes.'); Chris@0: } Chris@0: Chris@0: if (!isset($args)) { Chris@0: $args = $this->args; Chris@0: Chris@0: // Exclude arguments that were computed, not passed on the URL. Chris@0: $position = 0; Chris@0: if (!empty($this->argument)) { Chris@0: foreach ($this->argument as $argument) { Chris@0: if (!empty($argument->is_default) && !empty($argument->options['default_argument_skip_url'])) { Chris@0: unset($args[$position]); Chris@0: } Chris@0: $position++; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $path = $this->getPath(); Chris@0: Chris@0: // Don't bother working if there's nothing to do: Chris@0: if (empty($path) || (empty($args) && strpos($path, '%') === FALSE)) { Chris@0: return $display_handler->getUrlInfo(); Chris@0: } Chris@0: Chris@0: $argument_keys = isset($this->argument) ? array_keys($this->argument) : []; Chris@0: $id = current($argument_keys); Chris@0: Chris@0: /** @var \Drupal\Core\Url $url */ Chris@0: $url = $display_handler->getUrlInfo(); Chris@0: $route = $this->routeProvider->getRouteByName($url->getRouteName()); Chris@0: Chris@0: $variables = $route->compile()->getVariables(); Chris@0: $parameters = $url->getRouteParameters(); Chris@0: Chris@0: foreach ($variables as $variable_name) { Chris@0: if (empty($args)) { Chris@0: // Try to never put % in a URL; use the wildcard instead. Chris@0: if ($id && !empty($this->argument[$id]->options['exception']['value'])) { Chris@0: $parameters[$variable_name] = $this->argument[$id]->options['exception']['value']; Chris@0: } Chris@0: else { Chris@0: // Provide some fallback in case no exception value could be found. Chris@0: $parameters[$variable_name] = '*'; Chris@0: } Chris@0: } Chris@0: else { Chris@0: $parameters[$variable_name] = array_shift($args); Chris@0: } Chris@0: Chris@0: if ($id) { Chris@0: $id = next($argument_keys); Chris@0: } Chris@0: } Chris@0: Chris@0: $url->setRouteParameters($parameters); Chris@0: return $url; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the Url object associated with the display handler. Chris@0: * Chris@0: * @param string $display_id Chris@0: * (optional) The display ID (used only to detail an exception). Chris@0: * Chris@0: * @return \Drupal\Core\Url Chris@0: * The display handlers URL object. Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: * Thrown when the display plugin does not have a URL to return. Chris@0: */ Chris@0: public function getUrlInfo($display_id = '') { Chris@0: $this->initDisplay(); Chris@0: if (!$this->display_handler instanceof DisplayRouterInterface) { Chris@0: throw new \InvalidArgumentException("You cannot generate a URL for the display '$display_id'"); Chris@0: } Chris@0: return $this->display_handler->getUrlInfo(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the base path used for this view. Chris@0: * Chris@0: * @return string|false Chris@0: * The base path used for the view or FALSE if setting the display fails. Chris@0: */ Chris@0: public function getPath() { Chris@0: if (!empty($this->override_path)) { Chris@0: return $this->override_path; Chris@0: } Chris@0: Chris@0: if (empty($this->display_handler)) { Chris@0: if (!$this->setDisplay('default')) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: return $this->display_handler->getPath(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current user. Chris@0: * Chris@0: * Views plugins can receive the current user in order to not need dependency Chris@0: * injection. Chris@0: * Chris@0: * @return \Drupal\Core\Session\AccountInterface Chris@0: * The current user. Chris@0: */ Chris@0: public function getUser() { Chris@0: return $this->user; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a duplicate ViewExecutable object. Chris@0: * Chris@0: * Makes a copy of this view that has been sanitized of handlers, any runtime Chris@0: * data, ID, and UUID. Chris@0: */ Chris@0: public function createDuplicate() { Chris@0: return $this->storage->createDuplicate()->getExecutable(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unsets references so that a $view object may be properly garbage collected. Chris@0: */ Chris@0: public function destroy() { Chris@0: foreach ($this::getHandlerTypes() as $type => $info) { Chris@0: if (isset($this->$type)) { Chris@0: foreach ($this->{$type} as $handler) { Chris@0: $handler->destroy(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($this->style_plugin)) { Chris@0: $this->style_plugin->destroy(); Chris@0: } Chris@0: Chris@0: $reflection = new \ReflectionClass($this); Chris@0: $defaults = $reflection->getDefaultProperties(); Chris@0: // The external dependencies should not be reset. This is not generated by Chris@0: // the execution of a view. Chris@0: unset( Chris@0: $defaults['storage'], Chris@0: $defaults['user'], Chris@0: $defaults['request'], Chris@0: $defaults['routeProvider'], Chris@0: $defaults['viewsData'] Chris@0: ); Chris@0: Chris@0: foreach ($defaults as $property => $default) { Chris@0: $this->{$property} = $default; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Makes sure the view is completely valid. Chris@0: * Chris@0: * @return array Chris@0: * An array of error strings. This will be empty if there are no validation Chris@0: * errors. Chris@0: */ Chris@0: public function validate() { Chris@0: $errors = []; Chris@0: Chris@0: $this->initDisplay(); Chris@0: $current_display = $this->current_display; Chris@0: Chris@0: foreach ($this->displayHandlers as $id => $display) { Chris@0: if (!empty($display)) { Chris@0: if (!empty($display->display['deleted'])) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $result = $this->displayHandlers->get($id)->validate(); Chris@0: if (!empty($result) && is_array($result)) { Chris@0: $errors[$id] = $result; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $this->setDisplay($current_display); Chris@0: Chris@0: return $errors; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides a list of views handler types used in a view. Chris@0: * Chris@0: * This also provides some information about the views handler types. Chris@0: * Chris@0: * @return array Chris@0: * An array of associative arrays containing: Chris@0: * - title: The title of the handler type. Chris@0: * - ltitle: The lowercase title of the handler type. Chris@0: * - stitle: A singular title of the handler type. Chris@0: * - lstitle: A singular lowercase title of the handler type. Chris@0: * - plural: Plural version of the handler type. Chris@0: * - (optional) type: The actual internal used handler type. This key is Chris@0: * just used for header,footer,empty to link to the internal type: area. Chris@0: */ Chris@0: public static function getHandlerTypes() { Chris@0: return Views::getHandlerTypes(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the valid types of plugins that can be used. Chris@0: * Chris@0: * @return array Chris@0: * An array of plugin type strings. Chris@0: */ Chris@0: public static function getPluginTypes($type = NULL) { Chris@0: return Views::getPluginTypes($type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds an instance of a handler to the view. Chris@0: * Chris@0: * Items may be fields, filters, sort criteria, or arguments. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The machine name of the display. Chris@0: * @param string $type Chris@0: * The type of handler being added. Chris@0: * @param string $table Chris@0: * The name of the table this handler is from. Chris@0: * @param string $field Chris@0: * The name of the field this handler is from. Chris@0: * @param array $options Chris@0: * (optional) Extra options for this instance. Defaults to an empty array. Chris@0: * @param string $id Chris@0: * (optional) A unique ID for this handler instance. Defaults to NULL, in Chris@0: * which case one will be generated. Chris@0: * Chris@0: * @return string Chris@0: * The unique ID for this handler instance. Chris@0: */ Chris@0: public function addHandler($display_id, $type, $table, $field, $options = [], $id = NULL) { Chris@0: $types = $this::getHandlerTypes(); Chris@0: $this->setDisplay($display_id); Chris@0: Chris@0: $data = $this->viewsData->get($table); Chris@0: $fields = $this->displayHandlers->get($display_id)->getOption($types[$type]['plural']); Chris@0: Chris@0: if (empty($id)) { Chris@0: $id = $this->generateHandlerId($field, $fields); Chris@0: } Chris@0: Chris@0: // If the desired type is not found, use the original value directly. Chris@0: $handler_type = !empty($types[$type]['type']) ? $types[$type]['type'] : $type; Chris@0: Chris@0: $fields[$id] = [ Chris@0: 'id' => $id, Chris@0: 'table' => $table, Chris@0: 'field' => $field, Chris@0: ] + $options; Chris@0: Chris@0: if (isset($data['table']['entity type'])) { Chris@0: $fields[$id]['entity_type'] = $data['table']['entity type']; Chris@0: } Chris@0: if (isset($data[$field]['entity field'])) { Chris@0: $fields[$id]['entity_field'] = $data[$field]['entity field']; Chris@0: } Chris@0: Chris@0: // Load the plugin ID if available. Chris@0: if (isset($data[$field][$handler_type]['id'])) { Chris@0: $fields[$id]['plugin_id'] = $data[$field][$handler_type]['id']; Chris@0: } Chris@0: Chris@0: $this->displayHandlers->get($display_id)->setOption($types[$type]['plural'], $fields); Chris@0: Chris@0: return $id; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a unique ID for an handler instance. Chris@0: * Chris@0: * These handler instances are typically fields, filters, sort criteria, or Chris@0: * arguments. Chris@0: * Chris@0: * @param string $requested_id Chris@0: * The requested ID for the handler instance. Chris@0: * @param array $existing_items Chris@0: * An array of existing handler instances, keyed by their IDs. Chris@0: * Chris@0: * @return string Chris@0: * A unique ID. This will be equal to $requested_id if no handler instance Chris@0: * with that ID already exists. Otherwise, it will be appended with an Chris@0: * integer to make it unique, e.g., "{$requested_id}_1", Chris@0: * "{$requested_id}_2", etc. Chris@0: */ Chris@0: public static function generateHandlerId($requested_id, $existing_items) { Chris@0: $count = 0; Chris@0: $id = $requested_id; Chris@0: while (!empty($existing_items[$id])) { Chris@0: $id = $requested_id . '_' . ++$count; Chris@0: } Chris@0: return $id; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets an array of handler instances for the current display. Chris@0: * Chris@0: * @param string $type Chris@0: * The type of handlers to retrieve. Chris@0: * @param string $display_id Chris@0: * (optional) A specific display machine name to use. If NULL, the current Chris@0: * display will be used. Chris@0: * Chris@0: * @return array Chris@0: * An array of handler instances of a given type for this display. Chris@0: */ Chris@0: public function getHandlers($type, $display_id = NULL) { Chris@0: $old_display_id = !empty($this->current_display) ? $this->current_display : 'default'; Chris@0: Chris@0: $this->setDisplay($display_id); Chris@0: Chris@0: if (!isset($display_id)) { Chris@0: $display_id = $this->current_display; Chris@0: } Chris@0: Chris@0: // Get info about the types so we can get the right data. Chris@0: $types = static::getHandlerTypes(); Chris@0: Chris@0: $handlers = $this->displayHandlers->get($display_id)->getOption($types[$type]['plural']); Chris@0: Chris@0: // Restore initial display id (if any) or set to 'default'. Chris@0: if ($display_id != $old_display_id) { Chris@0: $this->setDisplay($old_display_id); Chris@0: } Chris@0: return $handlers; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the configuration of a handler instance on a given display. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The machine name of the display. Chris@0: * @param string $type Chris@0: * The type of handler to retrieve. Chris@0: * @param string $id Chris@0: * The ID of the handler to retrieve. Chris@0: * Chris@0: * @return array|null Chris@0: * Either the handler instance's configuration, or NULL if the handler is Chris@0: * not used on the display. Chris@0: */ Chris@0: public function getHandler($display_id, $type, $id) { Chris@0: // Get info about the types so we can get the right data. Chris@0: $types = static::getHandlerTypes(); Chris@0: // Initialize the display Chris@0: $this->setDisplay($display_id); Chris@0: Chris@0: // Get the existing configuration Chris@0: $fields = $this->displayHandlers->get($display_id)->getOption($types[$type]['plural']); Chris@0: Chris@0: return isset($fields[$id]) ? $fields[$id] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the configuration of a handler instance on a given display. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The machine name of the display. Chris@0: * @param string $type Chris@0: * The type of handler being set. Chris@0: * @param string $id Chris@0: * The ID of the handler being set. Chris@0: * @param array|null $item Chris@0: * An array of configuration for a handler, or NULL to remove this instance. Chris@0: * Chris@0: * @see set_item_option() Chris@0: */ Chris@0: public function setHandler($display_id, $type, $id, $item) { Chris@0: // Get info about the types so we can get the right data. Chris@0: $types = static::getHandlerTypes(); Chris@0: // Initialize the display. Chris@0: $this->setDisplay($display_id); Chris@0: Chris@0: // Get the existing configuration. Chris@0: $fields = $this->displayHandlers->get($display_id)->getOption($types[$type]['plural']); Chris@0: if (isset($item)) { Chris@0: $fields[$id] = $item; Chris@0: } Chris@0: Chris@0: // Store. Chris@0: $this->displayHandlers->get($display_id)->setOption($types[$type]['plural'], $fields); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes configuration for a handler instance on a given display. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The machine name of the display. Chris@0: * @param string $type Chris@0: * The type of handler being removed. Chris@0: * @param string $id Chris@0: * The ID of the handler being removed. Chris@0: */ Chris@0: public function removeHandler($display_id, $type, $id) { Chris@0: // Get info about the types so we can get the right data. Chris@0: $types = static::getHandlerTypes(); Chris@0: // Initialize the display. Chris@0: $this->setDisplay($display_id); Chris@0: Chris@0: // Get the existing configuration. Chris@0: $fields = $this->displayHandlers->get($display_id)->getOption($types[$type]['plural']); Chris@0: // Unset the item. Chris@0: unset($fields[$id]); Chris@0: Chris@0: // Store. Chris@0: $this->displayHandlers->get($display_id)->setOption($types[$type]['plural'], $fields); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets an option on a handler instance. Chris@0: * Chris@0: * Use this only if you have just 1 or 2 options to set; if you have many, Chris@0: * consider getting the handler instance, adding the options and using Chris@0: * set_item() directly. Chris@0: * Chris@0: * @param string $display_id Chris@0: * The machine name of the display. Chris@0: * @param string $type Chris@0: * The type of handler being set. Chris@0: * @param string $id Chris@0: * The ID of the handler being set. Chris@0: * @param string $option Chris@0: * The configuration key for the value being set. Chris@0: * @param mixed $value Chris@0: * The value being set. Chris@0: * Chris@0: * @see set_item() Chris@0: */ Chris@0: public function setHandlerOption($display_id, $type, $id, $option, $value) { Chris@0: $item = $this->getHandler($display_id, $type, $id); Chris@0: $item[$option] = $value; Chris@0: $this->setHandler($display_id, $type, $id, $item); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables admin links on the rendered view. Chris@0: * Chris@0: * @param bool $show_admin_links Chris@0: * TRUE if the admin links should be shown. Chris@0: */ Chris@0: public function setShowAdminLinks($show_admin_links) { Chris@0: $this->showAdminLinks = (bool) $show_admin_links; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns whether admin links should be rendered on the view. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if admin links should be rendered, else FALSE. Chris@0: */ Chris@0: public function getShowAdminLinks() { Chris@0: if (!isset($this->showAdminLinks)) { Chris@0: return $this->getDisplay()->getOption('show_admin_links'); Chris@0: } Chris@0: return $this->showAdminLinks; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges all plugin default values for each display. Chris@0: */ Chris@0: public function mergeDefaults() { Chris@0: $this->initDisplay(); Chris@0: // Initialize displays and merge all plugin defaults. Chris@0: foreach ($this->displayHandlers as $display) { Chris@0: $display->mergeDefaults(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides a full array of possible theme functions to try for a given hook. Chris@0: * Chris@0: * @param string $hook Chris@0: * The hook to use. This is the base theme/template name. Chris@0: * Chris@0: * @return array Chris@0: * An array of theme hook suggestions. Chris@0: */ Chris@0: public function buildThemeFunctions($hook) { Chris@0: $themes = []; Chris@0: $display = isset($this->display_handler) ? $this->display_handler->display : NULL; Chris@0: $id = $this->storage->id(); Chris@0: Chris@0: if ($display) { Chris@0: $themes[] = $hook . '__' . $id . '__' . $display['id']; Chris@0: $themes[] = $hook . '__' . $display['id']; Chris@0: // Add theme suggestions for each single tag. Chris@0: foreach (Tags::explode($this->storage->get('tag')) as $tag) { Chris@0: $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($tag)); Chris@0: } Chris@0: Chris@0: if ($display['id'] != $display['display_plugin']) { Chris@0: $themes[] = $hook . '__' . $id . '__' . $display['display_plugin']; Chris@0: $themes[] = $hook . '__' . $display['display_plugin']; Chris@0: } Chris@0: } Chris@0: $themes[] = $hook . '__' . $id; Chris@0: $themes[] = $hook; Chris@0: Chris@0: return $themes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if this view has form elements. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if this view contains handlers with views form implementations, Chris@0: * FALSE otherwise. Chris@0: */ Chris@0: public function hasFormElements() { Chris@0: foreach ($this->field as $field) { Chris@0: if (property_exists($field, 'views_form_callback') || method_exists($field, 'viewsForm')) { Chris@0: return TRUE; Chris@0: } Chris@0: } Chris@0: $area_handlers = array_merge(array_values($this->header), array_values($this->footer)); Chris@0: $empty = empty($this->result); Chris@0: foreach ($area_handlers as $area) { Chris@0: if (method_exists($area, 'viewsForm') && !$area->viewsFormEmpty($empty)) { Chris@0: return TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets dependencies for the view. Chris@0: * Chris@0: * @see \Drupal\views\Entity\View::calculateDependencies() Chris@0: * @see \Drupal\views\Entity\View::getDependencies() Chris@0: * Chris@0: * @return array Chris@0: * An array of dependencies grouped by type (module, theme, entity). Chris@0: */ Chris@0: public function getDependencies() { Chris@0: return $this->storage->calculateDependencies()->getDependencies(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Magic method implementation to serialize the view executable. Chris@0: * Chris@0: * @return array Chris@0: * The names of all variables that should be serialized. Chris@0: */ Chris@0: public function __sleep() { Chris@0: // Limit to only the required data which is needed to properly restore the Chris@0: // state during unserialization. Chris@0: $this->serializationData = [ Chris@0: 'storage' => $this->storage->id(), Chris@0: 'views_data' => $this->viewsData->_serviceId, Chris@0: 'route_provider' => $this->routeProvider->_serviceId, Chris@0: 'current_display' => $this->current_display, Chris@0: 'args' => $this->args, Chris@0: 'current_page' => $this->current_page, Chris@0: 'exposed_input' => $this->exposed_input, Chris@0: 'exposed_raw_input' => $this->exposed_raw_input, Chris@0: 'exposed_data' => $this->exposed_data, Chris@0: 'dom_id' => $this->dom_id, Chris@0: 'executed' => $this->executed, Chris@0: ]; Chris@0: return ['serializationData']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Magic method implementation to unserialize the view executable. Chris@0: */ Chris@0: public function __wakeup() { Chris@0: // There are cases, like in testing where we don't have a container Chris@0: // available. Chris@0: if (\Drupal::hasContainer() && !empty($this->serializationData)) { Chris@0: // Load and reference the storage. Chris@0: $this->storage = \Drupal::entityTypeManager()->getStorage('view') Chris@0: ->load($this->serializationData['storage']); Chris@0: $this->storage->set('executable', $this); Chris@0: Chris@0: // Attach all necessary services. Chris@0: $this->user = \Drupal::currentUser(); Chris@0: $this->viewsData = \Drupal::service($this->serializationData['views_data']); Chris@0: $this->routeProvider = \Drupal::service($this->serializationData['route_provider']); Chris@0: Chris@0: // Restore the state of this executable. Chris@0: if ($request = \Drupal::request()) { Chris@0: $this->setRequest($request); Chris@0: } Chris@0: $this->setDisplay($this->serializationData['current_display']); Chris@0: $this->setArguments($this->serializationData['args']); Chris@0: $this->setCurrentPage($this->serializationData['current_page']); Chris@0: $this->setExposedInput($this->serializationData['exposed_input']); Chris@0: $this->exposed_data = $this->serializationData['exposed_data']; Chris@0: $this->exposed_raw_input = $this->serializationData['exposed_raw_input']; Chris@0: $this->dom_id = $this->serializationData['dom_id']; Chris@0: Chris@0: $this->initHandlers(); Chris@0: Chris@0: // If the display was previously executed, execute it now. Chris@0: if ($this->serializationData['executed']) { Chris@0: $this->execute($this->current_display); Chris@0: } Chris@0: } Chris@0: // Unset serializationData since it serves no further purpose. Chris@0: unset($this->serializationData); Chris@0: } Chris@0: Chris@0: }