3 namespace Drupal\views_ui\Controller;
5 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\ViewEntityInterface;
9 use Drupal\views\Views;
10 use Drupal\views_ui\ViewUI;
11 use Drupal\views\ViewsData;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\JsonResponse;
15 use Drupal\Core\Ajax\AjaxResponse;
16 use Drupal\Core\Ajax\ReplaceCommand;
17 use Drupal\Component\Utility\Html;
20 * Returns responses for Views UI routes.
22 class ViewsUIController extends ControllerBase {
25 * Stores the Views data cache object.
27 * @var \Drupal\views\ViewsData
32 * Constructs a new \Drupal\views_ui\Controller\ViewsUIController object.
34 * @param \Drupal\views\ViewsData $views_data
35 * The Views data cache object.
37 public function __construct(ViewsData $views_data) {
38 $this->viewsData = $views_data;
44 public static function create(ContainerInterface $container) {
46 $container->get('views.views_data')
51 * Lists all instances of fields on any views.
54 * The Views fields report page.
56 public function reportFields() {
57 $views = $this->entityManager()->getStorage('view')->loadMultiple();
59 // Fetch all fieldapi fields which are used in views
60 // Therefore search in all views, displays and handler-types.
62 $handler_types = ViewExecutable::getHandlerTypes();
63 foreach ($views as $view) {
64 $executable = $view->getExecutable();
65 $executable->initDisplay();
66 foreach ($executable->displayHandlers as $display_id => $display) {
67 if ($executable->setDisplay($display_id)) {
68 foreach ($handler_types as $type => $info) {
69 foreach ($executable->getHandlers($type, $display_id) as $item) {
70 $table_data = $this->viewsData->get($item['table']);
71 if (isset($table_data[$item['field']]) && isset($table_data[$item['field']][$type])
72 && $field_data = $table_data[$item['field']][$type]) {
73 // The final check that we have a fieldapi field now.
74 if (isset($field_data['field_name'])) {
75 $fields[$field_data['field_name']][$view->id()] = $view->id();
84 $header = [t('Field name'), t('Used in')];
86 foreach ($fields as $field_name => $views) {
87 $rows[$field_name]['data'][0]['data']['#plain_text'] = $field_name;
88 foreach ($views as $view) {
89 $rows[$field_name]['data'][1][] = $this->l($view, new Url('entity.view.edit_form', ['view' => $view]));
92 '#theme' => 'item_list',
93 '#items' => $rows[$field_name]['data'][1],
94 '#context' => ['list_style' => 'comma-list'],
96 $rows[$field_name]['data'][1] = ['data' => $item_list];
99 // Sort rows by field name.
103 '#header' => $header,
105 '#empty' => t('No fields have been used in views yet.'),
112 * Lists all plugins and what enabled Views use them.
115 * The Views plugins report page.
117 public function reportPlugins() {
118 $rows = Views::pluginList();
119 foreach ($rows as &$row) {
121 // Link each view name to the view itself.
122 foreach ($row['views'] as $row_name => $view) {
123 $views[] = $this->l($view, new Url('entity.view.edit_form', ['view' => $view]));
125 unset($row['views']);
126 $row['views']['data'] = [
127 '#theme' => 'item_list',
129 '#context' => ['list_style' => 'comma-list'],
133 // Sort rows by field name.
137 '#header' => [t('Type'), t('Name'), t('Provided by'), t('Used in')],
139 '#empty' => t('There are no enabled views.'),
144 * Calls a method on a view and reloads the listing page.
146 * @param \Drupal\views\ViewEntityInterface $view
147 * The view being acted upon.
149 * The operation to perform, e.g., 'enable' or 'disable'.
150 * @param \Symfony\Component\HttpFoundation\Request $request
151 * The current request.
153 * @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
154 * Either returns a rebuilt listing page as an AJAX response, or redirects
155 * back to the listing page.
157 public function ajaxOperation(ViewEntityInterface $view, $op, Request $request) {
158 // Perform the operation.
159 $view->$op()->save();
161 // If the request is via AJAX, return the rendered list as JSON.
162 if ($request->request->get('js')) {
163 $list = $this->entityManager()->getListBuilder('view')->render();
164 $response = new AjaxResponse();
165 $response->addCommand(new ReplaceCommand('#views-entity-list', $list));
169 // Otherwise, redirect back to the page.
170 return $this->redirect('entity.view.collection');
174 * Menu callback for Views tag autocompletion.
176 * Like other autocomplete functions, this function inspects the 'q' query
177 * parameter for the string to use to search for suggestions.
179 * @return \Symfony\Component\HttpFoundation\JsonResponse
180 * A JSON response containing the autocomplete suggestions for Views tags.
182 public function autocompleteTag(Request $request) {
184 $string = $request->query->get('q');
185 // Get matches from default views.
186 $views = $this->entityManager()->getStorage('view')->loadMultiple();
187 // Keep track of previously processed tags so they can be skipped.
189 foreach ($views as $view) {
190 $tag = $view->get('tag');
191 if ($tag && !in_array($tag, $tags)) {
193 if (strpos($tag, $string) === 0) {
194 $matches[] = ['value' => $tag, 'label' => Html::escape($tag)];
195 if (count($matches) >= 10) {
202 return new JsonResponse($matches);
206 * Returns the form to edit a view.
208 * @param \Drupal\views_ui\ViewUI $view
209 * The view to be edited.
210 * @param string|null $display_id
211 * (optional) The display ID being edited. Defaults to NULL, which will load
212 * the first available display.
215 * An array containing the Views edit and preview forms.
217 public function edit(ViewUI $view, $display_id = NULL) {
218 $name = $view->label();
219 $data = $this->viewsData->get($view->get('base_table'));
221 if (isset($data['table']['base']['title'])) {
222 $name .= ' (' . $data['table']['base']['title'] . ')';
224 $build['#title'] = $name;
226 $build['edit'] = $this->entityFormBuilder()->getForm($view, 'edit', ['display_id' => $display_id]);
227 $build['preview'] = $this->entityFormBuilder()->getForm($view, 'preview', ['display_id' => $display_id]);