You are here

class ViewsUIController in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController

Returns responses for Views UI routes.

Hierarchy

Expanded class hierarchy of ViewsUIController

1 file declares its use of ViewsUIController
TagTest.php in core/modules/views_ui/src/Tests/TagTest.php
Contains \Drupal\views_ui\Tests\TagTest.

File

core/modules/views_ui/src/Controller/ViewsUIController.php, line 26
Contains \Drupal\views_ui\Controller\ViewsUIController.

Namespace

Drupal\views_ui\Controller
View source
class ViewsUIController extends ControllerBase {

  /**
   * Stores the Views data cache object.
   *
   * @var \Drupal\views\ViewsData
   */
  protected $viewsData;

  /**
   * Constructs a new \Drupal\views_ui\Controller\ViewsUIController object.
   *
   * @param \Drupal\views\ViewsData views_data
   *   The Views data cache object.
   */
  public function __construct(ViewsData $views_data) {
    $this->viewsData = $views_data;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('views.views_data'));
  }

  /**
   * Lists all instances of fields on any views.
   *
   * @return array
   *   The Views fields report page.
   */
  public function reportFields() {
    $views = $this
      ->entityManager()
      ->getStorage('view')
      ->loadMultiple();

    // Fetch all fieldapi fields which are used in views
    // Therefore search in all views, displays and handler-types.
    $fields = array();
    $handler_types = ViewExecutable::getHandlerTypes();
    foreach ($views as $view) {
      $executable = $view
        ->getExecutable();
      $executable
        ->initDisplay();
      foreach ($executable->displayHandlers as $display_id => $display) {
        if ($executable
          ->setDisplay($display_id)) {
          foreach ($handler_types as $type => $info) {
            foreach ($executable
              ->getHandlers($type, $display_id) as $item) {
              $table_data = $this->viewsData
                ->get($item['table']);
              if (isset($table_data[$item['field']]) && isset($table_data[$item['field']][$type]) && ($field_data = $table_data[$item['field']][$type])) {

                // The final check that we have a fieldapi field now.
                if (isset($field_data['field_name'])) {
                  $fields[$field_data['field_name']][$view
                    ->id()] = $view
                    ->id();
                }
              }
            }
          }
        }
      }
    }
    $header = array(
      t('Field name'),
      t('Used in'),
    );
    $rows = array();
    foreach ($fields as $field_name => $views) {
      $rows[$field_name]['data'][0]['data']['#plain_text'] = $field_name;
      foreach ($views as $view) {
        $rows[$field_name]['data'][1][] = $this
          ->l($view, new Url('entity.view.edit_form', array(
          'view' => $view,
        )));
      }
      $item_list = [
        '#theme' => 'item_list',
        '#items' => $rows[$field_name]['data'][1],
        '#context' => [
          'list_style' => 'comma-list',
        ],
      ];
      $rows[$field_name]['data'][1] = [
        'data' => $item_list,
      ];
    }

    // Sort rows by field name.
    ksort($rows);
    $output = array(
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => t('No fields have been used in views yet.'),
    );
    return $output;
  }

  /**
   * Lists all plugins and what enabled Views use them.
   *
   * @return array
   *   The Views plugins report page.
   */
  public function reportPlugins() {
    $rows = Views::pluginList();
    foreach ($rows as &$row) {
      $views = [];

      // Link each view name to the view itself.
      foreach ($row['views'] as $row_name => $view) {
        $views[] = $this
          ->l($view, new Url('entity.view.edit_form', array(
          'view' => $view,
        )));
      }
      unset($row['views']);
      $row['views']['data'] = [
        '#theme' => 'item_list',
        '#items' => $views,
        '#context' => [
          'list_style' => 'comma-list',
        ],
      ];
    }

    // Sort rows by field name.
    ksort($rows);
    return array(
      '#type' => 'table',
      '#header' => array(
        t('Type'),
        t('Name'),
        t('Provided by'),
        t('Used in'),
      ),
      '#rows' => $rows,
      '#empty' => t('There are no enabled views.'),
    );
  }

  /**
   * Calls a method on a view and reloads the listing page.
   *
   * @param \Drupal\views\ViewEntityInterface $view
   *   The view being acted upon.
   * @param string $op
   *   The operation to perform, e.g., 'enable' or 'disable'.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
   *   Either returns a rebuilt listing page as an AJAX response, or redirects
   *   back to the listing page.
   *
   */
  public function ajaxOperation(ViewEntityInterface $view, $op, Request $request) {

    // Perform the operation.
    $view
      ->{$op}()
      ->save();

    // If the request is via AJAX, return the rendered list as JSON.
    if ($request->request
      ->get('js')) {
      $list = $this
        ->entityManager()
        ->getListBuilder('view')
        ->render();
      $response = new AjaxResponse();
      $response
        ->addCommand(new ReplaceCommand('#views-entity-list', $list));
      return $response;
    }

    // Otherwise, redirect back to the page.
    return $this
      ->redirect('entity.view.collection');
  }

  /**
   * Menu callback for Views tag autocompletion.
   *
   * Like other autocomplete functions, this function inspects the 'q' query
   * parameter for the string to use to search for suggestions.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   A JSON response containing the autocomplete suggestions for Views tags.
   */
  public function autocompleteTag(Request $request) {
    $matches = array();
    $string = $request->query
      ->get('q');

    // Get matches from default views.
    $views = $this
      ->entityManager()
      ->getStorage('view')
      ->loadMultiple();
    foreach ($views as $view) {
      $tag = $view
        ->get('tag');
      if ($tag && strpos($tag, $string) === 0) {
        $matches[$tag] = $tag;
        if (count($matches) >= 10) {
          break;
        }
      }
    }
    return new JsonResponse($matches);
  }

  /**
   * Returns the form to edit a view.
   *
   * @param \Drupal\views_ui\ViewUI $view
   *   The view to be edited.
   * @param string|null $display_id
   *   (optional) The display ID being edited. Defaults to NULL, which will load
   *   the first available display.
   *
   * @return array
   *   An array containing the Views edit and preview forms.
   */
  public function edit(ViewUI $view, $display_id = NULL) {
    $name = $view
      ->label();
    $data = $this->viewsData
      ->get($view
      ->get('base_table'));
    if (isset($data['table']['base']['title'])) {
      $name .= ' (' . $data['table']['base']['title'] . ')';
    }
    $build['#title'] = $name;
    $build['edit'] = $this
      ->entityFormBuilder()
      ->getForm($view, 'edit', array(
      'display_id' => $display_id,
    ));
    $build['preview'] = $this
      ->entityFormBuilder()
      ->getForm($view, 'preview', array(
      'display_id' => $display_id,
    ));
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager. 1
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 3
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service. 1
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 3
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator protected function Returns the link generator.
LinkGeneratorTrait::l protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator public function Sets the link generator service.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service.
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator protected function Returns the URL generator service.
UrlGeneratorTrait::redirect protected function Returns a redirect response object for the specified route.
UrlGeneratorTrait::setUrlGenerator public function Sets the URL generator service.
UrlGeneratorTrait::url protected function Generates a URL or path for a specific route based on the given parameters.
ViewsUIController::$viewsData protected property Stores the Views data cache object.
ViewsUIController::ajaxOperation public function Calls a method on a view and reloads the listing page.
ViewsUIController::autocompleteTag public function Menu callback for Views tag autocompletion.
ViewsUIController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
ViewsUIController::edit public function Returns the form to edit a view.
ViewsUIController::reportFields public function Lists all instances of fields on any views.
ViewsUIController::reportPlugins public function Lists all plugins and what enabled Views use them.
ViewsUIController::__construct public function Constructs a new \Drupal\views_ui\Controller\ViewsUIController object.