views_ui.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * @file
  4. * Provide structure for the administrative interface to Views.
  5. */
  6. use Drupal\Component\Utility\Unicode;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\Core\Url;
  9. use Drupal\views\ViewExecutable;
  10. use Drupal\views\Analyzer;
  11. /**
  12. * Implements hook_help().
  13. */
  14. function views_ui_help($route_name, RouteMatchInterface $route_match) {
  15. switch ($route_name) {
  16. case 'help.page.views_ui':
  17. $output = '';
  18. $output .= '<h3>' . t('About') . '</h3>';
  19. $output .= '<p>' . t('The Views UI module provides an interface for managing views for the <a href=":views">Views module</a>. For more information, see the <a href=":handbook">online documentation for the Views UI module</a>.', [':views' => \Drupal::url('help.page', ['name' => 'views']), ':handbook' => 'https://www.drupal.org/documentation/modules/views_ui']) . '</p>';
  20. $output .= '<h3>' . t('Uses') . '</h3>';
  21. $output .= '<dl>';
  22. $output .= '<dt>' . t('Creating and managing views') . '</dt>';
  23. $output .= '<dd>' . t('Views can be created from the <a href=":list">Views list page</a> by using the "Add view" action. Existing views can be managed from the <a href=":list">Views list page</a> by locating the view in the "Enabled" or "Disabled" list and selecting the desired operation action, for example "Edit".', [':list' => \Drupal::url('entity.view.collection', ['name' => 'views_ui'])]) . '</dd>';
  24. $output .= '<dt>' . t('Enabling and disabling views') . '<dt>';
  25. $output .= '<dd>' . t('Views can be enabled or disabled from the <a href=":list">Views list page</a>. To enable a view, find the view within the "Disabled" list and select the "Enable" operation. To disable a view find the view within the "Enabled" list and select the "Disable" operation.', [':list' => \Drupal::url('entity.view.collection', ['name' => 'views_ui'])]) . '</dd>';
  26. $output .= '<dt>' . t('Exporting and importing views') . '</dt>';
  27. $output .= '<dd>' . t('Views can be exported and imported as configuration files by using the <a href=":config">Configuration Manager module</a>.', [':config' => (\Drupal::moduleHandler()->moduleExists('config')) ? \Drupal::url('help.page', ['name' => 'config']) : '#']) . '</dd>';
  28. $output .= '</dl>';
  29. return $output;
  30. }
  31. }
  32. /**
  33. * Implements hook_entity_type_build().
  34. */
  35. function views_ui_entity_type_build(array &$entity_types) {
  36. /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  37. $entity_types['view']
  38. ->setFormClass('edit', 'Drupal\views_ui\ViewEditForm')
  39. ->setFormClass('add', 'Drupal\views_ui\ViewAddForm')
  40. ->setFormClass('preview', 'Drupal\views_ui\ViewPreviewForm')
  41. ->setFormClass('duplicate', 'Drupal\views_ui\ViewDuplicateForm')
  42. ->setFormClass('delete', 'Drupal\Core\Entity\EntityDeleteForm')
  43. ->setFormClass('break_lock', 'Drupal\views_ui\Form\BreakLockForm')
  44. ->setListBuilderClass('Drupal\views_ui\ViewListBuilder')
  45. ->setLinkTemplate('edit-form', '/admin/structure/views/view/{view}')
  46. ->setLinkTemplate('edit-display-form', '/admin/structure/views/view/{view}/edit/{display_id}')
  47. ->setLinkTemplate('preview-form', '/admin/structure/views/view/{view}/preview/{display_id}')
  48. ->setLinkTemplate('duplicate-form', '/admin/structure/views/view/{view}/duplicate')
  49. ->setLinkTemplate('delete-form', '/admin/structure/views/view/{view}/delete')
  50. ->setLinkTemplate('enable', '/admin/structure/views/view/{view}/enable')
  51. ->setLinkTemplate('disable', '/admin/structure/views/view/{view}/disable')
  52. ->setLinkTemplate('break-lock-form', '/admin/structure/views/view/{view}/break-lock')
  53. ->setLinkTemplate('collection', '/admin/structure/views');
  54. }
  55. /**
  56. * Implements hook_theme().
  57. */
  58. function views_ui_theme() {
  59. return [
  60. // edit a view
  61. 'views_ui_display_tab_setting' => [
  62. 'variables' => ['description' => '', 'link' => '', 'settings_links' => [], 'overridden' => FALSE, 'defaulted' => FALSE, 'description_separator' => TRUE, 'class' => []],
  63. 'file' => 'views_ui.theme.inc',
  64. ],
  65. 'views_ui_display_tab_bucket' => [
  66. 'render element' => 'element',
  67. 'file' => 'views_ui.theme.inc',
  68. ],
  69. 'views_ui_rearrange_filter_form' => [
  70. 'render element' => 'form',
  71. 'file' => 'views_ui.theme.inc',
  72. ],
  73. 'views_ui_expose_filter_form' => [
  74. 'render element' => 'form',
  75. 'file' => 'views_ui.theme.inc',
  76. ],
  77. // Legacy theme hook for displaying views info.
  78. 'views_ui_view_info' => [
  79. 'variables' => ['view' => NULL, 'displays' => NULL],
  80. 'file' => 'views_ui.theme.inc',
  81. ],
  82. // List views.
  83. 'views_ui_views_listing_table' => [
  84. 'variables' => [
  85. 'headers' => NULL,
  86. 'rows' => NULL,
  87. 'attributes' => [],
  88. ],
  89. 'file' => 'views_ui.theme.inc',
  90. ],
  91. 'views_ui_view_displays_list' => [
  92. 'variables' => ['displays' => []],
  93. ],
  94. // Group of filters.
  95. 'views_ui_build_group_filter_form' => [
  96. 'render element' => 'form',
  97. 'file' => 'views_ui.theme.inc',
  98. ],
  99. // On behalf of a plugin
  100. 'views_ui_style_plugin_table' => [
  101. 'render element' => 'form',
  102. 'file' => 'views_ui.theme.inc',
  103. ],
  104. // When previewing a view.
  105. 'views_ui_view_preview_section' => [
  106. 'variables' => ['view' => NULL, 'section' => NULL, 'content' => NULL, 'links' => ''],
  107. 'file' => 'views_ui.theme.inc',
  108. ],
  109. // Generic container wrapper, to use instead of theme_container when an id
  110. // is not desired.
  111. 'views_ui_container' => [
  112. 'variables' => ['children' => NULL, 'attributes' => []],
  113. 'file' => 'views_ui.theme.inc',
  114. ],
  115. ];
  116. }
  117. /**
  118. * Implements hook_preprocess_HOOK() for views templates.
  119. */
  120. function views_ui_preprocess_views_view(&$variables) {
  121. $view = $variables['view'];
  122. // Render title for the admin preview.
  123. if (!empty($view->live_preview)) {
  124. $variables['title'] = [
  125. '#markup' => $view->getTitle()
  126. ];
  127. }
  128. if (!empty($view->live_preview) && \Drupal::moduleHandler()->moduleExists('contextual')) {
  129. $view->setShowAdminLinks(FALSE);
  130. foreach (['title', 'header', 'exposed', 'rows', 'pager', 'more', 'footer', 'empty', 'attachment_after', 'attachment_before'] as $section) {
  131. if (!empty($variables[$section])) {
  132. $variables[$section] = [
  133. '#theme' => 'views_ui_view_preview_section',
  134. '#view' => $view,
  135. '#section' => $section,
  136. '#content' => $variables[$section],
  137. '#theme_wrappers' => ['views_ui_container'],
  138. '#attributes' => ['class' => ['contextual-region']],
  139. ];
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * Returns contextual links for each handler of a certain section.
  146. *
  147. * @TODO
  148. * Bring in relationships
  149. * Refactor this function to use much stuff of views_ui_edit_form_get_bucket.
  150. *
  151. * @param $title
  152. * Add a bolded title of this section.
  153. */
  154. function views_ui_view_preview_section_handler_links(ViewExecutable $view, $type, $title = FALSE) {
  155. $display = $view->display_handler->display;
  156. $handlers = $view->display_handler->getHandlers($type);
  157. $links = [];
  158. $types = ViewExecutable::getHandlerTypes();
  159. if ($title) {
  160. $links[$type . '-title'] = [
  161. 'title' => $types[$type]['title'],
  162. ];
  163. }
  164. foreach ($handlers as $id => $handler) {
  165. $field_name = $handler->adminLabel(TRUE);
  166. $links[$type . '-edit-' . $id] = [
  167. 'title' => t('Edit @section', ['@section' => $field_name]),
  168. 'url' => Url::fromRoute('views_ui.form_handler', ['js' => 'nojs', 'view' => $view->storage->id(), 'display_id' => $display['id'], 'type' => $type, 'id' => $id]),
  169. 'attributes' => ['class' => ['views-ajax-link']],
  170. ];
  171. }
  172. $links[$type . '-add'] = [
  173. 'title' => t('Add new'),
  174. 'url' => Url::fromRoute('views_ui.form_add_handler', ['js' => 'nojs', 'view' => $view->storage->id(), 'display_id' => $display['id'], 'type' => $type]),
  175. 'attributes' => ['class' => ['views-ajax-link']],
  176. ];
  177. return $links;
  178. }
  179. /**
  180. * Returns a link to editing a certain display setting.
  181. */
  182. function views_ui_view_preview_section_display_category_links(ViewExecutable $view, $type, $title) {
  183. $display = $view->display_handler->display;
  184. $links = [
  185. $type . '-edit' => [
  186. 'title' => t('Edit @section', ['@section' => $title]),
  187. 'url' => Url::fromRoute('views_ui.form_display', ['js' => 'nojs', 'view' => $view->storage->id(), 'display_id' => $display['id'], 'type' => $type]),
  188. 'attributes' => ['class' => ['views-ajax-link']],
  189. ],
  190. ];
  191. return $links;
  192. }
  193. /**
  194. * Returns all contextual links for the main content part of the view.
  195. */
  196. function views_ui_view_preview_section_rows_links(ViewExecutable $view) {
  197. $links = [];
  198. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'filter', TRUE));
  199. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'field', TRUE));
  200. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'sort', TRUE));
  201. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'argument', TRUE));
  202. $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'relationship', TRUE));
  203. return $links;
  204. }
  205. /**
  206. * Implements hook_views_plugins_display_alter().
  207. */
  208. function views_ui_views_plugins_display_alter(&$plugins) {
  209. // Attach contextual links to each display plugin. The links will point to
  210. // paths underneath "admin/structure/views/view/{$view->id()}" (i.e., paths
  211. // for editing and performing other contextual actions on the view).
  212. foreach ($plugins as &$display) {
  213. $display['contextual links']['entity.view.edit_form'] = [
  214. 'route_name' => 'entity.view.edit_form',
  215. 'route_parameters_names' => ['view' => 'id'],
  216. ];
  217. }
  218. }
  219. /**
  220. * Implements hook_contextual_links_view_alter().
  221. */
  222. function views_ui_contextual_links_view_alter(&$element, $items) {
  223. // Remove contextual links from being rendered, when so desired, such as
  224. // within a View preview.
  225. if (views_ui_contextual_links_suppress()) {
  226. $element['#links'] = [];
  227. }
  228. // Append the display ID to the Views UI edit links, so that clicking on the
  229. // contextual link takes you directly to the correct display tab on the edit
  230. // screen.
  231. elseif (!empty($element['#links']['entityviewedit-form'])) {
  232. $display_id = $items['entity.view.edit_form']['metadata']['display_id'];
  233. $route_parameters = $element['#links']['entityviewedit-form']['url']->getRouteParameters() + ['display_id' => $display_id];
  234. $element['#links']['entityviewedit-form']['url'] = Url::fromRoute('entity.view.edit_display_form', $route_parameters);
  235. }
  236. }
  237. /**
  238. * Sets a static variable for controlling whether contextual links are rendered.
  239. *
  240. * @see views_ui_contextual_links_view_alter()
  241. */
  242. function views_ui_contextual_links_suppress($set = NULL) {
  243. $suppress = &drupal_static(__FUNCTION__);
  244. if (isset($set)) {
  245. $suppress = $set;
  246. }
  247. return $suppress;
  248. }
  249. /**
  250. * Increments the views_ui_contextual_links_suppress() static variable.
  251. *
  252. * When this function is added to the #pre_render of an element, and
  253. * 'views_ui_contextual_links_suppress_pop' is added to the #post_render of the
  254. * same element, then all contextual links within the element and its
  255. * descendants are suppressed from being rendered. This is used, for example,
  256. * during a View preview, when it is not desired for nodes in the Views result
  257. * to have contextual links.
  258. *
  259. * @see views_ui_contextual_links_suppress_pop()
  260. */
  261. function views_ui_contextual_links_suppress_push() {
  262. views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress()) + 1);
  263. }
  264. /**
  265. * Decrements the views_ui_contextual_links_suppress() static variable.
  266. *
  267. * @see views_ui_contextual_links_suppress_push()
  268. */
  269. function views_ui_contextual_links_suppress_pop() {
  270. views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress()) - 1);
  271. }
  272. /**
  273. * Implements hook_views_analyze().
  274. *
  275. * This is the basic views analysis that checks for very minimal problems.
  276. * There are other analysis tools in core specific sections, such as
  277. * node.views.inc as well.
  278. */
  279. function views_ui_views_analyze(ViewExecutable $view) {
  280. $ret = [];
  281. // Check for something other than the default display:
  282. if (count($view->displayHandlers) < 2) {
  283. $ret[] = Analyzer::formatMessage(t('This view has only a default display and therefore will not be placed anywhere on your site; perhaps you want to add a page or a block display.'), 'warning');
  284. }
  285. // You can give a page display the same path as an alias existing in the
  286. // system, so the alias will not work anymore. Report this to the user,
  287. // because he probably wanted something else.
  288. foreach ($view->displayHandlers as $display) {
  289. if (empty($display)) {
  290. continue;
  291. }
  292. if ($display->hasPath() && $path = $display->getOption('path')) {
  293. $normal_path = \Drupal::service('path.alias_manager')->getPathByAlias($path);
  294. if ($path != $normal_path) {
  295. $ret[] = Analyzer::formatMessage(t('You have configured display %display with a path which is an path alias as well. This might lead to unwanted effects so better use an internal path.', ['%display' => $display->display['display_title']]), 'warning');
  296. }
  297. }
  298. }
  299. return $ret;
  300. }
  301. /**
  302. * Truncate strings to a set length and provide a '...' if they truncated.
  303. *
  304. * This is often used in the UI to ensure long strings fit.
  305. */
  306. function views_ui_truncate($string, $length) {
  307. if (Unicode::strlen($string) > $length) {
  308. $string = Unicode::substr($string, 0, $length);
  309. $string .= '...';
  310. }
  311. return $string;
  312. }