Commit d27e8b5b authored by tim.plunkett's avatar tim.plunkett

Issue #1760284 by tim.plunkett, damiankloip: Rewrite ViewListController to use...

Issue #1760284 by tim.plunkett, damiankloip: Rewrite ViewListController to use the core EntityListController.
parent 12f9f87c
......@@ -7,7 +7,7 @@
namespace Drupal\views;
use Drupal\views_ui_listing\EntityListController;
use Drupal\Core\Entity\EntityListController;
use Drupal\Core\Entity\EntityInterface;
/**
......@@ -16,7 +16,7 @@
class ViewListController extends EntityListController {
/**
* Overrides Drupal\views_ui_listing\EntityListController::load();
* Overrides Drupal\Core\Entity\EntityListController::load();
*/
public function load() {
$entities = parent::load();
......@@ -32,18 +32,18 @@ public function load() {
}
/**
* Overrides Drupal\views_ui_listing\EntityListController::buildRow();
* Overrides Drupal\Core\Entity\EntityListController::buildRow();
*/
public function buildRow(EntityInterface $view) {
$operations = $this->buildOperations($view);
$operations['#theme'] = 'links__ctools_dropbutton';
return array(
'data' => array(
'view_name' => theme('views_ui_view_info', array('view' => $view)),
'description' => $view->description,
'tag' => $view->tag,
'path' => implode(', ', $view->getPaths()),
'operations' => drupal_render($operations),
'operations' => array(
'data' => $this->buildOperations($view),
),
),
'title' => t('Machine name: ') . $view->id(),
'class' => array($view->isEnabled() ? 'views-ui-list-enabled' : 'views-ui-list-disabled'),
......@@ -51,7 +51,7 @@ public function buildRow(EntityInterface $view) {
}
/**
* Overrides Drupal\views_ui_listing\EntityListController::buildHeader();
* Overrides Drupal\Core\Entity\EntityListController::buildHeader();
*/
public function buildHeader() {
return array(
......@@ -79,7 +79,7 @@ public function buildHeader() {
}
/**
* Implements Drupal\views_ui_listing\EntityListController::getOperations();
* Implements Drupal\Core\Entity\EntityListController::getOperations();
*/
public function getOperations(EntityInterface $view) {
$uri = $view->uri();
......@@ -127,11 +127,32 @@ public function getOperations(EntityInterface $view) {
}
/**
* Overrides Drupal\views_ui_listing\EntityListController::render();
* Overrides Drupal\Core\Entity\EntityListController::buildOperations();
*/
public function buildOperations(EntityInterface $entity) {
$build = parent::buildOperations($entity);
// Allow operations to specify that they use AJAX.
foreach ($build['#links'] as &$operation) {
if (!empty($operation['ajax'])) {
$operation['attributes']['class'][] = 'use-ajax';
}
}
// Use theme_links__ctools_dropbutton().
$build['#theme'] = 'links__ctools_dropbutton';
return $build;
}
/**
* Overrides Drupal\Core\Entity\EntityListController::render();
*/
public function render() {
$list = parent::render();
$list['#attached']['css'] = ViewUI::getAdminCSS();
$list['#attached']['library'][] = array('system', 'drupal.ajax');
$list['#attributes']['id'] = 'views-entity-list';
return $list;
}
......
......@@ -4,4 +4,3 @@ package = Views
core = 8.x
configure = admin/structure/views
dependencies[] = views
dependencies[] = views_ui_listing
......@@ -9,6 +9,7 @@
use Drupal\views\ViewUI;
use Drupal\views\Analyzer;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Implements hook_menu().
......@@ -801,17 +802,19 @@ function views_ui_load($name) {
* redirect back to the listing page.
*/
function views_ui_ajax_callback(ViewExecutable $view, $op) {
$controller = views_ui_entity_list_controller('view');
// Perform the operation.
$view->storage->$op();
// If the request is via AJAX, return the rendered list as JSON.
if (drupal_container()->get('request')->request->get('js')) {
return $controller->renderListAJAX();
$list = entity_list_controller('view')->render();
$commands = array(ajax_command_replace('#views-entity-list', drupal_render($list)));
return new JsonResponse(ajax_render($commands));
}
// Otherwise, redirect back to the page.
else {
drupal_goto($controller->getPath());
$entity_info = entity_get_info('view');
drupal_goto($entity_info['list path']);
}
}
......@@ -824,6 +827,5 @@ function views_ui_ajax_callback(ViewExecutable $view, $op) {
* @see views_ui_menu()
*/
function views_ui_list_page() {
$controller = views_ui_entity_list_controller('view');
return $controller->render();
return entity_list_controller('view')->render();
}
<?php
/**
* @file
* Definition of Drupal\views_ui_listing\EntityListController.
*/
namespace Drupal\views_ui_listing;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a generic implementation of an entity list controller.
*/
class EntityListController implements EntityListControllerInterface {
/**
* The entity storage controller class.
*
* @var Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $storage;
/**
* The entity type name.
*
* @var string
*/
protected $entityType;
/**
* The entity info array.
*
* @var array
*
* @see entity_get_info()
*/
protected $entityInfo;
/**
* Constructs a new EntityListController object.
*
* @param string $entity_type.
* The type of entity to be listed.
*/
public function __construct($entity_type) {
$this->entityType = $entity_type;
$this->storage = entity_get_controller($this->entityType);
$this->entityInfo = entity_get_info($this->entityType);
}
/**
* Implements Drupal\views_ui_listing\EntityListControllerInterface::getStorageController().
*/
public function getStorageController() {
return $this->storage;
}
/**
* Implements Drupal\views_ui_listing\EntityListControllerInterface::load().
*/
public function load() {
return $this->storage->load();
}
/**
* Implements Drupal\views_ui_listing\EntityListControllerInterface::getOperations().
*/
public function getOperations(EntityInterface $entity) {
$uri = $entity->uri();
$operations['edit'] = array(
'title' => t('Edit'),
'href' => $uri['path'] . '/edit',
'options' => $uri['options'],
'weight' => 10,
);
$operations['delete'] = array(
'title' => t('Delete'),
'href' => $uri['path'] . '/delete',
'options' => $uri['options'],
'weight' => 100,
);
return $operations;
}
/**
* Retrieves the entity list path from the entity information.
*
* @return string
* The internal system path where the entity list will be rendered.
*
* @todo What is this method for, other than fetching the list path? Is this
* for http://drupal.org/node/1783964 ? Should it be on the interface?
*/
public function getPath() {
return $this->entityInfo['list path'];
}
/**
* Implements Drupal\views_ui_listing\EntityListControllerInterface::buildHeader().
*/
public function buildHeader() {
$row['label'] = t('Label');
$row['id'] = t('Machine name');
$row['operations'] = t('Operations');
return $row;
}
/**
* Implements Drupal\views_ui_listing\EntityListControllerInterface::buildRow().
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['id'] = $entity->id();
$operations = $this->buildOperations($entity);
$row['operations'] = drupal_render($operations);
return $row;
}
/**
* Implements Drupal\views_ui_listing\EntityListControllerInterface::buildOperations().
*/
public function buildOperations(EntityInterface $entity) {
// Retrieve and sort operations.
$operations = $this->getOperations($entity);
uasort($operations, 'drupal_sort_weight');
$build = array(
'#theme' => 'links',
'#links' => $operations,
);
return $build;
}
/**
* Implements Drupal\views_ui_listing\EntityListControllerInterface::render().
*/
public function render() {
$build = array(
'#theme' => 'table',
'#header' => $this->buildHeader(),
'#rows' => array(),
'#empty' => t('There is no @label yet. <a href="@add-url">Add one</a>.', array(
'@label' => $this->entityInfo['label'],
'@add-url' => url($this->getPath() . '/add'),
)),
);
foreach ($this->load() as $entity) {
$build['#rows'][$entity->id()] = $this->buildRow($entity);
}
return $build;
}
}
<?php
/**
* @file
* Definition of Drupal\views_ui_listing\EntityListControllerInterface.
*/
namespace Drupal\views_ui_listing;
use Drupal\Core\Entity\EntityInterface;
/**
* Defines an interface for entity list controllers.
*/
interface EntityListControllerInterface {
/**
* Gets the entity storage controller.
*
* @return Drupal\Core\Entity\EntityStorageControllerInterface
* The storage controller used by this list controller.
*/
public function getStorageController();
/**
* Loads entities of this type from storage for listing.
*
* @return array
* An array of entities implementing Drupal\Core\Entity\EntityInterface.
*/
public function load();
/**
* Provides an array of information to render the operation links.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity the operations are for.
*
* @return array
* A array of operation link data to use in
* EntityListControllerInterface::buildOperations().
*/
public function getOperations(EntityInterface $entity);
/**
* Builds the header row.
*
* @return array
* An array of header strings.
*/
public function buildHeader();
/**
* Builds an array of data for each row.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity for this row of the list.
*
* @return array
* An array of fields to use for this entity.
*/
public function buildRow(EntityInterface $entity);
/**
* Renders a list of operation links.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity on which the linked operations will be performed.
*
* @return array
* A renderable array of operation links.
*/
public function buildOperations(EntityInterface $entity);
/**
* Renders the list page markup to be output.
*
* @return string
* The output markup for the listing page.
*/
public function render();
}
name = Views UI Listing
description = temporary replacement for export UI.
package = Views
core = 8.x
<?php
/**
* Returns an entity list controller for a given entity type.
*
* @param string $entity_type
* The type of the entity.
*
* @return Drupal\Core\Entity\EntityListControllerInterface
* An entity list controller.
*
* @see hook_entity_info()
*/
function views_ui_entity_list_controller($entity_type) {
$controllers = &drupal_static(__FUNCTION__, array());
if (!isset($instances[$entity_type])) {
$info = entity_get_info($entity_type);
$class = $info['list controller class'];
$controllers[$entity_type] = new $class($entity_type);
}
return $controllers[$entity_type];
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment