Chris@0: viewsData = $views_data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('views.views_data') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Lists all instances of fields on any views. Chris@0: * Chris@0: * @return array Chris@0: * The Views fields report page. Chris@0: */ Chris@0: public function reportFields() { Chris@18: $views = $this->entityTypeManager()->getStorage('view')->loadMultiple(); Chris@0: Chris@0: // Fetch all fieldapi fields which are used in views Chris@0: // Therefore search in all views, displays and handler-types. Chris@0: $fields = []; Chris@0: $handler_types = ViewExecutable::getHandlerTypes(); Chris@0: foreach ($views as $view) { Chris@0: $executable = $view->getExecutable(); Chris@0: $executable->initDisplay(); Chris@0: foreach ($executable->displayHandlers as $display_id => $display) { Chris@0: if ($executable->setDisplay($display_id)) { Chris@0: foreach ($handler_types as $type => $info) { Chris@0: foreach ($executable->getHandlers($type, $display_id) as $item) { Chris@0: $table_data = $this->viewsData->get($item['table']); Chris@0: if (isset($table_data[$item['field']]) && isset($table_data[$item['field']][$type]) Chris@0: && $field_data = $table_data[$item['field']][$type]) { Chris@0: // The final check that we have a fieldapi field now. Chris@0: if (isset($field_data['field_name'])) { Chris@0: $fields[$field_data['field_name']][$view->id()] = $view->id(); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $header = [t('Field name'), t('Used in')]; Chris@0: $rows = []; Chris@0: foreach ($fields as $field_name => $views) { Chris@0: $rows[$field_name]['data'][0]['data']['#plain_text'] = $field_name; Chris@0: foreach ($views as $view) { Chris@0: $rows[$field_name]['data'][1][] = $this->l($view, new Url('entity.view.edit_form', ['view' => $view])); Chris@0: } Chris@0: $item_list = [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $rows[$field_name]['data'][1], Chris@0: '#context' => ['list_style' => 'comma-list'], Chris@0: ]; Chris@0: $rows[$field_name]['data'][1] = ['data' => $item_list]; Chris@0: } Chris@0: Chris@0: // Sort rows by field name. Chris@0: ksort($rows); Chris@0: $output = [ Chris@0: '#type' => 'table', Chris@0: '#header' => $header, Chris@0: '#rows' => $rows, Chris@0: '#empty' => t('No fields have been used in views yet.'), Chris@0: ]; Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Lists all plugins and what enabled Views use them. Chris@0: * Chris@0: * @return array Chris@0: * The Views plugins report page. Chris@0: */ Chris@0: public function reportPlugins() { Chris@0: $rows = Views::pluginList(); Chris@0: foreach ($rows as &$row) { Chris@0: $views = []; Chris@0: // Link each view name to the view itself. Chris@0: foreach ($row['views'] as $row_name => $view) { Chris@0: $views[] = $this->l($view, new Url('entity.view.edit_form', ['view' => $view])); Chris@0: } Chris@0: unset($row['views']); Chris@0: $row['views']['data'] = [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $views, Chris@0: '#context' => ['list_style' => 'comma-list'], Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Sort rows by field name. Chris@0: ksort($rows); Chris@0: return [ Chris@0: '#type' => 'table', Chris@0: '#header' => [t('Type'), t('Name'), t('Provided by'), t('Used in')], Chris@0: '#rows' => $rows, Chris@0: '#empty' => t('There are no enabled views.'), Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Calls a method on a view and reloads the listing page. Chris@0: * Chris@0: * @param \Drupal\views\ViewEntityInterface $view Chris@0: * The view being acted upon. Chris@0: * @param string $op Chris@0: * The operation to perform, e.g., 'enable' or 'disable'. Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The current request. Chris@0: * Chris@0: * @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * Either returns a rebuilt listing page as an AJAX response, or redirects Chris@0: * back to the listing page. Chris@0: */ Chris@0: public function ajaxOperation(ViewEntityInterface $view, $op, Request $request) { Chris@0: // Perform the operation. Chris@0: $view->$op()->save(); Chris@0: Chris@0: // If the request is via AJAX, return the rendered list as JSON. Chris@0: if ($request->request->get('js')) { Chris@18: $list = $this->entityTypeManager()->getListBuilder('view')->render(); Chris@0: $response = new AjaxResponse(); Chris@0: $response->addCommand(new ReplaceCommand('#views-entity-list', $list)); Chris@0: return $response; Chris@0: } Chris@0: Chris@0: // Otherwise, redirect back to the page. Chris@0: return $this->redirect('entity.view.collection'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Menu callback for Views tag autocompletion. Chris@0: * Chris@0: * Like other autocomplete functions, this function inspects the 'q' query Chris@0: * parameter for the string to use to search for suggestions. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\JsonResponse Chris@0: * A JSON response containing the autocomplete suggestions for Views tags. Chris@0: */ Chris@0: public function autocompleteTag(Request $request) { Chris@0: $matches = []; Chris@0: $string = $request->query->get('q'); Chris@0: // Get matches from default views. Chris@18: $views = $this->entityTypeManager()->getStorage('view')->loadMultiple(); Chris@0: // Keep track of previously processed tags so they can be skipped. Chris@0: $tags = []; Chris@0: foreach ($views as $view) { Chris@0: $tag = $view->get('tag'); Chris@0: if ($tag && !in_array($tag, $tags)) { Chris@0: $tags[] = $tag; Chris@0: if (strpos($tag, $string) === 0) { Chris@0: $matches[] = ['value' => $tag, 'label' => Html::escape($tag)]; Chris@0: if (count($matches) >= 10) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return new JsonResponse($matches); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the form to edit a view. Chris@0: * Chris@0: * @param \Drupal\views_ui\ViewUI $view Chris@0: * The view to be edited. Chris@0: * @param string|null $display_id Chris@0: * (optional) The display ID being edited. Defaults to NULL, which will load Chris@0: * the first available display. Chris@0: * Chris@0: * @return array Chris@0: * An array containing the Views edit and preview forms. Chris@0: */ Chris@0: public function edit(ViewUI $view, $display_id = NULL) { Chris@0: $name = $view->label(); Chris@0: $data = $this->viewsData->get($view->get('base_table')); Chris@0: Chris@0: if (isset($data['table']['base']['title'])) { Chris@0: $name .= ' (' . $data['table']['base']['title'] . ')'; Chris@0: } Chris@0: $build['#title'] = $name; Chris@0: Chris@0: $build['edit'] = $this->entityFormBuilder()->getForm($view, 'edit', ['display_id' => $display_id]); Chris@0: $build['preview'] = $this->entityFormBuilder()->getForm($view, 'preview', ['display_id' => $display_id]); Chris@0: return $build; Chris@0: } Chris@0: Chris@0: }