class ViewListBuilder in Drupal 9
Same name and namespace in other branches
- 8 core/modules/views_ui/src/ViewListBuilder.php \Drupal\views_ui\ViewListBuilder
Defines a class to build a listing of view entities.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder
- class \Drupal\views_ui\ViewListBuilder
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
Expanded class hierarchy of ViewListBuilder
See also
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_uiView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 5 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 5 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityListBuilder:: |
protected | property | Information about the entity type. | |
EntityListBuilder:: |
protected | property | The entity type ID. | |
EntityListBuilder:: |
protected | property | The number of entities to list per page, or FALSE to list all entities. | |
EntityListBuilder:: |
protected | property | The entity storage class. | 1 |
EntityListBuilder:: |
public | function | Builds a renderable list of operation links for the entity. | 2 |
EntityListBuilder:: |
protected | function | Ensures that a destination is present on the given URL. | |
EntityListBuilder:: |
protected | function | Loads entity IDs using a pager sorted by the entity id. | 4 |
EntityListBuilder:: |
public | function |
Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface:: |
2 |
EntityListBuilder:: |
public | function |
Gets the entity storage. Overrides EntityListBuilderInterface:: |
|
EntityListBuilder:: |
protected | function | Gets the title of the page. | 1 |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ViewListBuilder:: |
protected | property | The views display plugin manager to use. | |
ViewListBuilder:: |
public | function |
Builds the header row for the entity listing. Overrides EntityListBuilder:: |
|
ViewListBuilder:: |
public | function |
Builds a row for an entity in the entity listing. Overrides EntityListBuilder:: |
|
ViewListBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityListBuilder:: |
|
ViewListBuilder:: |
public | function |
Gets this list's default operations. Overrides ConfigEntityListBuilder:: |
|
ViewListBuilder:: |
protected | function | Gets a list of displays included in the view. | |
ViewListBuilder:: |
public | function |
Loads entities of this type from storage for listing. Overrides ConfigEntityListBuilder:: |
|
ViewListBuilder:: |
public | function |
Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder:: |
|
ViewListBuilder:: |
public | function |
Constructs a new ViewListBuilder object. Overrides EntityListBuilder:: |