View source
<?php
namespace Drupal\views_ui;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
class ViewListBuilder extends ConfigEntityListBuilder {
protected $displayManager;
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'));
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, PluginManagerInterface $display_manager) {
parent::__construct($entity_type, $storage);
$this->displayManager = $display_manager;
$this->limit = FALSE;
}
public function load() {
$entities = [
'enabled' => [],
'disabled' => [],
];
foreach (parent::load() as $entity) {
if ($entity
->status()) {
$entities['enabled'][] = $entity;
}
else {
$entities['disabled'][] = $entity;
}
}
return $entities;
}
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',
],
],
];
}
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',
],
],
],
];
}
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
$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'),
];
}
foreach ([
'enable',
'disable',
] as $op) {
if (isset($operations[$op])) {
$operations[$op]['url'] = $entity
->toUrl($op);
$operations[$op]['attributes']['class'][] = 'use-ajax';
}
}
foreach ($operations as &$operation) {
$operation['attributes']['data-drupal-selector'] = 'views-listing-' . $entity
->id();
}
return $operations;
}
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;
}
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) {
try {
$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;
}
}