PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views/src/Plugin/views/HandlerBase.php

http://github.com/drupal/drupal
PHP | 864 lines | 421 code | 120 blank | 323 comment | 59 complexity | 06cf2f0d1bfca3e4479a0c92d6839fe8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\views\Plugin\views;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Component\Utility\Unicode;
  5. use Drupal\Component\Utility\UrlHelper;
  6. use Drupal\Component\Utility\Xss;
  7. use Drupal\Core\Extension\ModuleHandlerInterface;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Render\Element;
  10. use Drupal\Core\Session\AccountInterface;
  11. use Drupal\views\Plugin\views\display\DisplayPluginBase;
  12. use Drupal\views\Render\ViewsRenderPipelineMarkup;
  13. use Drupal\views\ViewExecutable;
  14. use Drupal\views\Views;
  15. use Drupal\views\ViewsData;
  16. /**
  17. * Base class for Views handler plugins.
  18. *
  19. * @ingroup views_plugins
  20. */
  21. abstract class HandlerBase extends PluginBase implements ViewsHandlerInterface {
  22. /**
  23. * Where the $query object will reside:
  24. *
  25. * @var \Drupal\views\Plugin\views\query\QueryPluginBase
  26. */
  27. public $query = NULL;
  28. /**
  29. * The table this handler is attached to.
  30. *
  31. * @var string
  32. */
  33. public $table;
  34. /**
  35. * The alias of the table of this handler which is used in the query.
  36. *
  37. * @var string
  38. */
  39. public $tableAlias;
  40. /**
  41. * The actual field in the database table, maybe different
  42. * on other kind of query plugins/special handlers.
  43. *
  44. * @var string
  45. */
  46. public $realField;
  47. /**
  48. * With field you can override the realField if the real field is not set.
  49. *
  50. * @var string
  51. */
  52. public $field;
  53. /**
  54. * The relationship used for this field.
  55. *
  56. * @var string
  57. */
  58. public $relationship = NULL;
  59. /**
  60. * The module handler.
  61. *
  62. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  63. */
  64. protected $moduleHandler;
  65. /**
  66. * The views data service.
  67. *
  68. * @var \Drupal\views\ViewsData
  69. */
  70. protected $viewsData;
  71. /**
  72. * Constructs a Handler object.
  73. *
  74. * @param array $configuration
  75. * A configuration array containing information about the plugin instance.
  76. * @param string $plugin_id
  77. * The plugin_id for the plugin instance.
  78. * @param mixed $plugin_definition
  79. * The plugin implementation definition.
  80. */
  81. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  82. parent::__construct($configuration, $plugin_id, $plugin_definition);
  83. $this->is_handler = TRUE;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
  89. parent::init($view, $display, $options);
  90. // Check to see if this handler type is defaulted. Note that
  91. // we have to do a lookup because the type is singular but the
  92. // option is stored as the plural.
  93. $this->unpackOptions($this->options, $options);
  94. // This exist on most handlers, but not all. So they are still optional.
  95. if (isset($options['table'])) {
  96. $this->table = $options['table'];
  97. }
  98. // Allow aliases on both fields and tables.
  99. if (isset($this->definition['real table'])) {
  100. $this->table = $this->definition['real table'];
  101. }
  102. if (isset($this->definition['real field'])) {
  103. $this->realField = $this->definition['real field'];
  104. }
  105. if (isset($this->definition['field'])) {
  106. $this->realField = $this->definition['field'];
  107. }
  108. if (isset($options['field'])) {
  109. $this->field = $options['field'];
  110. if (!isset($this->realField)) {
  111. $this->realField = $options['field'];
  112. }
  113. }
  114. $this->query = &$view->query;
  115. }
  116. protected function defineOptions() {
  117. $options = parent::defineOptions();
  118. $options['id'] = ['default' => ''];
  119. $options['table'] = ['default' => ''];
  120. $options['field'] = ['default' => ''];
  121. $options['relationship'] = ['default' => 'none'];
  122. $options['group_type'] = ['default' => 'group'];
  123. $options['admin_label'] = ['default' => ''];
  124. return $options;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function adminLabel($short = FALSE) {
  130. if (!empty($this->options['admin_label'])) {
  131. return $this->options['admin_label'];
  132. }
  133. $title = ($short && isset($this->definition['title short'])) ? $this->definition['title short'] : $this->definition['title'];
  134. return $this->t('@group: @title', ['@group' => $this->definition['group'], '@title' => $title]);
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function getField($field = NULL) {
  140. if (!isset($field)) {
  141. if (!empty($this->formula)) {
  142. $field = $this->getFormula();
  143. }
  144. else {
  145. $field = $this->tableAlias . '.' . $this->realField;
  146. }
  147. }
  148. // If grouping, check to see if the aggregation method needs to modify the field.
  149. if ($this->view->display_handler->useGroupBy()) {
  150. $this->view->initQuery();
  151. if ($this->query) {
  152. $info = $this->query->getAggregationInfo();
  153. if (!empty($info[$this->options['group_type']]['method'])) {
  154. $method = $info[$this->options['group_type']]['method'];
  155. if (method_exists($this->query, $method)) {
  156. return $this->query->$method($this->options['group_type'], $field);
  157. }
  158. }
  159. }
  160. }
  161. return $field;
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function sanitizeValue($value, $type = NULL) {
  167. switch ($type) {
  168. case 'xss':
  169. $value = Xss::filter($value);
  170. break;
  171. case 'xss_admin':
  172. $value = Xss::filterAdmin($value);
  173. break;
  174. case 'url':
  175. $value = Html::escape(UrlHelper::stripDangerousProtocols($value));
  176. break;
  177. default:
  178. $value = Html::escape($value);
  179. break;
  180. }
  181. return ViewsRenderPipelineMarkup::create($value);
  182. }
  183. /**
  184. * Transform a string by a certain method.
  185. *
  186. * @param $string
  187. * The input you want to transform.
  188. * @param $option
  189. * How do you want to transform it, possible values:
  190. * - upper: Uppercase the string.
  191. * - lower: lowercase the string.
  192. * - ucfirst: Make the first char uppercase.
  193. * - ucwords: Make each word in the string uppercase.
  194. *
  195. * @return string
  196. * The transformed string.
  197. */
  198. protected function caseTransform($string, $option) {
  199. switch ($option) {
  200. default:
  201. return $string;
  202. case 'upper':
  203. return mb_strtoupper($string);
  204. case 'lower':
  205. return mb_strtolower($string);
  206. case 'ucfirst':
  207. return Unicode::ucfirst($string);
  208. case 'ucwords':
  209. return Unicode::ucwords($string);
  210. }
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  216. // Some form elements belong in a fieldset for presentation, but can't
  217. // be moved into one because of the $form_state->getValues() hierarchy. Those
  218. // elements can add a #fieldset => 'fieldset_name' property, and they'll
  219. // be moved to their fieldset during pre_render.
  220. $form['#pre_render'][] = [get_class($this), 'preRenderAddFieldsetMarkup'];
  221. parent::buildOptionsForm($form, $form_state);
  222. $form['fieldsets'] = [
  223. '#type' => 'value',
  224. '#value' => ['more', 'admin_label'],
  225. ];
  226. $form['admin_label'] = [
  227. '#type' => 'details',
  228. '#title' => $this->t('Administrative title'),
  229. '#weight' => 150,
  230. ];
  231. $form['admin_label']['admin_label'] = [
  232. '#type' => 'textfield',
  233. '#title' => $this->t('Administrative title'),
  234. '#description' => $this->t('This title will be displayed on the views edit page instead of the default one. This might be useful if you have the same item twice.'),
  235. '#default_value' => $this->options['admin_label'],
  236. '#parents' => ['options', 'admin_label'],
  237. ];
  238. // This form is long and messy enough that the "Administrative title" option
  239. // belongs in "Administrative title" fieldset at the bottom of the form.
  240. $form['more'] = [
  241. '#type' => 'details',
  242. '#title' => $this->t('More'),
  243. '#weight' => 200,
  244. '#optional' => TRUE,
  245. ];
  246. // Allow to alter the default values brought into the form.
  247. // @todo Do we really want to keep this hook.
  248. $this->getModuleHandler()->alter('views_handler_options', $this->options, $this->view);
  249. }
  250. /**
  251. * Gets the module handler.
  252. *
  253. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  254. */
  255. protected function getModuleHandler() {
  256. if (!$this->moduleHandler) {
  257. $this->moduleHandler = \Drupal::moduleHandler();
  258. }
  259. return $this->moduleHandler;
  260. }
  261. /**
  262. * Sets the module handler.
  263. *
  264. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  265. * The module handler.
  266. */
  267. public function setModuleHandler(ModuleHandlerInterface $module_handler) {
  268. $this->moduleHandler = $module_handler;
  269. }
  270. /**
  271. * Provides the handler some groupby.
  272. */
  273. public function usesGroupBy() {
  274. return TRUE;
  275. }
  276. /**
  277. * Provide a form for aggregation settings.
  278. */
  279. public function buildGroupByForm(&$form, FormStateInterface $form_state) {
  280. $display_id = $form_state->get('display_id');
  281. $type = $form_state->get('type');
  282. $id = $form_state->get('id');
  283. $form['#section'] = $display_id . '-' . $type . '-' . $id;
  284. $this->view->initQuery();
  285. $info = $this->view->query->getAggregationInfo();
  286. foreach ($info as $id => $aggregate) {
  287. $group_types[$id] = $aggregate['title'];
  288. }
  289. $form['group_type'] = [
  290. '#type' => 'select',
  291. '#title' => $this->t('Aggregation type'),
  292. '#default_value' => $this->options['group_type'],
  293. '#description' => $this->t('Select the aggregation function to use on this field.'),
  294. '#options' => $group_types,
  295. ];
  296. }
  297. /**
  298. * Perform any necessary changes to the form values prior to storage.
  299. * There is no need for this function to actually store the data.
  300. */
  301. public function submitGroupByForm(&$form, FormStateInterface $form_state) {
  302. $form_state->get('handler')->options['group_type'] = $form_state->getValue(['options', 'group_type']);
  303. }
  304. /**
  305. * If a handler has 'extra options' it will get a little settings widget and
  306. * another form called extra_options.
  307. */
  308. public function hasExtraOptions() {
  309. return FALSE;
  310. }
  311. /**
  312. * Provide defaults for the handler.
  313. */
  314. public function defineExtraOptions(&$option) {}
  315. /**
  316. * Provide a form for setting options.
  317. */
  318. public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {}
  319. /**
  320. * Validate the options form.
  321. */
  322. public function validateExtraOptionsForm($form, FormStateInterface $form_state) {}
  323. /**
  324. * Perform any necessary changes to the form values prior to storage.
  325. * There is no need for this function to actually store the data.
  326. */
  327. public function submitExtraOptionsForm($form, FormStateInterface $form_state) {}
  328. /**
  329. * Determine if a handler can be exposed.
  330. */
  331. public function canExpose() {
  332. return FALSE;
  333. }
  334. /**
  335. * Set new exposed option defaults when exposed setting is flipped
  336. * on.
  337. */
  338. public function defaultExposeOptions() {}
  339. /**
  340. * Get information about the exposed form for the form renderer.
  341. */
  342. public function exposedInfo() {}
  343. /**
  344. * Render our chunk of the exposed handler form when selecting
  345. */
  346. public function buildExposedForm(&$form, FormStateInterface $form_state) {}
  347. /**
  348. * Validate the exposed handler form
  349. */
  350. public function validateExposed(&$form, FormStateInterface $form_state) {}
  351. /**
  352. * Submit the exposed handler form
  353. */
  354. public function submitExposed(&$form, FormStateInterface $form_state) {}
  355. /**
  356. * Form for exposed handler options.
  357. */
  358. public function buildExposeForm(&$form, FormStateInterface $form_state) {}
  359. /**
  360. * Validate the options form.
  361. */
  362. public function validateExposeForm($form, FormStateInterface $form_state) {}
  363. /**
  364. * Perform any necessary changes to the form exposes prior to storage.
  365. * There is no need for this function to actually store the data.
  366. */
  367. public function submitExposeForm($form, FormStateInterface $form_state) {}
  368. /**
  369. * Shortcut to display the expose/hide button.
  370. */
  371. public function showExposeButton(&$form, FormStateInterface $form_state) {}
  372. /**
  373. * Shortcut to display the exposed options form.
  374. */
  375. public function showExposeForm(&$form, FormStateInterface $form_state) {
  376. if (empty($this->options['exposed'])) {
  377. return;
  378. }
  379. $this->buildExposeForm($form, $form_state);
  380. // When we click the expose button, we add new gadgets to the form but they
  381. // have no data in POST so their defaults get wiped out. This prevents
  382. // these defaults from getting wiped out. This setting will only be TRUE
  383. // during a 2nd pass rerender.
  384. if ($form_state->get('force_expose_options')) {
  385. foreach (Element::children($form['expose']) as $id) {
  386. if (isset($form['expose'][$id]['#default_value']) && !isset($form['expose'][$id]['#value'])) {
  387. $form['expose'][$id]['#value'] = $form['expose'][$id]['#default_value'];
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * {@inheritdoc}
  394. */
  395. public function access(AccountInterface $account) {
  396. if (isset($this->definition['access callback']) && function_exists($this->definition['access callback'])) {
  397. if (isset($this->definition['access arguments']) && is_array($this->definition['access arguments'])) {
  398. return call_user_func_array($this->definition['access callback'], [$account] + $this->definition['access arguments']);
  399. }
  400. return $this->definition['access callback']($account);
  401. }
  402. return TRUE;
  403. }
  404. /**
  405. * {@inheritdoc}
  406. */
  407. public function preQuery() {
  408. }
  409. /**
  410. * {@inheritdoc}
  411. */
  412. public function query() {
  413. }
  414. /**
  415. * {@inheritdoc}
  416. */
  417. public function postExecute(&$values) {}
  418. /**
  419. * Provides a unique placeholders for handlers.
  420. *
  421. * @return string
  422. * A placeholder which contains the table and the fieldname.
  423. */
  424. protected function placeholder() {
  425. return $this->query->placeholder($this->table . '_' . $this->field);
  426. }
  427. /**
  428. * {@inheritdoc}
  429. */
  430. public function setRelationship() {
  431. // Ensure this gets set to something.
  432. $this->relationship = NULL;
  433. // Don't process non-existent relationships.
  434. if (empty($this->options['relationship']) || $this->options['relationship'] == 'none') {
  435. return;
  436. }
  437. $relationship = $this->options['relationship'];
  438. // Ignore missing/broken relationships.
  439. if (empty($this->view->relationship[$relationship])) {
  440. return;
  441. }
  442. // Check to see if the relationship has already processed. If not, then we
  443. // cannot process it.
  444. if (empty($this->view->relationship[$relationship]->alias)) {
  445. return;
  446. }
  447. // Finally!
  448. $this->relationship = $this->view->relationship[$relationship]->alias;
  449. }
  450. /**
  451. * {@inheritdoc}
  452. */
  453. public function ensureMyTable() {
  454. if (!isset($this->tableAlias)) {
  455. $this->tableAlias = $this->query->ensureTable($this->table, $this->relationship);
  456. }
  457. return $this->tableAlias;
  458. }
  459. /**
  460. * {@inheritdoc}
  461. */
  462. public function adminSummary() {}
  463. /**
  464. * Determine if this item is 'exposed', meaning it provides form elements
  465. * to let users modify the view.
  466. *
  467. * @return bool
  468. */
  469. public function isExposed() {
  470. return !empty($this->options['exposed']);
  471. }
  472. /**
  473. * Returns TRUE if the exposed filter works like a grouped filter.
  474. */
  475. public function isAGroup() {
  476. return FALSE;
  477. }
  478. /**
  479. * Define if the exposed input has to be submitted multiple times.
  480. * This is TRUE when exposed filters grouped are using checkboxes as
  481. * widgets.
  482. */
  483. public function multipleExposedInput() {
  484. return FALSE;
  485. }
  486. /**
  487. * Take input from exposed handlers and assign to this handler, if necessary.
  488. */
  489. public function acceptExposedInput($input) {
  490. return TRUE;
  491. }
  492. /**
  493. * If set to remember exposed input in the session, store it there.
  494. */
  495. public function storeExposedInput($input, $status) {
  496. return TRUE;
  497. }
  498. /**
  499. * {@inheritdoc}
  500. */
  501. public function getJoin() {
  502. // get the join from this table that links back to the base table.
  503. // Determine the primary table to seek
  504. if (empty($this->query->relationships[$this->relationship])) {
  505. $base_table = $this->view->storage->get('base_table');
  506. }
  507. else {
  508. $base_table = $this->query->relationships[$this->relationship]['base'];
  509. }
  510. $join = $this->getTableJoin($this->table, $base_table);
  511. if ($join) {
  512. return clone $join;
  513. }
  514. }
  515. /**
  516. * {@inheritdoc}
  517. */
  518. public function validate() {
  519. return [];
  520. }
  521. /**
  522. * {@inheritdoc}
  523. */
  524. public function broken() {
  525. return FALSE;
  526. }
  527. /**
  528. * Creates cross-database SQL date formatting.
  529. *
  530. * @param string $format
  531. * A format string for the result, like 'Y-m-d H:i:s'.
  532. *
  533. * @return string
  534. * An appropriate SQL string for the DB type and field type.
  535. */
  536. public function getDateFormat($format) {
  537. return $this->query->getDateFormat($this->getDateField(), $format);
  538. }
  539. /**
  540. * Creates cross-database SQL dates.
  541. *
  542. * @return string
  543. * An appropriate SQL string for the db type and field type.
  544. */
  545. public function getDateField() {
  546. return $this->query->getDateField("$this->tableAlias.$this->realField");
  547. }
  548. /**
  549. * Gets views data service.
  550. *
  551. * @return \Drupal\views\ViewsData
  552. */
  553. protected function getViewsData() {
  554. if (!$this->viewsData) {
  555. $this->viewsData = Views::viewsData();
  556. }
  557. return $this->viewsData;
  558. }
  559. /**
  560. * {@inheritdoc}
  561. */
  562. public function setViewsData(ViewsData $views_data) {
  563. $this->viewsData = $views_data;
  564. }
  565. /**
  566. * {@inheritdoc}
  567. */
  568. public static function getTableJoin($table, $base_table) {
  569. $data = Views::viewsData()->get($table);
  570. if (isset($data['table']['join'][$base_table])) {
  571. $join_info = $data['table']['join'][$base_table];
  572. if (!empty($join_info['join_id'])) {
  573. $id = $join_info['join_id'];
  574. }
  575. else {
  576. $id = 'standard';
  577. }
  578. $configuration = $join_info;
  579. // Fill in some easy defaults.
  580. if (empty($configuration['table'])) {
  581. $configuration['table'] = $table;
  582. }
  583. // If this is empty, it's a direct link.
  584. if (empty($configuration['left_table'])) {
  585. $configuration['left_table'] = $base_table;
  586. }
  587. if (isset($join_info['arguments'])) {
  588. foreach ($join_info['arguments'] as $key => $argument) {
  589. $configuration[$key] = $argument;
  590. }
  591. }
  592. $join = Views::pluginManager('join')->createInstance($id, $configuration);
  593. return $join;
  594. }
  595. }
  596. /**
  597. * {@inheritdoc}
  598. */
  599. public function getEntityType() {
  600. // If the user has configured a relationship on the handler take that into
  601. // account.
  602. if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
  603. $relationship = $this->displayHandler->getOption('relationships')[$this->options['relationship']];
  604. $table_data = $this->getViewsData()->get($relationship['table']);
  605. $views_data = $this->getViewsData()->get($table_data[$relationship['field']]['relationship']['base']);
  606. }
  607. else {
  608. $views_data = $this->getViewsData()->get($this->view->storage->get('base_table'));
  609. }
  610. if (isset($views_data['table']['entity type'])) {
  611. return $views_data['table']['entity type'];
  612. }
  613. else {
  614. throw new \Exception("No entity type for field {$this->options['id']} on view {$this->view->storage->id()}");
  615. }
  616. }
  617. /**
  618. * {@inheritdoc}
  619. */
  620. public static function breakString($str, $force_int = FALSE) {
  621. $operator = NULL;
  622. $value = [];
  623. // Determine if the string has 'or' operators (plus signs) or 'and'
  624. // operators (commas) and split the string accordingly.
  625. if (preg_match('/^([\w0-9-_\.]+[+ ]+)+[\w0-9-_\.]+$/u', $str)) {
  626. // The '+' character in a query string may be parsed as ' '.
  627. $operator = 'or';
  628. $value = preg_split('/[+ ]/', $str);
  629. }
  630. elseif (preg_match('/^([\w0-9-_\.]+[, ]+)*[\w0-9-_\.]+$/u', $str)) {
  631. $operator = 'and';
  632. $value = explode(',', $str);
  633. }
  634. // Filter any empty matches (Like from '++' in a string) and reset the
  635. // array keys. 'strlen' is used as the filter callback so we do not lose
  636. // 0 values (would otherwise evaluate == FALSE).
  637. $value = array_values(array_filter($value, 'strlen'));
  638. if ($force_int) {
  639. $value = array_map('intval', $value);
  640. }
  641. return (object) ['value' => $value, 'operator' => $operator];
  642. }
  643. /**
  644. * Displays the Expose form.
  645. */
  646. public function displayExposedForm($form, FormStateInterface $form_state) {
  647. $item = &$this->options;
  648. // flip
  649. $item['exposed'] = empty($item['exposed']);
  650. // If necessary, set new defaults:
  651. if ($item['exposed']) {
  652. $this->defaultExposeOptions();
  653. }
  654. $view = $form_state->get('view');
  655. $display_id = $form_state->get('display_id');
  656. $type = $form_state->get('type');
  657. $id = $form_state->get('id');
  658. $view->getExecutable()->setHandler($display_id, $type, $id, $item);
  659. $view->addFormToStack($form_state->get('form_key'), $display_id, $type, $id, TRUE, TRUE);
  660. $view->cacheSet();
  661. $form_state->set('rerender', TRUE);
  662. $form_state->setRebuild();
  663. $form_state->set('force_expose_options', TRUE);
  664. }
  665. /**
  666. * A submit handler that is used for storing temporary items when using
  667. * multi-step changes, such as ajax requests.
  668. */
  669. public function submitTemporaryForm($form, FormStateInterface $form_state) {
  670. // Run it through the handler's submit function.
  671. $this->submitOptionsForm($form['options'], $form_state);
  672. $item = $this->options;
  673. $types = ViewExecutable::getHandlerTypes();
  674. // For footer/header $handler_type is area but $type is footer/header.
  675. // For all other handle types it's the same.
  676. $handler_type = $type = $form_state->get('type');
  677. if (!empty($types[$type]['type'])) {
  678. $handler_type = $types[$type]['type'];
  679. }
  680. $override = NULL;
  681. $view = $form_state->get('view');
  682. $executable = $view->getExecutable();
  683. if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) {
  684. if (empty($executable->query)) {
  685. $executable->initQuery();
  686. }
  687. $aggregate = $executable->query->getAggregationInfo();
  688. if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
  689. $override = $aggregate[$item['group_type']]['handler'][$type];
  690. }
  691. }
  692. // Create a new handler and unpack the options from the form onto it. We
  693. // can use that for storage.
  694. $handler = Views::handlerManager($handler_type)->getHandler($item, $override);
  695. $handler->init($executable, $executable->display_handler, $item);
  696. // Add the incoming options to existing options because items using
  697. // the extra form may not have everything in the form here.
  698. $options = $form_state->getValue('options') + $this->options;
  699. // This unpacks only options that are in the definition, ensuring random
  700. // extra stuff on the form is not sent through.
  701. $handler->unpackOptions($handler->options, $options, NULL, FALSE);
  702. // Store the item back on the view.
  703. $executable = $view->getExecutable();
  704. $executable->temporary_options[$type][$form_state->get('id')] = $handler->options;
  705. // @todo Decide if \Drupal\views_ui\Form\Ajax\ViewsFormBase::getForm() is
  706. // perhaps the better place to fix the issue.
  707. // \Drupal\views_ui\Form\Ajax\ViewsFormBase::getForm() drops the current
  708. // form from the stack, even if it's an #ajax. So add the item back to the top
  709. // of the stack.
  710. $view->addFormToStack($form_state->get('form_key'), $form_state->get('display_id'), $type, $item['id'], TRUE);
  711. $form_state->get('rerender', TRUE);
  712. $form_state->setRebuild();
  713. // Write to cache
  714. $view->cacheSet();
  715. }
  716. /**
  717. * Calculates options stored on the handler
  718. *
  719. * @param array $options
  720. * The options stored in the handler
  721. * @param array $form_state_options
  722. * The newly submitted form state options.
  723. *
  724. * @return array
  725. * The new options
  726. */
  727. public function submitFormCalculateOptions(array $options, array $form_state_options) {
  728. return $form_state_options + $options;
  729. }
  730. /**
  731. * {@inheritdoc}
  732. */
  733. public function calculateDependencies() {
  734. $dependencies = parent::calculateDependencies();
  735. if ($this->table) {
  736. // Ensure that the view depends on the module that provides the table.
  737. $data = $this->getViewsData()->get($this->table);
  738. if (isset($data['table']['provider'])) {
  739. $dependencies['module'][] = $data['table']['provider'];
  740. }
  741. }
  742. return $dependencies;
  743. }
  744. }