PageRenderTime 63ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

/core/modules/views/src/ViewExecutable.php

http://github.com/drupal/drupal
PHP | 2547 lines | 1059 code | 329 blank | 1159 comment | 185 complexity | bb0d08d657ea6dac38957df7177e77d6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. namespace Drupal\views;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\Component\Utility\Html;
  5. use Drupal\Component\Utility\Tags;
  6. use Drupal\Core\Routing\RouteProviderInterface;
  7. use Drupal\Core\Session\AccountInterface;
  8. use Drupal\views\Plugin\views\display\DisplayRouterInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  12. /**
  13. * Represents a view as a whole.
  14. *
  15. * An object to contain all of the data to generate a view, plus the member
  16. * functions to build the view query, execute the query and render the output.
  17. *
  18. * This class does not implement the Serializable interface since problems
  19. * occurred when using the serialize method.
  20. *
  21. * @see https://www.drupal.org/node/2849674
  22. * @see https://bugs.php.net/bug.php?id=66052
  23. */
  24. class ViewExecutable {
  25. /**
  26. * The config entity in which the view is stored.
  27. *
  28. * @var \Drupal\views\Entity\View
  29. */
  30. public $storage;
  31. /**
  32. * Whether or not the view has been built.
  33. *
  34. * @todo Group with other static properties.
  35. *
  36. * @var bool
  37. */
  38. public $built = FALSE;
  39. /**
  40. * Whether the view has been executed/query has been run.
  41. *
  42. * @todo Group with other static properties.
  43. *
  44. * @var bool
  45. */
  46. public $executed = FALSE;
  47. /**
  48. * Any arguments that have been passed into the view.
  49. *
  50. * @var array
  51. */
  52. public $args = [];
  53. /**
  54. * An array of build info.
  55. *
  56. * @var array
  57. */
  58. public $build_info = [];
  59. /**
  60. * Whether this view uses AJAX.
  61. *
  62. * @var bool
  63. */
  64. protected $ajaxEnabled = FALSE;
  65. /**
  66. * Where the results of a query will go.
  67. *
  68. * The array must use a numeric index starting at 0.
  69. *
  70. * @var \Drupal\views\ResultRow[]
  71. */
  72. public $result = [];
  73. // May be used to override the current pager info.
  74. /**
  75. * The current page. If the view uses pagination.
  76. *
  77. * @var int
  78. */
  79. protected $current_page = NULL;
  80. /**
  81. * The number of items per page.
  82. *
  83. * @var int
  84. */
  85. protected $items_per_page = NULL;
  86. /**
  87. * The pager offset.
  88. *
  89. * @var int
  90. */
  91. protected $offset = NULL;
  92. /**
  93. * The total number of rows returned from the query.
  94. *
  95. * @var int
  96. */
  97. public $total_rows = NULL;
  98. /**
  99. * Attachments to place before the view.
  100. *
  101. * @var array
  102. */
  103. public $attachment_before = [];
  104. /**
  105. * Attachments to place after the view.
  106. *
  107. * @var array
  108. */
  109. public $attachment_after = [];
  110. /**
  111. * Feed icons attached to the view.
  112. *
  113. * @var array
  114. */
  115. public $feedIcons = [];
  116. // Exposed widget input
  117. /**
  118. * All the form data from $form_state->getValues().
  119. *
  120. * @var array
  121. */
  122. public $exposed_data = [];
  123. /**
  124. * An array of input values from exposed forms.
  125. *
  126. * @var array
  127. */
  128. protected $exposed_input = [];
  129. /**
  130. * Exposed widget input directly from the $form_state->getValues().
  131. *
  132. * @var array
  133. */
  134. public $exposed_raw_input = [];
  135. /**
  136. * Used to store views that were previously running if we recurse.
  137. *
  138. * @var \Drupal\views\ViewExecutable[]
  139. */
  140. public $old_view = [];
  141. /**
  142. * To avoid recursion in views embedded into areas.
  143. *
  144. * @var \Drupal\views\ViewExecutable[]
  145. */
  146. public $parent_views = [];
  147. /**
  148. * Whether this view is an attachment to another view.
  149. *
  150. * @var bool
  151. */
  152. public $is_attachment = NULL;
  153. /**
  154. * Identifier of the current display.
  155. *
  156. * @var string
  157. */
  158. public $current_display;
  159. /**
  160. * Where the $query object will reside.
  161. *
  162. * @var \Drupal\views\Plugin\views\query\QueryPluginBase
  163. */
  164. public $query = NULL;
  165. /**
  166. * The used pager plugin used by the current executed view.
  167. *
  168. * @var \Drupal\views\Plugin\views\pager\PagerPluginBase
  169. */
  170. public $pager = NULL;
  171. /**
  172. * The current used display plugin.
  173. *
  174. * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
  175. */
  176. public $display_handler;
  177. /**
  178. * The list of used displays of the view.
  179. *
  180. * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase
  181. * objects.
  182. *
  183. * @var \Drupal\views\DisplayPluginCollection
  184. */
  185. public $displayHandlers;
  186. /**
  187. * The current used style plugin.
  188. *
  189. * @var \Drupal\views\Plugin\views\style\StylePluginBase
  190. */
  191. public $style_plugin;
  192. /**
  193. * The current used row plugin, if the style plugin supports row plugins.
  194. *
  195. * @var \Drupal\views\Plugin\views\row\RowPluginBase
  196. */
  197. public $rowPlugin;
  198. /**
  199. * Stores the current active row while rendering.
  200. *
  201. * @var int
  202. */
  203. public $row_index;
  204. /**
  205. * Allow to override the url of the current view.
  206. *
  207. * @var \Drupal\Core\Url
  208. */
  209. public $override_url;
  210. /**
  211. * Allow to override the path used for generated urls.
  212. *
  213. * @var string
  214. */
  215. public $override_path = NULL;
  216. /**
  217. * Allow to override the used database which is used for this query.
  218. *
  219. * @var bool
  220. */
  221. public $base_database = NULL;
  222. // Handlers which are active on this view.
  223. /**
  224. * Stores the field handlers which are initialized on this view.
  225. *
  226. * @var \Drupal\views\Plugin\views\field\FieldPluginBase[]
  227. */
  228. public $field;
  229. /**
  230. * Stores the argument handlers which are initialized on this view.
  231. *
  232. * @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase[]
  233. */
  234. public $argument;
  235. /**
  236. * Stores the sort handlers which are initialized on this view.
  237. *
  238. * @var \Drupal\views\Plugin\views\sort\SortPluginBase[]
  239. */
  240. public $sort;
  241. /**
  242. * Stores the filter handlers which are initialized on this view.
  243. *
  244. * @var \Drupal\views\Plugin\views\filter\FilterPluginBase[]
  245. */
  246. public $filter;
  247. /**
  248. * Stores the relationship handlers which are initialized on this view.
  249. *
  250. * @var \Drupal\views\Plugin\views\relationship\RelationshipPluginBase[]
  251. */
  252. public $relationship;
  253. /**
  254. * Stores the area handlers for the header which are initialized on this view.
  255. *
  256. * @var \Drupal\views\Plugin\views\area\AreaPluginBase[]
  257. */
  258. public $header;
  259. /**
  260. * Stores the area handlers for the footer which are initialized on this view.
  261. *
  262. * @var \Drupal\views\Plugin\views\area\AreaPluginBase[]
  263. */
  264. public $footer;
  265. /**
  266. * Stores the area handlers for the empty text which are initialized on this view.
  267. *
  268. * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects.
  269. *
  270. * @var \Drupal\views\Plugin\views\area\AreaPluginBase[]
  271. */
  272. public $empty;
  273. /**
  274. * Stores the current response object.
  275. *
  276. * @var \Symfony\Component\HttpFoundation\Response
  277. */
  278. protected $response = NULL;
  279. /**
  280. * Stores the current request object.
  281. *
  282. * @var \Symfony\Component\HttpFoundation\Request
  283. */
  284. protected $request;
  285. /**
  286. * Does this view already have loaded its handlers.
  287. *
  288. * @todo Group with other static properties.
  289. *
  290. * @var bool
  291. */
  292. public $inited;
  293. /**
  294. * The rendered output of the exposed form.
  295. *
  296. * @var string
  297. */
  298. public $exposed_widgets;
  299. /**
  300. * If this view has been previewed.
  301. *
  302. * @var bool
  303. */
  304. public $preview;
  305. /**
  306. * Force the query to calculate the total number of results.
  307. *
  308. * @todo Move to the query.
  309. *
  310. * @var bool
  311. */
  312. public $get_total_rows;
  313. /**
  314. * Indicates if the sorts have been built.
  315. *
  316. * @todo Group with other static properties.
  317. *
  318. * @var bool
  319. */
  320. public $build_sort;
  321. /**
  322. * Stores the many-to-one tables for performance.
  323. *
  324. * @var array
  325. */
  326. public $many_to_one_tables;
  327. /**
  328. * A unique identifier which allows to update multiple views output via js.
  329. *
  330. * @var string
  331. */
  332. public $dom_id;
  333. /**
  334. * A render array container to store render related information.
  335. *
  336. * For example you can alter the array and attach some asset library or JS
  337. * settings via the #attached key. This is the required way to add custom
  338. * CSS or JS.
  339. *
  340. * @var array
  341. *
  342. * @see \Drupal\Core\Render\AttachmentsResponseProcessorInterface::processAttachments()
  343. */
  344. public $element = [
  345. '#attached' => [
  346. 'library' => ['views/views.module'],
  347. 'drupalSettings' => [],
  348. ],
  349. '#cache' => [],
  350. ];
  351. /**
  352. * The current user.
  353. *
  354. * @var \Drupal\Core\Session\AccountInterface
  355. */
  356. protected $user;
  357. /**
  358. * Should the admin links be shown on the rendered view.
  359. *
  360. * @var bool
  361. */
  362. protected $showAdminLinks;
  363. /**
  364. * The views data.
  365. *
  366. * @var \Drupal\views\ViewsData
  367. */
  368. protected $viewsData;
  369. /**
  370. * The route provider.
  371. *
  372. * @var \Drupal\Core\Routing\RouteProviderInterface
  373. */
  374. protected $routeProvider;
  375. /**
  376. * The entity type of the base table, if available.
  377. *
  378. * @var \Drupal\Core\Entity\EntityTypeInterface|false
  379. */
  380. protected $baseEntityType;
  381. /**
  382. * Holds all necessary data for proper unserialization.
  383. *
  384. * @var array
  385. */
  386. protected $serializationData;
  387. /**
  388. * Constructs a new ViewExecutable object.
  389. *
  390. * @param \Drupal\views\ViewEntityInterface $storage
  391. * The view config entity the actual information is stored on.
  392. * @param \Drupal\Core\Session\AccountInterface $user
  393. * The current user.
  394. * @param \Drupal\views\ViewsData $views_data
  395. * The views data.
  396. * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
  397. * The route provider.
  398. */
  399. public function __construct(ViewEntityInterface $storage, AccountInterface $user, ViewsData $views_data, RouteProviderInterface $route_provider) {
  400. // Reference the storage and the executable to each other.
  401. $this->storage = $storage;
  402. $this->storage->set('executable', $this);
  403. $this->user = $user;
  404. $this->viewsData = $views_data;
  405. $this->routeProvider = $route_provider;
  406. }
  407. /**
  408. * Returns the identifier.
  409. *
  410. * @return string|null
  411. * The entity identifier, or NULL if the object does not yet have an
  412. * identifier.
  413. */
  414. public function id() {
  415. return $this->storage->id();
  416. }
  417. /**
  418. * Saves the view.
  419. */
  420. public function save() {
  421. $this->storage->save();
  422. }
  423. /**
  424. * Sets the arguments for the view.
  425. *
  426. * @param array $args
  427. * The arguments passed to the view.
  428. */
  429. public function setArguments(array $args) {
  430. // The array keys of the arguments will be incorrect if set by
  431. // views_embed_view() or \Drupal\views\ViewExecutable:preview().
  432. $this->args = array_values($args);
  433. }
  434. /**
  435. * Expands the list of used cache contexts for the view.
  436. *
  437. * @param string $cache_context
  438. * The additional cache context.
  439. *
  440. * @return $this
  441. */
  442. public function addCacheContext($cache_context) {
  443. $this->element['#cache']['contexts'][] = $cache_context;
  444. return $this;
  445. }
  446. /**
  447. * Sets the current page for the pager.
  448. *
  449. * @param int $page
  450. * The current page.
  451. */
  452. public function setCurrentPage($page) {
  453. $this->current_page = $page;
  454. // Calls like ::unserialize() might call this method without a proper $page.
  455. // Also check whether the element is pre rendered. At that point, the cache
  456. // keys cannot longer be manipulated.
  457. if ($page !== NULL && empty($this->element['#pre_rendered'])) {
  458. $this->element['#cache']['keys'][] = 'page:' . $page;
  459. }
  460. // If the pager is already initialized, pass it through to the pager.
  461. if (!empty($this->pager)) {
  462. return $this->pager->setCurrentPage($page);
  463. }
  464. }
  465. /**
  466. * Gets the current page from the pager.
  467. *
  468. * @return int
  469. * The current page.
  470. */
  471. public function getCurrentPage() {
  472. // If the pager is already initialized, pass it through to the pager.
  473. if (!empty($this->pager)) {
  474. return $this->pager->getCurrentPage();
  475. }
  476. if (isset($this->current_page)) {
  477. return $this->current_page;
  478. }
  479. }
  480. /**
  481. * Gets the items per page from the pager.
  482. *
  483. * @return int
  484. * The items per page.
  485. */
  486. public function getItemsPerPage() {
  487. // If the pager is already initialized, pass it through to the pager.
  488. if (!empty($this->pager)) {
  489. return $this->pager->getItemsPerPage();
  490. }
  491. if (isset($this->items_per_page)) {
  492. return $this->items_per_page;
  493. }
  494. }
  495. /**
  496. * Sets the items per page on the pager.
  497. *
  498. * @param int $items_per_page
  499. * The items per page.
  500. */
  501. public function setItemsPerPage($items_per_page) {
  502. // Check whether the element is pre rendered. At that point, the cache keys
  503. // cannot longer be manipulated.
  504. if (empty($this->element['#pre_rendered'])) {
  505. $this->element['#cache']['keys'][] = 'items_per_page:' . $items_per_page;
  506. }
  507. $this->items_per_page = $items_per_page;
  508. // If the pager is already initialized, pass it through to the pager.
  509. if (!empty($this->pager)) {
  510. $this->pager->setItemsPerPage($items_per_page);
  511. }
  512. }
  513. /**
  514. * Gets the pager offset from the pager.
  515. *
  516. * @return int
  517. * The pager offset.
  518. */
  519. public function getOffset() {
  520. // If the pager is already initialized, pass it through to the pager.
  521. if (!empty($this->pager)) {
  522. return $this->pager->getOffset();
  523. }
  524. if (isset($this->offset)) {
  525. return $this->offset;
  526. }
  527. }
  528. /**
  529. * Sets the offset on the pager.
  530. *
  531. * @param int $offset
  532. * The pager offset.
  533. */
  534. public function setOffset($offset) {
  535. // Check whether the element is pre rendered. At that point, the cache keys
  536. // cannot longer be manipulated.
  537. if (empty($this->element['#pre_rendered'])) {
  538. $this->element['#cache']['keys'][] = 'offset:' . $offset;
  539. }
  540. $this->offset = $offset;
  541. // If the pager is already initialized, pass it through to the pager.
  542. if (!empty($this->pager)) {
  543. $this->pager->setOffset($offset);
  544. }
  545. }
  546. /**
  547. * Determines if the view uses a pager.
  548. *
  549. * @return bool
  550. * TRUE if the view uses a pager, FALSE otherwise.
  551. */
  552. public function usePager() {
  553. if (!empty($this->pager)) {
  554. return $this->pager->usePager();
  555. }
  556. }
  557. /**
  558. * Sets whether or not AJAX should be used.
  559. *
  560. * If AJAX is used, paging, table sorting, and exposed filters will be fetched
  561. * via an AJAX call rather than a page refresh.
  562. *
  563. * @param bool $ajax_enabled
  564. * TRUE if AJAX should be used, FALSE otherwise.
  565. */
  566. public function setAjaxEnabled($ajax_enabled) {
  567. $this->ajaxEnabled = (bool) $ajax_enabled;
  568. }
  569. /**
  570. * Determines whether or not AJAX should be used.
  571. *
  572. * @return bool
  573. * TRUE if AJAX is enabled, FALSE otherwise.
  574. */
  575. public function ajaxEnabled() {
  576. return $this->ajaxEnabled;
  577. }
  578. /**
  579. * Sets the exposed filters input to an array.
  580. *
  581. * @param string[] $filters
  582. * The values taken from the view's exposed filters and sorts.
  583. */
  584. public function setExposedInput($filters) {
  585. $this->exposed_input = $filters;
  586. }
  587. /**
  588. * Figures out what the exposed input for this view is.
  589. *
  590. * They will be taken from \Drupal::request()->query or from
  591. * something previously set on the view.
  592. *
  593. * @return string[]
  594. * An array containing the exposed input values keyed by the filter and sort
  595. * name.
  596. *
  597. * @see self::setExposedInput()
  598. */
  599. public function getExposedInput() {
  600. // Fill our input either from \Drupal::request()->query or from something
  601. // previously set on the view.
  602. if (empty($this->exposed_input)) {
  603. // Ensure that we can call the method at any point in time.
  604. $this->initDisplay();
  605. $this->exposed_input = \Drupal::request()->query->all();
  606. // unset items that are definitely not our input:
  607. foreach (['page', 'q'] as $key) {
  608. if (isset($this->exposed_input[$key])) {
  609. unset($this->exposed_input[$key]);
  610. }
  611. }
  612. // If we have no input at all, check for remembered input via session.
  613. // If filters are not overridden, store the 'remember' settings on the
  614. // default display. If they are, store them on this display. This way,
  615. // multiple displays in the same view can share the same filters and
  616. // remember settings.
  617. $display_id = ($this->display_handler->isDefaulted('filters')) ? 'default' : $this->current_display;
  618. if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->storage->id()][$display_id])) {
  619. $this->exposed_input = $_SESSION['views'][$this->storage->id()][$display_id];
  620. }
  621. }
  622. return $this->exposed_input;
  623. }
  624. /**
  625. * Sets the display for this view and initializes the display handler.
  626. *
  627. * @return true
  628. * Always returns TRUE.
  629. */
  630. public function initDisplay() {
  631. if (isset($this->current_display)) {
  632. return TRUE;
  633. }
  634. // Initialize the display cache array.
  635. $this->displayHandlers = new DisplayPluginCollection($this, Views::pluginManager('display'));
  636. $this->current_display = 'default';
  637. $this->display_handler = $this->displayHandlers->get('default');
  638. return TRUE;
  639. }
  640. /**
  641. * Gets the first display that is accessible to the user.
  642. *
  643. * @param array|string $displays
  644. * Either a single display id or an array of display ids.
  645. *
  646. * @return string
  647. * The first accessible display id, at least default.
  648. */
  649. public function chooseDisplay($displays) {
  650. if (!is_array($displays)) {
  651. return $displays;
  652. }
  653. $this->initDisplay();
  654. foreach ($displays as $display_id) {
  655. if ($this->displayHandlers->get($display_id)->access($this->user)) {
  656. return $display_id;
  657. }
  658. }
  659. return 'default';
  660. }
  661. /**
  662. * Gets the current display plugin.
  663. *
  664. * @return \Drupal\views\Plugin\views\display\DisplayPluginBase
  665. * The current display plugin.
  666. */
  667. public function getDisplay() {
  668. if (!isset($this->display_handler)) {
  669. $this->initDisplay();
  670. }
  671. return $this->display_handler;
  672. }
  673. /**
  674. * Sets the current display.
  675. *
  676. * @param string $display_id
  677. * The ID of the display to mark as current.
  678. *
  679. * @return bool
  680. * TRUE if the display was correctly set, FALSE otherwise.
  681. */
  682. public function setDisplay($display_id = NULL) {
  683. // If we have not already initialized the display, do so.
  684. if (!isset($this->current_display)) {
  685. // This will set the default display and instantiate the default display
  686. // plugin.
  687. $this->initDisplay();
  688. }
  689. // If no display ID is passed, we either have initialized the default or
  690. // already have a display set.
  691. if (!isset($display_id)) {
  692. return TRUE;
  693. }
  694. $display_id = $this->chooseDisplay($display_id);
  695. // Ensure the requested display exists.
  696. if (!$this->displayHandlers->has($display_id)) {
  697. trigger_error(new FormattableMarkup('setDisplay() called with invalid display ID "@display".', ['@display' => $display_id]), E_USER_WARNING);
  698. return FALSE;
  699. }
  700. // Reset if the display has changed. It could be called multiple times for
  701. // the same display, especially in the UI.
  702. if ($this->current_display != $display_id) {
  703. // Set the current display.
  704. $this->current_display = $display_id;
  705. // Reset the style and row plugins.
  706. $this->style_plugin = NULL;
  707. $this->plugin_name = NULL;
  708. $this->rowPlugin = NULL;
  709. }
  710. if ($display = $this->displayHandlers->get($display_id)) {
  711. // Set a shortcut.
  712. $this->display_handler = $display;
  713. return TRUE;
  714. }
  715. return FALSE;
  716. }
  717. /**
  718. * Creates a new display and a display handler instance for it.
  719. *
  720. * @param string $plugin_id
  721. * (optional) The plugin type from the Views plugin annotation. Defaults to
  722. * 'page'.
  723. * @param string $title
  724. * (optional) The title of the display. Defaults to NULL.
  725. * @param string $id
  726. * (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults
  727. * to NULL.
  728. *
  729. * @return \Drupal\views\Plugin\views\display\DisplayPluginBase
  730. * A new display plugin instance if executable is set, the new display ID
  731. * otherwise.
  732. */
  733. public function newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
  734. $this->initDisplay();
  735. $id = $this->storage->addDisplay($plugin_id, $title, $id);
  736. $this->displayHandlers->addInstanceId($id);
  737. $display = $this->displayHandlers->get($id);
  738. $display->newDisplay();
  739. return $display;
  740. }
  741. /**
  742. * Gets the current style plugin.
  743. *
  744. * @return \Drupal\views\Plugin\views\style\StylePluginBase
  745. * The current style plugin.
  746. */
  747. public function getStyle() {
  748. if (!isset($this->style_plugin)) {
  749. $this->initStyle();
  750. }
  751. return $this->style_plugin;
  752. }
  753. /**
  754. * Finds and initializes the style plugin.
  755. *
  756. * Note that arguments may have changed which style plugin we use, so
  757. * check the view object first, then ask the display handler.
  758. *
  759. * @return bool
  760. * TRUE if the style plugin was or could be initialized, FALSE otherwise.
  761. */
  762. public function initStyle() {
  763. if (isset($this->style_plugin)) {
  764. return TRUE;
  765. }
  766. $this->style_plugin = $this->display_handler->getPlugin('style');
  767. if (empty($this->style_plugin)) {
  768. return FALSE;
  769. }
  770. return TRUE;
  771. }
  772. /**
  773. * Acquires and attaches all of the handlers.
  774. */
  775. public function initHandlers() {
  776. $this->initDisplay();
  777. if (empty($this->inited)) {
  778. foreach ($this::getHandlerTypes() as $key => $info) {
  779. $this->_initHandler($key, $info);
  780. }
  781. $this->inited = TRUE;
  782. }
  783. }
  784. /**
  785. * Gets the current pager plugin.
  786. *
  787. * @return \Drupal\views\Plugin\views\pager\PagerPluginBase
  788. * The current pager plugin.
  789. */
  790. public function getPager() {
  791. if (!isset($this->pager)) {
  792. $this->initPager();
  793. }
  794. return $this->pager;
  795. }
  796. /**
  797. * Initializes the pager.
  798. *
  799. * Like style initialization, pager initialization is held until late to allow
  800. * for overrides.
  801. */
  802. public function initPager() {
  803. if (!isset($this->pager)) {
  804. $this->pager = $this->display_handler->getPlugin('pager');
  805. if ($this->usePager()) {
  806. $this->pager->setCurrentPage($this->current_page);
  807. }
  808. // These overrides may have been set earlier via $view->set_*
  809. // functions.
  810. if (isset($this->items_per_page)) {
  811. $this->pager->setItemsPerPage($this->items_per_page);
  812. }
  813. if (isset($this->offset)) {
  814. $this->pager->setOffset($this->offset);
  815. }
  816. }
  817. }
  818. /**
  819. * Renders the pager, if necessary.
  820. *
  821. * @param string[] $exposed_input
  822. * The input values from the exposed forms and sorts of the view.
  823. *
  824. * @return array|string
  825. * The render array of the pager if it's set, blank string otherwise.
  826. */
  827. public function renderPager($exposed_input) {
  828. if ($this->usePager()) {
  829. return $this->pager->render($exposed_input);
  830. }
  831. return '';
  832. }
  833. /**
  834. * Creates a list of base tables to be used by the view.
  835. *
  836. * This is used primarily for the UI. The display must be already initialized.
  837. *
  838. * @return array
  839. * An array of base tables to be used by the view.
  840. */
  841. public function getBaseTables() {
  842. $base_tables = [
  843. $this->storage->get('base_table') => TRUE,
  844. '#global' => TRUE,
  845. ];
  846. foreach ($this->display_handler->getHandlers('relationship') as $handler) {
  847. $base_tables[$handler->definition['base']] = TRUE;
  848. }
  849. return $base_tables;
  850. }
  851. /**
  852. * Returns the entity type of the base table, if available.
  853. *
  854. * @return \Drupal\Core\Entity\EntityType|false
  855. * The entity type of the base table, or FALSE if none exists.
  856. */
  857. public function getBaseEntityType() {
  858. if (!isset($this->baseEntityType)) {
  859. $view_base_table = $this->storage->get('base_table');
  860. $views_data = $this->viewsData->get($view_base_table);
  861. if (!empty($views_data['table']['entity type'])) {
  862. $entity_type_id = $views_data['table']['entity type'];
  863. $this->baseEntityType = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
  864. }
  865. else {
  866. $this->baseEntityType = FALSE;
  867. }
  868. }
  869. return $this->baseEntityType;
  870. }
  871. /**
  872. * Runs the preQuery() on all active handlers.
  873. */
  874. protected function _preQuery() {
  875. foreach ($this::getHandlerTypes() as $key => $info) {
  876. $handlers = &$this->$key;
  877. $position = 0;
  878. foreach ($handlers as $id => $handler) {
  879. $handlers[$id]->position = $position;
  880. $handlers[$id]->preQuery();
  881. $position++;
  882. }
  883. }
  884. }
  885. /**
  886. * Runs the postExecute() on all active handlers.
  887. */
  888. protected function _postExecute() {
  889. foreach ($this::getHandlerTypes() as $key => $info) {
  890. $handlers = &$this->$key;
  891. foreach ($handlers as $id => $handler) {
  892. $handlers[$id]->postExecute($this->result);
  893. }
  894. }
  895. }
  896. /**
  897. * Attaches the views handler for the specific type.
  898. *
  899. * @param string $key
  900. * One of 'argument', 'field', 'sort', 'filter', 'relationship'.
  901. * @param array $info
  902. * An array of views handler types use in the view with additional
  903. * information about them.
  904. */
  905. protected function _initHandler($key, $info) {
  906. // Load the requested items from the display onto the object.
  907. $this->$key = &$this->display_handler->getHandlers($key);
  908. // This reference deals with difficult PHP indirection.
  909. $handlers = &$this->$key;
  910. // Run through and test for accessibility.
  911. foreach ($handlers as $id => $handler) {
  912. if (!$handler->access($this->user)) {
  913. unset($handlers[$id]);
  914. }
  915. }
  916. }
  917. /**
  918. * Builds all the arguments.
  919. *
  920. * @return bool
  921. * TRUE if the arguments were built successfully, FALSE otherwise.
  922. */
  923. protected function _buildArguments() {
  924. // Initially, we want to build sorts and fields. This can change, though,
  925. // if we get a summary view.
  926. if (empty($this->argument)) {
  927. return TRUE;
  928. }
  929. // build arguments.
  930. $position = -1;
  931. $substitutions = [];
  932. $status = TRUE;
  933. // Get the title.
  934. $title = $this->display_handler->getOption('title');
  935. // Iterate through each argument and process.
  936. foreach ($this->argument as $id => $arg) {
  937. $position++;
  938. $argument = $this->argument[$id];
  939. if ($argument->broken()) {
  940. continue;
  941. }
  942. $argument->setRelationship();
  943. $arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
  944. $argument->position = $position;
  945. if (isset($arg) || $argument->hasDefaultArgument()) {
  946. if (!isset($arg)) {
  947. $arg = $argument->getDefaultArgument();
  948. // make sure default args get put back.
  949. if (isset($arg)) {
  950. $this->args[$position] = $arg;
  951. }
  952. // remember that this argument was computed, not passed on the URL.
  953. $argument->is_default = TRUE;
  954. }
  955. // Set the argument, which ensures that the argument is valid and
  956. // possibly transforms the value.
  957. if (!$argument->setArgument($arg)) {
  958. $status = $argument->validateFail($arg);
  959. break;
  960. }
  961. if ($argument->isException()) {
  962. $arg_title = $argument->exceptionTitle();
  963. }
  964. else {
  965. $arg_title = $argument->getTitle();
  966. $argument->query($this->display_handler->useGroupBy());
  967. }
  968. // Add this argument's substitution.
  969. $substitutions["{{ arguments.$id }}"] = $arg_title;
  970. // Since argument validator plugins can potentially transform the value,
  971. // use whatever value the argument handler now has, not the raw value.
  972. $substitutions["{{ raw_arguments.$id }}"] = strip_tags(Html::decodeEntities($argument->getValue()));
  973. // Test to see if we should use this argument's title
  974. if (!empty($argument->options['title_enable']) && !empty($argument->options['title'])) {
  975. $title = $argument->options['title'];
  976. }
  977. }
  978. else {
  979. // determine default condition and handle.
  980. $status = $argument->defaultAction();
  981. break;
  982. }
  983. // Be safe with references and loops:
  984. unset($argument);
  985. }
  986. // set the title in the build info.
  987. if (!empty($title)) {
  988. $this->build_info['title'] = $title;
  989. }
  990. // Store the arguments for later use.
  991. $this->build_info['substitutions'] = $substitutions;
  992. return $status;
  993. }
  994. /**
  995. * Gets the current query plugin.
  996. *
  997. * @return \Drupal\views\Plugin\views\query\QueryPluginBase
  998. * The current query plugin.
  999. */
  1000. public function getQuery() {
  1001. if (!isset($this->query)) {
  1002. $this->initQuery();
  1003. }
  1004. return $this->query;
  1005. }
  1006. /**
  1007. * Initializes the query object for the view.
  1008. *
  1009. * @return true
  1010. * Always returns TRUE.
  1011. */
  1012. public function initQuery() {
  1013. if (!empty($this->query)) {
  1014. $class = get_class($this->query);
  1015. if ($class && $class != 'stdClass') {
  1016. // return if query is already initialized.
  1017. return TRUE;
  1018. }
  1019. }
  1020. // Create and initialize the query object.
  1021. $views_data = Views::viewsData()->get($this->storage->get('base_table'));
  1022. $this->storage->set('base_field', !empty($views_data['table']['base']['field']) ? $views_data['table']['base']['field'] : '');
  1023. if (!empty($views_data['table']['base']['database'])) {
  1024. $this->base_database = $views_data['table']['base']['database'];
  1025. }
  1026. $this->query = $this->display_handler->getPlugin('query');
  1027. return TRUE;
  1028. }
  1029. /**
  1030. * Builds the query for the view.
  1031. *
  1032. * @param string $display_id
  1033. * The display ID of the view.
  1034. *
  1035. * @return bool|null
  1036. * TRUE if the view build process was successful, FALSE if setting the
  1037. * display fails or NULL if the view has been built already.
  1038. */
  1039. public function build($display_id = NULL) {
  1040. if (!empty($this->built)) {
  1041. return;
  1042. }
  1043. if (empty($this->current_display) || $display_id) {
  1044. if (!$this->setDisplay($display_id)) {
  1045. return FALSE;
  1046. }
  1047. }
  1048. // Let modules modify the view just prior to building it.
  1049. $module_handler = \Drupal::moduleHandler();
  1050. $module_handler->invokeAll('views_pre_build', [$this]);
  1051. // Attempt to load from cache.
  1052. // @todo Load a build_info from cache.
  1053. $start = microtime(TRUE);
  1054. // If that fails, let's build!
  1055. $this->build_info = [
  1056. 'query' => '',
  1057. 'count_query' => '',
  1058. 'query_args' => [],
  1059. ];
  1060. $this->initQuery();
  1061. // Call a module hook and see if it wants to present us with a
  1062. // pre-built query or instruct us not to build the query for
  1063. // some reason.
  1064. // @todo: Implement this. Use the same mechanism Panels uses.
  1065. // Run through our handlers and ensure they have necessary information.
  1066. $this->initHandlers();
  1067. // Let the handlers interact with each other if they really want.
  1068. $this->_preQuery();
  1069. if ($this->display_handler->usesExposed()) {
  1070. /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
  1071. $exposed_form = $this->display_handler->getPlugin('exposed_form');
  1072. $this->exposed_widgets = $exposed_form->renderExposedForm();
  1073. if (!empty($this->build_info['abort'])) {
  1074. $this->built = TRUE;
  1075. // Don't execute the query, $form_state, but rendering will still be executed to display the empty text.
  1076. $this->executed = TRUE;
  1077. return empty($this->build_info['fail']);
  1078. }
  1079. }
  1080. // Build all the relationships first thing.
  1081. $this->_build('relationship');
  1082. // Set the filtering groups.
  1083. if (!empty($this->filter)) {
  1084. $filter_groups = $this->display_handler->getOption('filter_groups');
  1085. if ($filter_groups) {
  1086. $this->query->setGroupOperator($filter_groups['operator']);
  1087. foreach ($filter_groups['groups'] as $id => $operator) {
  1088. $this->query->setWhereGroup($operator, $id);
  1089. }
  1090. }
  1091. }
  1092. // Build all the filters.
  1093. $this->_build('filter');
  1094. $this->build_sort = TRUE;
  1095. // Arguments can, in fact, cause this whole thing to abort.
  1096. if (!$this->_buildArguments()) {
  1097. $this->build_time = microtime(TRUE) - $start;
  1098. $this->attachDisplays();
  1099. return $this->built;
  1100. }
  1101. // Initialize the style; arguments may have changed which style we use,
  1102. // so waiting as long as possible is important. But we need to know
  1103. // about the style when we go to build fields.
  1104. if (!$this->initStyle()) {
  1105. $this->build_info['fail'] = TRUE;
  1106. return FALSE;
  1107. }
  1108. if ($this->style_plugin->usesFields()) {
  1109. $this->_build('field');
  1110. }
  1111. // Build our sort criteria if we were instructed to do so.
  1112. if (!empty($this->build_sort)) {
  1113. // Allow the style handler to deal with sorting.
  1114. if ($this->style_plugin->buildSort()) {
  1115. $this->_build('sort');
  1116. }
  1117. // allow the plugin to build second sorts as well.
  1118. $this->style_plugin->buildSortPost();
  1119. }
  1120. // Allow area handlers to affect the query.
  1121. $this->_build('header');
  1122. $this->_build('footer');
  1123. $this->_build('empty');
  1124. // Allow display handler to affect the query:
  1125. $this->display_handler->query($this->display_handler->useGroupBy());
  1126. // Allow style handler to affect the query:
  1127. $this->style_plugin->query($this->display_handler->useGroupBy());
  1128. // Allow exposed form to affect the query:
  1129. if (isset($exposed_form)) {
  1130. $exposed_form->query();
  1131. }
  1132. if (\Drupal::config('views.settings')->get('sql_signature')) {
  1133. $this->query->addSignature($this);
  1134. }
  1135. // Let modules modify the query just prior to finalizing it.
  1136. $this->query->alter($this);
  1137. // Only build the query if we weren't interrupted.
  1138. if (empty($this->built)) {
  1139. // Build the necessary info to execute the query.
  1140. $this->query->build($this);
  1141. }
  1142. $this->built = TRUE;
  1143. $this->build_time = microtime(TRUE) - $start;
  1144. // Attach displays
  1145. $this->attachDisplays();
  1146. // Let modules modify the view just after building it.
  1147. $module_handler->invokeAll('views_post_build', [$this]);
  1148. return TRUE;
  1149. }
  1150. /**
  1151. * Builds an individual set of handlers.
  1152. *
  1153. * This is an internal method.
  1154. *
  1155. * @todo Some filter needs this function, even it is internal.
  1156. *
  1157. * @param string $key
  1158. * The type of handlers (filter etc.) which should be iterated over to build
  1159. * the relationship and query information.
  1160. */
  1161. public function _build($key) {
  1162. $handlers = &$this->$key;
  1163. foreach ($handlers as $id => $data) {
  1164. if (!empty($handlers[$id]) && is_object($handlers[$id])) {
  1165. $multiple_exposed_input = [0 => NULL];
  1166. if ($handlers[$id]->multipleExposedInput()) {
  1167. $multiple_exposed_input = $handlers[$id]->groupMultipleExposedInput($this->exposed_data);
  1168. }
  1169. foreach ($multiple_exposed_input as $group_id) {
  1170. // Give this handler access to the exposed filter input.
  1171. if (!empty($this->exposed_data)) {
  1172. if ($handlers[$id]->isAGroup()) {
  1173. $converted = $handlers[$id]->convertExposedInput($this->exposed_data, $group_id);
  1174. $handlers[$id]->storeGroupInput($this->exposed_data, $converted);
  1175. if (!$converted) {
  1176. continue;
  1177. }
  1178. }
  1179. $rc = $handlers[$id]->acceptExposedInput($this->exposed_data);
  1180. $handlers[$id]->storeExposedInput($this->exposed_data, $rc);
  1181. if (!$rc) {
  1182. continue;
  1183. }
  1184. }
  1185. $handlers[$id]->setRelationship();
  1186. $handlers[$id]->query($this->display_handler->useGroupBy());
  1187. }
  1188. }
  1189. }
  1190. }
  1191. /**
  1192. * Executes the view's query.
  1193. *
  1194. * @param string $display_id
  1195. * The machine name of the display, which should be executed.
  1196. *
  1197. * @return bool
  1198. * TRUE if the view execution was successful, FALSE otherwise. For example,
  1199. * an argument could stop the process.
  1200. */
  1201. public function execute($display_id = NULL) {
  1202. if (empty($this->built)) {
  1203. if (!$this->build($display_id)) {
  1204. return FALSE;
  1205. }
  1206. }
  1207. if (!empty($this->executed)) {
  1208. return TRUE;
  1209. }
  1210. // Don't allow to use deactivated displays, but display them on the live preview.
  1211. if (!$this->display_handler->isEnabled() && empty($this->live_preview)) {
  1212. $this->build_info['fail'] = TRUE;
  1213. return FALSE;
  1214. }
  1215. // Let modules modify the view just prior to executing it.
  1216. $module_handler = \Drupal::moduleHandler();
  1217. $module_handler->invokeAll('views_pre_execute', [$this]);
  1218. // Check for already-cached results.
  1219. /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
  1220. if (!empty($this->live_preview)) {
  1221. $cache = Views::pluginManager('cache')->createInstance('none');
  1222. }
  1223. else {
  1224. $cache = $this->display_handler->getPlugin('cache');
  1225. }
  1226. if ($cache->cacheGet('results')) {
  1227. if ($this->usePager()) {
  1228. $this->pager->total_items = $this->total_rows;
  1229. $this->pager->updatePageInfo();
  1230. }
  1231. }
  1232. else {
  1233. $this->query->execute($this);
  1234. // Enforce the array key rule as documented in
  1235. // views_plugin_query::execute().
  1236. $this->result = array_values($this->result);
  1237. $this->_postExecute();
  1238. $cache->cacheSet('results');
  1239. }
  1240. // Let modules modify the view just after executing it.
  1241. $module_handler->invokeAll('views_post_execute', [$this]);
  1242. return $this->executed = TRUE;
  1243. }
  1244. /**
  1245. * Renders this view for a certain display.
  1246. *
  1247. * Note: You should better use just the preview function if you want to
  1248. * render a view.
  1249. *
  1250. * @param string $display_id
  1251. * The machine name of the display, which should be rendered.
  1252. *
  1253. * @return array|null
  1254. * A renderable array containing the view output or NULL if the build
  1255. * process failed.
  1256. */
  1257. public function render($display_id = NULL) {
  1258. $this->execute($display_id);
  1259. // Check to see if the build failed.
  1260. if (!empty($this->build_info['fail'])) {
  1261. return;
  1262. }
  1263. if (!empty($this->build_info['denied'])) {
  1264. return;
  1265. }
  1266. /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
  1267. $exposed_form = $this->display_handler->getPlugin('exposed_form');
  1268. $exposed_form->preRender($this->result);
  1269. $module_handler = \Drupal::moduleHandler();
  1270. // @TODO In the longrun, it would be great to execute a view without
  1271. // the theme system at all. See https://www.drupal.org/node/2322623.
  1272. $active_theme = \Drupal::theme()->getActiveTheme();
  1273. $themes = array_keys($active_theme->getBaseThemeExtensions());
  1274. $themes[] = $active_theme->getName();
  1275. // Check for already-cached output.
  1276. /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
  1277. if (!empty($this->live_preview)) {
  1278. $cache = Views::pluginManager('cache')->createInstance('none');
  1279. }
  1280. else {
  1281. $cache = $this->display_handler->getPlugin('cache');
  1282. }
  1283. // Run preRender for the pager as it might change the result.
  1284. if (!empty($this->pager)) {
  1285. $this->pager->preRender($this->result);
  1286. }
  1287. // Initialize the style plugin.
  1288. $this->initStyle();
  1289. if (!isset($this->response)) {
  1290. // Set the response so other parts can alter it.
  1291. $this->response = new Response('', 200);
  1292. }
  1293. // Give field handlers the opportunity to perform additional queries
  1294. // using the entire resultset prior to rendering.
  1295. if ($this->style_plugin->usesFields()) {
  1296. foreach ($this->field as $id => $handler) {
  1297. if (!empty($this->field[$id])) {
  1298. $this->field[$id]->preRender($this->result);
  1299. }
  1300. }
  1301. }
  1302. $this->style_plugin->preRender($this->result);
  1303. // Let each area handler have access to the result set.
  1304. $areas = ['header', 'footer'];
  1305. // Only call preRender() on the empty handlers if the result is empty.
  1306. if (empty($this->result)) {
  1307. $areas[] = 'empty';
  1308. }
  1309. foreach ($areas as $area) {
  1310. foreach ($this->{$area} as $handler) {
  1311. $handler->preRender($this->result);
  1312. }
  1313. }
  1314. // Let modules modify the view just prior to rendering it.
  1315. $module_handler->invokeAll('views_pre_render', [$this]);
  1316. // Let the themes play too, because prerender is a very themey thing.
  1317. foreach ($themes as $theme_name) {
  1318. $function = $theme_name . '_views_pre_render';
  1319. if (function_exists($function)) {
  1320. $function($this);
  1321. }
  1322. }
  1323. $this->display_handler->output = $this->display_handler->render();
  1324. $exposed_form->postRender($this->display_handler->output);
  1325. $cache->postRender($this->display_handler->output);
  1326. // Let modules modify the view output after it is rendered.
  1327. $module_handler->invokeAll('views_post_render', [$this, &$this->display_handler->output, $cache]);
  1328. // Let the themes play too, because post render is a very themey thing.
  1329. foreach ($themes as $theme_name) {
  1330. $function = $theme_name . '_views_post_render';
  1331. if (function_exists($function)) {
  1332. $function($this, $this->display_handler->output, $cache);
  1333. }
  1334. }
  1335. return $this->display_handler->output;
  1336. }
  1337. /**
  1338. * Gets the cache tags associated with the executed view.
  1339. *
  1340. * Note: The cache plugin controls the used tags, so you can override it, if
  1341. * needed.
  1342. *
  1343. * @return string[]
  1344. * An array of cache tags.
  1345. */
  1346. public function getCacheTags() {
  1347. $this->initDisplay();
  1348. /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
  1349. $cache = $this->display_handler->getPlugin('cache');
  1350. return $cache->getCacheTags();
  1351. }
  1352. /**
  1353. * Builds the render array outline for the given display.
  1354. *
  1355. * This render array has a #pre_render callback which will call
  1356. * ::executeDisplay in order to actually execute the view and then build the
  1357. * final render array structure.
  1358. *
  1359. * @param string $display_id
  1360. * The display ID.
  1361. * @param array $args
  1362. * An array of arguments passed along to the view.
  1363. * @param bool $cache
  1364. * (optional) Should the result be render cached.
  1365. *
  1366. * @return array|null
  1367. * A renderable array with #type 'view' or NULL if the display ID was
  1368. * invalid.
  1369. */
  1370. public function buildRenderable($display_id = NULL, $args = [], $cache = TRUE) {
  1371. // @todo Extract that into a generic method.
  1372. if (empty($this->current_display) || $this->current_display != $this->chooseDisplay($display_id)) {
  1373. if (!$this->setDisplay($display_id)) {
  1374. return NULL;
  1375. }
  1376. }
  1377. return $this->display_handler->buildRenderable($args, $cache);
  1378. }
  1379. /**
  1380. * Executes the given display, with the given arguments.
  1381. *
  1382. * To be called externally by whatever mechanism invokes the view,
  1383. * such as a page callback, hook_block, etc.
  1384. *
  1385. * This function should NOT be used by anything external as this
  1386. * returns data in the format specified by the display. It can also
  1387. * have other side effects that are only intended for the 'proper'
  1388. * use of the display, such as setting page titles.
  1389. *
  1390. * If you simply want to view the display, use View::preview() instead.
  1391. *
  1392. * @param string $display_id
  1393. * The display ID of the view to be executed.
  1394. * @param string[] $args
  1395. * The arguments to be passed to the view.
  1396. *
  1397. * @return array|null
  1398. * A renderable array containing the view output or NULL if the display ID
  1399. * of the view to be executed doesn't exist.
  1400. */
  1401. public function executeDisplay($display_id = NULL, $args = []) {
  1402. if (empty($this->current_display) || $this->current_display != $this->chooseDisplay($display_id)) {
  1403. if (!$this->setDisplay($display_id)) {
  1404. return NULL;
  1405. }
  1406. }
  1407. $this->preExecute($args);
  1408. // Execute the view
  1409. $output = $this->display_handler->execute();
  1410. $this->postExecute();
  1411. return $output;
  1412. }
  1413. /**
  1414. * Previews the given display, with the given arguments.
  1415. *
  1416. * To be called externally, probably by an AJAX handler of some flavor.
  1417. * Can also be called when views are embedded, as this guarantees
  1418. * normalized output.
  1419. *
  1420. * This function does not do any access checks on the view. It is the
  1421. * responsibility of the caller to check $view->access() or implement other
  1422. * access logic. To render the view normally with access checks, use
  1423. * views_embed_view() instead.
  1424. *
  1425. * @return array|null
  1426. * A renderable array containing the view output or NULL if the display ID
  1427. * of the view to be executed doesn't exist.
  1428. */
  1429. public function preview($display_id = NULL, $args = []) {
  1430. if (empty($this->current_display) || ((!empty($display_id)) && $this->current_display != $display_id)) {
  1431. if (!$this->setDisplay($display_id)) {
  1432. return FALSE;
  1433. }
  1434. }
  1435. $this->preview = TRUE;
  1436. $this->preExecute($args);
  1437. // Preview the view.
  1438. $output = $this->display_handler->preview();
  1439. $this->postExecute();
  1440. return $output;
  1441. }
  1442. /**
  1443. * Runs attachments and lets the display do what it needs to before running.
  1444. *
  1445. * @param array $args
  1446. * An array of arguments from the URL that can be used by the view.
  1447. */
  1448. public function preExecute($args = []) {
  1449. $this->old_view[] = views_get_current_view();
  1450. views_set_current_view($this);
  1451. $display_id = $this->current_display;
  1452. // Prepare the view with the information we have, but only if we were
  1453. // passed arguments, as they may have been set previously.
  1454. if ($args) {
  1455. $this->setArguments($args);
  1456. }
  1457. // Let modules modify the view just prior to executing it.
  1458. \Drupal::moduleHandler()->invokeAll('views_pre_view', [$this, $display_id, &$this->args]);
  1459. // Allow hook_views_pre_view() to set the dom_id, then ensure it is set.
  1460. $this->dom_id = !empty($this->dom_id) ? $this->dom_id : hash('sha256', $this->storage->id() . REQUEST_TIME . mt_rand());
  1461. // Allow the display handler to set up for execution
  1462. $this->display_handler->preExecute();
  1463. }
  1464. /**
  1465. * Unsets the current view, mostly.
  1466. */
  1467. public function postExecute() {
  1468. // unset current view so we can be properly destructed later on.
  1469. // Return the previous value in case we're an attachment.
  1470. if ($this->old_view) {
  1471. $old_view = array_pop($this->old_view);
  1472. }
  1473. views_set_current_view(isset($old_view) ? $old_view : FALSE);
  1474. }
  1475. /**
  1476. * Runs attachment displays for the view.
  1477. */
  1478. public function attachDisplays() {
  1479. if (!empty($this->is_attachment)) {
  1480. return;
  1481. }
  1482. if (!$this->display_handler->acceptAttachments()) {
  1483. return;
  1484. }
  1485. $this->is_attachment = TRUE;
  1486. // Find out which other displays attach to the current one.
  1487. foreach ($this->display_handler->getAttachedDisplays() as $id) {
  1488. $display_handler = $this->displayHandlers->get($id);
  1489. // Only attach enabled attachments.
  1490. if ($display_handler->isEnabled()) {
  1491. $cloned_view = Views::executableFactory()->get($this->storage);
  1492. $display_handler->attachTo($cloned_view, $this->current_display, $this->element);
  1493. }
  1494. }
  1495. $this->is_attachment = FALSE;
  1496. }
  1497. /**
  1498. * Determines if the given user has access to the view.
  1499. *
  1500. * Note that this sets the display handler if it hasn't been set.
  1501. *
  1502. * @param string $displays
  1503. * The machine name of the display.
  1504. * @param \Drupal\Core\Session\AccountInterface $account
  1505. * The user object.
  1506. *
  1507. * @return bool
  1508. * TRUE if the user has access to the view, FALSE otherwise.
  1509. */
  1510. public function access($displays = NULL, $account = NULL) {
  1511. // No one should have access to disabled views.
  1512. if (!$this->storage->status()) {
  1513. return FALSE;
  1514. }
  1515. if (!isset($this->current_display)) {
  1516. $this->initDisplay();
  1517. }
  1518. if (!$account) {
  1519. $account = $this->user;
  1520. }
  1521. // We can't use choose_display() here because that function
  1522. // calls this one.
  1523. $displays = (array) $displays;
  1524. foreach ($displays as $display_id) {
  1525. if ($this->displayHandlers->has($display_id)) {
  1526. if (($display = $this->displayHandlers->get($display_id)) && $display->access($account)) {
  1527. return TRUE;
  1528. }
  1529. }
  1530. }
  1531. return FALSE;
  1532. }
  1533. /**
  1534. * Sets the used response object of the view.
  1535. *
  1536. * @param \Symfony\Component\HttpFoundation\Response $response
  1537. * The response object which should be set.
  1538. */
  1539. public function setResponse(Response $response) {
  1540. $this->response = $response;
  1541. }
  1542. /**
  1543. * Gets the response object used by the view.
  1544. *
  1545. * @return \Symfony\Component\HttpFoundation\Response
  1546. * The response object of the view.
  1547. */
  1548. public function getResponse() {
  1549. if (!isset($this->response)) {
  1550. $this->response = new Response();
  1551. }
  1552. return $this->response;
  1553. }
  1554. /**
  1555. * Sets the request object.
  1556. *
  1557. * @param \Symfony\Component\HttpFoundation\Request $request
  1558. * The request object.
  1559. */
  1560. public function setRequest(Request $request) {
  1561. $this->request = $request;
  1562. }
  1563. /**
  1564. * Gets the request object.
  1565. *
  1566. * @return \Symfony\Component\HttpFoundation\Request
  1567. * The request object.
  1568. */
  1569. public function getRequest() {
  1570. return $this->request;
  1571. }
  1572. /**
  1573. * Gets the view's current title.
  1574. *
  1575. * This can change depending upon how it was built.
  1576. *
  1577. * @return string|false
  1578. * The view title, FALSE if the display is not set.
  1579. */
  1580. public function getTitle() {
  1581. if (empty($this->display_handler)) {
  1582. if (!$this->setDisplay('default')) {
  1583. return FALSE;
  1584. }
  1585. }
  1586. // During building, we might find a title override. If so, use it.
  1587. if (!empty($this->build_info['title'])) {
  1588. $title = $this->build_info['title'];
  1589. }
  1590. else {
  1591. $title = $this->display_handler->getOption('title');
  1592. }
  1593. // Allow substitutions from the first row.
  1594. if ($this->initStyle()) {
  1595. $title = $this->style_plugin->tokenizeValue($title, 0);
  1596. }
  1597. return $title;
  1598. }
  1599. /**
  1600. * Overrides …

Large files files are truncated, but you can click here to view the full file