ViewListController.php 7.37 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\views_ui\ViewListController.
6 7
 */

8
namespace Drupal\views_ui;
9

10
use Drupal\Component\Utility\String;
11
use Drupal\Component\Plugin\PluginManagerInterface;
12
use Drupal\Core\Entity\EntityInterface;
13
use Drupal\Core\Config\Entity\ConfigEntityListController;
14 15
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
16
use Drupal\Core\Extension\ModuleHandlerInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18 19 20 21

/**
 * Provides a listing of Views.
 */
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
class ViewListController extends ConfigEntityListController implements EntityControllerInterface {

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

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    return new static(
      $entity_type,
      $container->get('plugin.manager.entity')->getStorageController($entity_type),
      $entity_info,
39 40
      $container->get('plugin.manager.views.display'),
      $container->get('module_handler')
41 42 43 44 45 46 47 48 49 50 51 52 53 54
    );
  }

  /**
   * Constructs a new EntityListController object.
   *
   * @param string $entity_type.
   *   The type of entity to be listed.
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage.
   *   The entity storage controller class.
   * @param array $entity_info
   *   An array of entity info for this entity type.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $display_manager
   *   The views display plugin manager to use.
55 56
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
57
   */
58 59 60
  public function __construct($entity_type, EntityStorageControllerInterface $storage, $entity_info, PluginManagerInterface $display_manager, ModuleHandlerInterface $module_handler) {
    parent::__construct($entity_type, $entity_info, $storage, $module_handler);

61 62
    $this->displayManager = $display_manager;
  }
63 64

  /**
65
   * {@inheritdoc}
66
   */
67
  public function load() {
68 69 70 71 72
    $entities = array(
      'enabled' => array(),
      'disabled' => array(),
    );
    foreach (parent::load() as $entity) {
73
      if ($entity->status()) {
74 75 76 77
        $entities['enabled'][] = $entity;
      }
      else {
        $entities['disabled'][] = $entity;
78
      }
79
    }
80
    return $entities;
81 82 83
  }

  /**
84
   * {@inheritdoc}
85
   */
86
  public function buildRow(EntityInterface $view) {
87
    $row = parent::buildRow($view);
88 89
    return array(
      'data' => array(
90 91 92 93 94 95 96
        'view_name' => array(
          'data' => array(
            '#theme' => 'views_ui_view_info',
            '#view' => $view,
            '#displays' => $this->getDisplaysList($view)
          ),
        ),
97 98
        'description' => $view->get('description'),
        'tag' => $view->get('tag'),
99
        'path' => implode(', ', $this->getDisplayPaths($view)),
100
        'operations' => $row['operations'],
101
      ),
102
      'title' => t('Machine name: @name', array('@name' => $view->id())),
103
      'class' => array($view->status() ? 'views-ui-list-enabled' : 'views-ui-list-disabled'),
104 105 106 107
    );
  }

  /**
108
   * {@inheritdoc}
109
   */
110
  public function buildHeader() {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    return array(
      'view_name' => array(
        'data' => t('View name'),
        'class' => array('views-ui-name'),
      ),
      'description' => array(
        'data' => t('Description'),
        'class' => array('views-ui-description'),
      ),
      'tag' => array(
        'data' => t('Tag'),
        'class' => array('views-ui-tag'),
      ),
      'path' => array(
        'data' => t('Path'),
        'class' => array('views-ui-path'),
      ),
128
      'operations' => array(
129 130 131 132 133 134 135
        'data' => t('Operations'),
        'class' => array('views-ui-operations'),
      ),
    );
  }

  /**
136
   * {@inheritdoc}
137
   */
138 139 140
  public function getOperations(EntityInterface $entity) {
    $operations = parent::getOperations($entity);
    $uri = $entity->uri();
141

142
    $operations['clone'] = array(
143
      'title' => t('Clone'),
144 145
      'href' => $uri['path'] . '/clone',
      'options' => $uri['options'],
146 147 148
      'weight' => 15,
    );

149 150
    // Add AJAX functionality to enable/disable operations.
    foreach (array('enable', 'disable') as $op) {
151 152
      if (isset($operations[$op])) {
        $operations[$op]['ajax'] = TRUE;
153
        $operations[$op]['query']['token'] = drupal_get_token($op);
154 155 156
      }
    }

157
    return $operations;
158 159 160
  }

  /**
161
   * {@inheritdoc}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
   */
  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';
      }
    }

    return $build;
  }

  /**
177
   * {@inheritdoc}
178
   */
179
  public function render() {
180 181
    $entities = $this->load();
    $list['#type'] = 'container';
182
    $list['#attached']['css'] = ViewFormControllerBase::getAdminCSS();
183 184
    $list['#attached']['library'][] = array('system', 'drupal.ajax');
    $list['#attributes']['id'] = 'views-entity-list';
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    $list['enabled']['heading']['#markup'] = '<h2>' . t('Enabled') . '</h2>';
    $list['disabled']['heading']['#markup'] = '<h2>' . t('Disabled') . '</h2>';
    foreach (array('enabled', 'disabled') as $status) {
      $list[$status]['#type'] = 'container';
      $list[$status]['#attributes'] = array('class' => array('views-list-section', $status));
      $list[$status]['table'] = array(
        '#theme' => 'table',
        '#header' => $this->buildHeader(),
        '#rows' => array(),
      );
      foreach ($entities[$status] as $entity) {
        $list[$status]['table']['#rows'][$entity->id()] = $this->buildRow($entity);
      }
    }
    // @todo Use a placeholder for the entity label if this is abstracted to
    // other entity types.
    $list['enabled']['table']['#empty'] = t('There are no enabled views.');
    $list['disabled']['table']['#empty'] = t('There are no disabled views.');

204 205 206
    return $list;
  }

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  /**
   * 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 = array();
    foreach ($view->get('display') as $display) {
      $definition = $this->displayManager->getDefinition($display['display_plugin']);
      if (!empty($definition['admin'])) {
        $displays[$definition['admin']] = TRUE;
      }
    }

    ksort($displays);
    return array_keys($displays);
  }

229 230 231 232 233 234 235 236 237 238 239
  /**
   * Gets a list of paths assigned to the view.
   *
   * @param \Drupal\Core\Entity\EntityInterface $view
   *   The view entity.
   *
   * @return array
   *   An array of paths for this view.
   */
  protected function getDisplayPaths(EntityInterface $view) {
    $all_paths = array();
240
    $executable = $view->getExecutable();
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    $executable->initDisplay();
    foreach ($executable->displayHandlers as $display) {
      if ($display->hasPath()) {
        $path = $display->getPath();
        if ($view->status() && strpos($path, '%') === FALSE) {
          $all_paths[] = l('/' . $path, $path);
        }
        else {
          $all_paths[] = String::checkPlain('/' . $path);
        }
      }
    }
    return array_unique($all_paths);
  }

256
}