PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/views/lib/Drupal/views/Plugin/views/exposed_form/ExposedFormPluginBase.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 326 lines | 223 code | 42 blank | 61 comment | 37 complexity | a3fe53372a8e89daec6f728df312e64e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase.
  5. */
  6. namespace Drupal\views\Plugin\views\exposed_form;
  7. use Drupal\Component\Utility\String;
  8. use Drupal\views\Form\ViewsExposedForm;
  9. use Drupal\views\ViewExecutable;
  10. use Drupal\views\Plugin\views\display\DisplayPluginBase;
  11. use Drupal\views\Plugin\views\PluginBase;
  12. /**
  13. * @defgroup views_exposed_form_plugins Views exposed form plugins
  14. * @{
  15. * Plugins that handle the validation/submission and rendering of exposed forms.
  16. *
  17. * If needed, it is possible to use them to add additional form elements.
  18. */
  19. /**
  20. * The base plugin to handle exposed filter forms.
  21. */
  22. abstract class ExposedFormPluginBase extends PluginBase {
  23. /**
  24. * Overrides Drupal\views\Plugin\Plugin::$usesOptions.
  25. */
  26. protected $usesOptions = TRUE;
  27. protected function defineOptions() {
  28. $options = parent::defineOptions();
  29. $options['submit_button'] = array('default' => 'Apply', 'translatable' => TRUE);
  30. $options['reset_button'] = array('default' => FALSE, 'bool' => TRUE);
  31. $options['reset_button_label'] = array('default' => 'Reset', 'translatable' => TRUE);
  32. $options['exposed_sorts_label'] = array('default' => 'Sort by', 'translatable' => TRUE);
  33. $options['expose_sort_order'] = array('default' => TRUE, 'bool' => TRUE);
  34. $options['sort_asc_label'] = array('default' => 'Asc', 'translatable' => TRUE);
  35. $options['sort_desc_label'] = array('default' => 'Desc', 'translatable' => TRUE);
  36. return $options;
  37. }
  38. public function buildOptionsForm(&$form, &$form_state) {
  39. parent::buildOptionsForm($form, $form_state);
  40. $form['submit_button'] = array(
  41. '#type' => 'textfield',
  42. '#title' => t('Submit button text'),
  43. '#default_value' => $this->options['submit_button'],
  44. '#required' => TRUE,
  45. );
  46. $form['reset_button'] = array(
  47. '#type' => 'checkbox',
  48. '#title' => t('Include reset button (resets all applied exposed filters).'),
  49. '#default_value' => $this->options['reset_button'],
  50. );
  51. $form['reset_button_label'] = array(
  52. '#type' => 'textfield',
  53. '#title' => t('Reset button label'),
  54. '#description' => t('Text to display in the reset button of the exposed form.'),
  55. '#default_value' => $this->options['reset_button_label'],
  56. '#required' => TRUE,
  57. '#states' => array(
  58. 'invisible' => array(
  59. 'input[name="exposed_form_options[reset_button]"]' => array('checked' => FALSE),
  60. ),
  61. ),
  62. );
  63. $form['exposed_sorts_label'] = array(
  64. '#type' => 'textfield',
  65. '#title' => t('Exposed sorts label'),
  66. '#default_value' => $this->options['exposed_sorts_label'],
  67. '#required' => TRUE,
  68. );
  69. $form['expose_sort_order'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Allow people to choose the sort order'),
  72. '#description' => t('If sort order is not exposed, the sort criteria settings for each sort will determine its order.'),
  73. '#default_value' => $this->options['expose_sort_order'],
  74. );
  75. $form['sort_asc_label'] = array(
  76. '#type' => 'textfield',
  77. '#title' => t('Label for ascending sort'),
  78. '#default_value' => $this->options['sort_asc_label'],
  79. '#required' => TRUE,
  80. '#states' => array(
  81. 'visible' => array(
  82. 'input[name="exposed_form_options[expose_sort_order]"]' => array('checked' => TRUE),
  83. ),
  84. ),
  85. );
  86. $form['sort_desc_label'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Label for descending sort'),
  89. '#default_value' => $this->options['sort_desc_label'],
  90. '#required' => TRUE,
  91. '#states' => array(
  92. 'visible' => array(
  93. 'input[name="exposed_form_options[expose_sort_order]"]' => array('checked' => TRUE),
  94. ),
  95. ),
  96. );
  97. }
  98. /**
  99. * Render the exposed filter form.
  100. *
  101. * This actually does more than that; because it's using FAPI, the form will
  102. * also assign data to the appropriate handlers for use in building the
  103. * query.
  104. */
  105. public function renderExposedForm($block = FALSE) {
  106. // Deal with any exposed filters we may have, before building.
  107. $form_state = array(
  108. 'view' => &$this->view,
  109. 'display' => &$this->view->display_handler->display,
  110. 'method' => 'get',
  111. 'rerender' => TRUE,
  112. 'no_redirect' => TRUE,
  113. 'always_process' => TRUE,
  114. );
  115. // Some types of displays (eg. attachments) may wish to use the exposed
  116. // filters of their parent displays instead of showing an additional
  117. // exposed filter form for the attachment as well as that for the parent.
  118. if (!$this->view->display_handler->displaysExposed() || (!$block && $this->view->display_handler->getOption('exposed_block'))) {
  119. unset($form_state['rerender']);
  120. }
  121. if (!empty($this->ajax)) {
  122. $form_state['ajax'] = TRUE;
  123. }
  124. $form_state['exposed_form_plugin'] = $this;
  125. $form = \Drupal::formBuilder()->buildForm('\Drupal\views\Form\ViewsExposedForm', $form_state);
  126. if (!$this->view->display_handler->displaysExposed() || (!$block && $this->view->display_handler->getOption('exposed_block'))) {
  127. return array();
  128. }
  129. else {
  130. return $form;
  131. }
  132. }
  133. public function query() {
  134. $view = $this->view;
  135. $exposed_data = isset($view->exposed_data) ? $view->exposed_data : array();
  136. $sort_by = isset($exposed_data['sort_by']) ? $exposed_data['sort_by'] : NULL;
  137. if (!empty($sort_by)) {
  138. // Make sure the original order of sorts is preserved
  139. // (e.g. a sticky sort is often first)
  140. if (isset($view->sort[$sort_by])) {
  141. $view->query->orderby = array();
  142. foreach ($view->sort as $key => $sort) {
  143. if (!$sort->isExposed()) {
  144. $sort->query();
  145. }
  146. elseif ($key == $sort_by) {
  147. if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('ASC', 'DESC'))) {
  148. $sort->options['order'] = $exposed_data['sort_order'];
  149. }
  150. $sort->setRelationship();
  151. $sort->query();
  152. }
  153. }
  154. }
  155. }
  156. }
  157. public function preRender($values) { }
  158. public function postRender(&$output) { }
  159. public function preExecute() { }
  160. public function postExecute() { }
  161. /**
  162. * Alters the view exposed form.
  163. *
  164. * @param $form
  165. * The form build array. Passed by reference.
  166. * @param $form_state
  167. * The form state. Passed by reference.
  168. */
  169. public function exposedFormAlter(&$form, &$form_state) {
  170. if (!empty($this->options['submit_button'])) {
  171. $form['actions']['submit']['#value'] = $this->options['submit_button'];
  172. }
  173. // Check if there is exposed sorts for this view
  174. $exposed_sorts = array();
  175. foreach ($this->view->sort as $id => $handler) {
  176. if ($handler->canExpose() && $handler->isExposed()) {
  177. $exposed_sorts[$id] = String::checkPlain($handler->options['expose']['label']);
  178. }
  179. }
  180. if (count($exposed_sorts)) {
  181. $form['sort_by'] = array(
  182. '#type' => 'select',
  183. '#options' => $exposed_sorts,
  184. '#title' => $this->options['exposed_sorts_label'],
  185. );
  186. $sort_order = array(
  187. 'ASC' => $this->options['sort_asc_label'],
  188. 'DESC' => $this->options['sort_desc_label'],
  189. );
  190. if (isset($form_state['input']['sort_by']) && isset($this->view->sort[$form_state['input']['sort_by']])) {
  191. $default_sort_order = $this->view->sort[$form_state['input']['sort_by']]->options['order'];
  192. }
  193. else {
  194. $first_sort = reset($this->view->sort);
  195. $default_sort_order = $first_sort->options['order'];
  196. }
  197. if (!isset($form_state['input']['sort_by'])) {
  198. $keys = array_keys($exposed_sorts);
  199. $form_state['input']['sort_by'] = array_shift($keys);
  200. }
  201. if ($this->options['expose_sort_order']) {
  202. $form['sort_order'] = array(
  203. '#type' => 'select',
  204. '#options' => $sort_order,
  205. '#title' => t('Order', array(), array('context' => 'Sort order')),
  206. '#default_value' => $default_sort_order,
  207. );
  208. }
  209. $form['submit']['#weight'] = 10;
  210. }
  211. if (!empty($this->options['reset_button'])) {
  212. $form['actions']['reset'] = array(
  213. '#value' => $this->options['reset_button_label'],
  214. '#type' => 'submit',
  215. '#weight' => 10,
  216. );
  217. // Get an array of exposed filters, keyed by identifier option.
  218. foreach ($this->view->filter as $id => $handler) {
  219. if ($handler->canExpose() && $handler->isExposed() && !empty($handler->options['expose']['identifier'])) {
  220. $exposed_filters[$handler->options['expose']['identifier']] = $id;
  221. }
  222. }
  223. $all_exposed = array_merge($exposed_sorts, $exposed_filters);
  224. // Set the access to FALSE if there is no exposed input.
  225. if (!array_intersect_key($all_exposed, $this->view->exposed_input)) {
  226. $form['actions']['reset']['#access'] = FALSE;
  227. }
  228. }
  229. $pager = $this->view->display_handler->getPlugin('pager');
  230. if ($pager) {
  231. $pager->exposedFormAlter($form, $form_state);
  232. $form_state['pager_plugin'] = $pager;
  233. }
  234. }
  235. public function exposedFormValidate(&$form, &$form_state) {
  236. if (isset($form_state['pager_plugin'])) {
  237. $form_state['pager_plugin']->exposedFormValidate($form, $form_state);
  238. }
  239. }
  240. /**
  241. * This function is executed when exposed form is submited.
  242. *
  243. * @param $form
  244. * Nested array of form elements that comprise the form.
  245. * @param $form_state
  246. * A keyed array containing the current state of the form.
  247. * @param $exclude
  248. * Nested array of keys to exclude of insert into
  249. * $view->exposed_raw_input
  250. */
  251. public function exposedFormSubmit(&$form, &$form_state, &$exclude) {
  252. if (!empty($form_state['values']['op']) && $form_state['values']['op'] == $this->options['reset_button_label']) {
  253. $this->resetForm($form, $form_state);
  254. }
  255. if (isset($form_state['pager_plugin'])) {
  256. $form_state['pager_plugin']->exposedFormSubmit($form, $form_state, $exclude);
  257. $exclude[] = 'pager_plugin';
  258. }
  259. }
  260. public function resetForm(&$form, &$form_state) {
  261. // _SESSION is not defined for users who are not logged in.
  262. // If filters are not overridden, store the 'remember' settings on the
  263. // default display. If they are, store them on this display. This way,
  264. // multiple displays in the same view can share the same filters and
  265. // remember settings.
  266. $display_id = ($this->view->display_handler->isDefaulted('filters')) ? 'default' : $this->view->current_display;
  267. if (isset($_SESSION['views'][$this->view->storage->id()][$display_id])) {
  268. unset($_SESSION['views'][$this->view->storage->id()][$display_id]);
  269. }
  270. // Set the form to allow redirect.
  271. if (empty($this->view->live_preview)) {
  272. $form_state['no_redirect'] = FALSE;
  273. }
  274. else {
  275. $form_state['rebuild'] = TRUE;
  276. $this->view->exposed_data = array();
  277. }
  278. $form_state['redirect'] = current_path();
  279. $form_state['values'] = array();
  280. }
  281. }
  282. /**
  283. * @}
  284. */