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