PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views_ui/src/Form/Ajax/ConfigHandler.php

https://gitlab.com/reasonat/test8
PHP | 280 lines | 190 code | 36 blank | 54 comment | 27 complexity | 993395e6e174f75c4f4d39f9b093119e MD5 | raw file
  1. <?php
  2. namespace Drupal\views_ui\Form\Ajax;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\views\ViewEntityInterface;
  5. use Drupal\views\ViewExecutable;
  6. use Drupal\views\Views;
  7. use Symfony\Component\HttpFoundation\Request;
  8. /**
  9. * Provides a form for configuring an item in the Views UI.
  10. */
  11. class ConfigHandler extends ViewsFormBase {
  12. /**
  13. * Constructs a new ConfigHandler object.
  14. */
  15. public function __construct($type = NULL, $id = NULL) {
  16. $this->setType($type);
  17. $this->setID($id);
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getFormKey() {
  23. return 'handler';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL, $id = NULL) {
  29. $this->setType($type);
  30. $this->setID($id);
  31. return parent::getForm($view, $display_id, $js);
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getFormId() {
  37. return 'views_ui_config_item_form';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  43. /** @var \Drupal\views\Entity\View $view */
  44. $view = $form_state->get('view');
  45. $display_id = $form_state->get('display_id');
  46. $type = $form_state->get('type');
  47. $id = $form_state->get('id');
  48. $form = array(
  49. 'options' => array(
  50. '#tree' => TRUE,
  51. '#theme_wrappers' => array('container'),
  52. '#attributes' => array('class' => array('scroll'), 'data-drupal-views-scroll' => TRUE),
  53. ),
  54. );
  55. $executable = $view->getExecutable();
  56. $save_ui_cache = FALSE;
  57. if (!$executable->setDisplay($display_id)) {
  58. $form['markup'] = array('#markup' => $this->t('Invalid display id @display', array('@display' => $display_id)));
  59. return $form;
  60. }
  61. $item = $executable->getHandler($display_id, $type, $id);
  62. if ($item) {
  63. $handler = $executable->display_handler->getHandler($type, $id);
  64. if (empty($handler)) {
  65. $form['markup'] = array('#markup' => $this->t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  66. }
  67. else {
  68. $types = ViewExecutable::getHandlerTypes();
  69. // If this item can come from the default display, show a dropdown
  70. // that lets the user choose which display the changes should apply to.
  71. if ($executable->display_handler->defaultableSections($types[$type]['plural'])) {
  72. $section = $types[$type]['plural'];
  73. $form_state->set('section', $section);
  74. views_ui_standard_display_dropdown($form, $form_state, $section);
  75. }
  76. // A whole bunch of code to figure out what relationships are valid for
  77. // this item.
  78. $relationships = $executable->display_handler->getOption('relationships');
  79. $relationship_options = array();
  80. foreach ($relationships as $relationship) {
  81. // relationships can't link back to self. But also, due to ordering,
  82. // relationships can only link to prior relationships.
  83. if ($type == 'relationship' && $id == $relationship['id']) {
  84. break;
  85. }
  86. $relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
  87. // ignore invalid/broken relationships.
  88. if (empty($relationship_handler)) {
  89. continue;
  90. }
  91. // If this relationship is valid for this type, add it to the list.
  92. $data = Views::viewsData()->get($relationship['table']);
  93. if (isset($data[$relationship['field']]['relationship']['base']) && $base = $data[$relationship['field']]['relationship']['base']) {
  94. $base_fields = Views::viewsDataHelper()->fetchFields($base, $type, $executable->display_handler->useGroupBy());
  95. if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  96. $relationship_handler->init($executable, $executable->display_handler, $relationship);
  97. $relationship_options[$relationship['id']] = $relationship_handler->adminLabel();
  98. }
  99. }
  100. }
  101. if (!empty($relationship_options)) {
  102. // Make sure the existing relationship is even valid. If not, force
  103. // it to none.
  104. $base_fields = Views::viewsDataHelper()->fetchFields($view->get('base_table'), $type, $executable->display_handler->useGroupBy());
  105. if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  106. $relationship_options = array_merge(array('none' => $this->t('Do not use a relationship')), $relationship_options);
  107. }
  108. $rel = empty($item['relationship']) ? 'none' : $item['relationship'];
  109. if (empty($relationship_options[$rel])) {
  110. // Pick the first relationship.
  111. $rel = key($relationship_options);
  112. // We want this relationship option to get saved even if the user
  113. // skips submitting the form.
  114. $executable->setHandlerOption($display_id, $type, $id, 'relationship', $rel);
  115. $save_ui_cache = TRUE;
  116. // Re-initialize with new relationship.
  117. $item['relationship'] = $rel;
  118. $handler->init($executable, $executable->display_handler, $item);
  119. }
  120. $form['options']['relationship'] = array(
  121. '#type' => 'select',
  122. '#title' => $this->t('Relationship'),
  123. '#options' => $relationship_options,
  124. '#default_value' => $rel,
  125. '#weight' => -500,
  126. );
  127. }
  128. else {
  129. $form['options']['relationship'] = array(
  130. '#type' => 'value',
  131. '#value' => 'none',
  132. );
  133. }
  134. $form['#title'] = $this->t('Configure @type: @item', array('@type' => $types[$type]['lstitle'], '@item' => $handler->adminLabel()));
  135. if (!empty($handler->definition['help'])) {
  136. $form['options']['form_description'] = array(
  137. '#markup' => $handler->definition['help'],
  138. '#theme_wrappers' => array('container'),
  139. '#attributes' => array('class' => array('js-form-item form-item description')),
  140. '#weight' => -1000,
  141. );
  142. }
  143. $form['#section'] = $display_id . '-' . $type . '-' . $id;
  144. // Get form from the handler.
  145. $handler->buildOptionsForm($form['options'], $form_state);
  146. $form_state->set('handler', $handler);
  147. }
  148. $name = $form_state->get('update_name');
  149. $view->getStandardButtons($form, $form_state, 'views_ui_config_item_form', $name);
  150. // Add a 'remove' button.
  151. $form['actions']['remove'] = array(
  152. '#type' => 'submit',
  153. '#value' => $this->t('Remove'),
  154. '#submit' => array(array($this, 'remove')),
  155. '#limit_validation_errors' => array(array('override')),
  156. '#button_type' => 'danger',
  157. );
  158. }
  159. if ($save_ui_cache) {
  160. $view->cacheSet();
  161. }
  162. return $form;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function validateForm(array &$form, FormStateInterface $form_state) {
  168. $form_state->get('handler')->validateOptionsForm($form['options'], $form_state);
  169. if ($form_state->getErrors()) {
  170. $form_state->set('rerender', TRUE);
  171. }
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function submitForm(array &$form, FormStateInterface $form_state) {
  177. $view = $form_state->get('view');
  178. $display_id = $form_state->get('display_id');
  179. $id = $form_state->get('id');
  180. $handler = $form_state->get('handler');
  181. // Run it through the handler's submit function.
  182. $handler->submitOptionsForm($form['options'], $form_state);
  183. $item = $handler->options;
  184. $types = ViewExecutable::getHandlerTypes();
  185. // For footer/header $handler_type is area but $type is footer/header.
  186. // For all other handle types it's the same.
  187. $handler_type = $type = $form_state->get('type');
  188. if (!empty($types[$type]['type'])) {
  189. $handler_type = $types[$type]['type'];
  190. }
  191. $override = NULL;
  192. $executable = $view->getExecutable();
  193. if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) {
  194. if (empty($executable->query)) {
  195. $executable->initQuery();
  196. }
  197. $aggregate = $executable->query->getAggregationInfo();
  198. if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
  199. $override = $aggregate[$item['group_type']]['handler'][$type];
  200. }
  201. }
  202. // Create a new handler and unpack the options from the form onto it. We
  203. // can use that for storage.
  204. $handler = Views::handlerManager($handler_type)->getHandler($item, $override);
  205. $handler->init($executable, $executable->display_handler, $item);
  206. // Add the incoming options to existing options because items using
  207. // the extra form may not have everything in the form here.
  208. $options = $handler->submitFormCalculateOptions($handler->options, $form_state->getValue('options', []));
  209. // This unpacks only options that are in the definition, ensuring random
  210. // extra stuff on the form is not sent through.
  211. $handler->unpackOptions($handler->options, $options, NULL, FALSE);
  212. // Store the item back on the view
  213. $executable->setHandler($display_id, $type, $id, $handler->options);
  214. // Ensure any temporary options are removed.
  215. if (isset($view->temporary_options[$type][$id])) {
  216. unset($view->temporary_options[$type][$id]);
  217. }
  218. // Write to cache
  219. $view->cacheSet();
  220. }
  221. /**
  222. * Submit handler for removing an item from a view
  223. */
  224. public function remove(&$form, FormStateInterface $form_state) {
  225. $view = $form_state->get('view');
  226. $display_id = $form_state->get('display_id');
  227. $type = $form_state->get('type');
  228. $id = $form_state->get('id');
  229. // Store the item back on the view
  230. list($was_defaulted, $is_defaulted) = $view->getOverrideValues($form, $form_state);
  231. $executable = $view->getExecutable();
  232. // If the display selection was changed toggle the override value.
  233. if ($was_defaulted != $is_defaulted) {
  234. $display = &$executable->displayHandlers->get($display_id);
  235. $display->optionsOverride($form, $form_state);
  236. }
  237. $executable->removeHandler($display_id, $type, $id);
  238. // Write to cache
  239. $view->cacheSet();
  240. }
  241. }