views_ui.module 13 KB

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