You are here

class ViewListBuilder in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views_ui/src/ViewListBuilder.php \Drupal\views_ui\ViewListBuilder
  2. 9 core/modules/views_ui/src/ViewListBuilder.php \Drupal\views_ui\ViewListBuilder

Defines a class to build a listing of view entities.

Hierarchy

Expanded class hierarchy of ViewListBuilder

See also

\Drupal\views\Entity\View

1 file declares its use of ViewListBuilder
ViewListBuilderTest.php in core/modules/views_ui/tests/src/Unit/ViewListBuilderTest.php
Contains \Drupal\Tests\views_ui\Unit\ViewListBuilderTest.

File

core/modules/views_ui/src/ViewListBuilder.php, line 20

Namespace

Drupal\views_ui
View source
class ViewListBuilder extends ConfigEntityListBuilder {

  /**
   * The views display plugin manager to use.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $displayManager;

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('plugin.manager.views.display'));
  }

  /**
   * Constructs a new ViewListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $display_manager
   *   The views display plugin manager to use.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, PluginManagerInterface $display_manager) {
    parent::__construct($entity_type, $storage);
    $this->displayManager = $display_manager;

    // This list builder uses client-side filters which requires all entities to
    // be listed, disable the pager.
    // @todo https://www.drupal.org/node/2536826 change the filtering to support
    //   a pager.
    $this->limit = FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    $entities = [
      'enabled' => [],
      'disabled' => [],
    ];
    foreach (parent::load() as $entity) {
      if ($entity
        ->status()) {
        $entities['enabled'][] = $entity;
      }
      else {
        $entities['disabled'][] = $entity;
      }
    }
    return $entities;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $view) {
    $row = parent::buildRow($view);
    return [
      'data' => [
        'view_name' => [
          'data' => [
            '#plain_text' => $view
              ->label(),
          ],
        ],
        'machine_name' => [
          'data' => [
            '#plain_text' => $view
              ->id(),
          ],
        ],
        'description' => [
          'data' => [
            '#plain_text' => $view
              ->get('description'),
          ],
        ],
        'displays' => [
          'data' => [
            '#theme' => 'views_ui_view_displays_list',
            '#displays' => $this
              ->getDisplaysList($view),
          ],
        ],
        'operations' => $row['operations'],
      ],
      '#attributes' => [
        'class' => [
          $view
            ->status() ? 'views-ui-list-enabled' : 'views-ui-list-disabled',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    return [
      'view_name' => [
        'data' => $this
          ->t('View name'),
        '#attributes' => [
          'class' => [
            'views-ui-name',
          ],
        ],
      ],
      'machine_name' => [
        'data' => $this
          ->t('Machine name'),
        '#attributes' => [
          'class' => [
            'views-ui-machine-name',
          ],
        ],
      ],
      'description' => [
        'data' => $this
          ->t('Description'),
        '#attributes' => [
          'class' => [
            'views-ui-description',
          ],
        ],
      ],
      'displays' => [
        'data' => $this
          ->t('Displays'),
        '#attributes' => [
          'class' => [
            'views-ui-displays',
          ],
        ],
      ],
      'operations' => [
        'data' => $this
          ->t('Operations'),
        '#attributes' => [
          'class' => [
            'views-ui-operations',
          ],
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {
    $operations = parent::getDefaultOperations($entity);

    // Remove destination redirect for Edit operation.
    $operations['edit']['url'] = $entity
      ->toUrl('edit-form');
    if ($entity
      ->hasLinkTemplate('duplicate-form')) {
      $operations['duplicate'] = [
        'title' => $this
          ->t('Duplicate'),
        'weight' => 15,
        'url' => $entity
          ->toUrl('duplicate-form'),
      ];
    }

    // Add AJAX functionality to enable/disable operations.
    foreach ([
      'enable',
      'disable',
    ] as $op) {
      if (isset($operations[$op])) {
        $operations[$op]['url'] = $entity
          ->toUrl($op);

        // Enable and disable operations should use AJAX.
        $operations[$op]['attributes']['class'][] = 'use-ajax';
      }
    }

    // ajax.js focuses automatically on the data-drupal-selector element. When
    // enabling the view again, focusing on the disable link doesn't work, as it
    // is hidden. We assign data-drupal-selector to every link, so it focuses
    // on the edit link.
    foreach ($operations as &$operation) {
      $operation['attributes']['data-drupal-selector'] = 'views-listing-' . $entity
        ->id();
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $entities = $this
      ->load();
    $list['#type'] = 'container';
    $list['#attributes']['id'] = 'views-entity-list';
    $list['#attached']['library'][] = 'core/drupal.ajax';
    $list['#attached']['library'][] = 'views_ui/views_ui.listing';
    $list['filters'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'table-filter',
          'js-show',
        ],
      ],
    ];
    $list['filters']['text'] = [
      '#type' => 'search',
      '#title' => $this
        ->t('Filter'),
      '#title_display' => 'invisible',
      '#size' => 60,
      '#placeholder' => $this
        ->t('Filter by view name, machine name, description, or display path'),
      '#attributes' => [
        'class' => [
          'views-filter-text',
        ],
        'data-table' => '.views-listing-table',
        'autocomplete' => 'off',
        'title' => $this
          ->t('Enter a part of the view name, machine name, description, or display path to filter by.'),
      ],
    ];
    $list['enabled']['heading']['#markup'] = '<h2>' . $this
      ->t('Enabled', [], [
      'context' => 'Plural',
    ]) . '</h2>';
    $list['disabled']['heading']['#markup'] = '<h2>' . $this
      ->t('Disabled', [], [
      'context' => 'Plural',
    ]) . '</h2>';
    foreach ([
      'enabled',
      'disabled',
    ] as $status) {
      $list[$status]['#type'] = 'container';
      $list[$status]['#attributes'] = [
        'class' => [
          'views-list-section',
          $status,
        ],
      ];
      $list[$status]['table'] = [
        '#theme' => 'views_ui_views_listing_table',
        '#headers' => $this
          ->buildHeader(),
        '#attributes' => [
          'class' => [
            'views-listing-table',
            $status,
          ],
        ],
      ];
      foreach ($entities[$status] as $entity) {
        $list[$status]['table']['#rows'][$entity
          ->id()] = $this
          ->buildRow($entity);
      }
    }
    $list['enabled']['table']['#empty'] = $this
      ->t('There are no enabled views.');
    $list['disabled']['table']['#empty'] = $this
      ->t('There are no disabled views.');
    return $list;
  }

  /**
   * Gets a list of displays included in the view.
   *
   * @param \Drupal\Core\Entity\EntityInterface $view
   *   The view entity instance to get a list of displays for.
   *
   * @return array
   *   An array of display types that this view includes.
   */
  protected function getDisplaysList(EntityInterface $view) {
    $displays = [];
    $executable = $view
      ->getExecutable();
    $executable
      ->initDisplay();
    foreach ($executable->displayHandlers as $display) {
      $rendered_path = FALSE;
      $definition = $display
        ->getPluginDefinition();
      if (!empty($definition['admin'])) {
        if ($display
          ->hasPath()) {
          $path = $display
            ->getPath();
          if ($view
            ->status() && strpos($path, '%') === FALSE) {

            // Wrap this in a try/catch as trying to generate links to some
            // routes may throw a NotAcceptableHttpException if they do not
            // respond to HTML, such as RESTExports.
            try {

              // @todo Views should expect and store a leading /. See:
              //   https://www.drupal.org/node/2423913
              $rendered_path = Link::fromTextAndUrl('/' . $path, Url::fromUserInput('/' . $path))
                ->toString();
            } catch (NotAcceptableHttpException $e) {
              $rendered_path = '/' . $path;
            }
          }
          else {
            $rendered_path = '/' . $path;
          }
        }
        $displays[] = [
          'display' => $definition['admin'],
          'path' => $rendered_path,
        ];
      }
    }
    sort($displays);
    return $displays;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 5
EntityHandlerBase::moduleHandler protected function Gets the module handler. 5
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities.
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity.
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL. 1
EntityListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. 4
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
MessengerTrait::$messenger protected property The messenger. 18
MessengerTrait::messenger public function Gets the messenger. 18
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
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. 3
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. 1
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
ViewListBuilder::$displayManager protected property The views display plugin manager to use.
ViewListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
ViewListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
ViewListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
ViewListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides ConfigEntityListBuilder::getDefaultOperations
ViewListBuilder::getDisplaysList protected function Gets a list of displays included in the view.
ViewListBuilder::load public function Loads entities of this type from storage for listing. Overrides ConfigEntityListBuilder::load
ViewListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
ViewListBuilder::__construct public function Constructs a new ViewListBuilder object. Overrides EntityListBuilder::__construct