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

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

8
namespace Drupal\views_ui;
9

10
use Drupal\Core\Entity\EntityListController;
11
use Drupal\Core\Entity\EntityInterface;
12
13
14
15

/**
 * Provides a listing of Views.
 */
16
class ViewListController extends EntityListController {
17
18

  /**
19
   * Overrides Drupal\Core\Entity\EntityListController::load();
20
   */
21
  public function load() {
22
23
24
25
26
27
28
29
30
31
    $entities = array(
      'enabled' => array(),
      'disabled' => array(),
    );
    foreach (parent::load() as $entity) {
      if ($entity->isEnabled()) {
        $entities['enabled'][] = $entity;
      }
      else {
        $entities['disabled'][] = $entity;
32
      }
33
    }
34
    return $entities;
35
36
37
  }

  /**
38
   * Overrides Drupal\Core\Entity\EntityListController::buildRow();
39
   */
40
  public function buildRow(EntityInterface $view) {
41
42
43
    return array(
      'data' => array(
        'view_name' => theme('views_ui_view_info', array('view' => $view)),
44
45
        'description' => $view->get('description'),
        'tag' => $view->get('tag'),
46
        'path' => implode(', ', $view->getPaths()),
47
48
49
        'operations' => array(
          'data' => $this->buildOperations($view),
        ),
50
51
52
53
54
55
56
      ),
      'title' => t('Machine name: ') . $view->id(),
      'class' => array($view->isEnabled() ? 'views-ui-list-enabled' : 'views-ui-list-disabled'),
    );
  }

  /**
57
   * Overrides Drupal\Core\Entity\EntityListController::buildHeader();
58
   */
59
  public function buildHeader() {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    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'),
      ),
77
      'operations' => array(
78
79
80
81
82
83
84
        'data' => t('Operations'),
        'class' => array('views-ui-operations'),
      ),
    );
  }

  /**
85
   * Implements Drupal\Core\Entity\EntityListController::getOperations();
86
   */
87
88
  public function getOperations(EntityInterface $view) {
    $uri = $view->uri();
89
    $path = $uri['path'];
90

91
92
93
94
95
96
    $definition['edit'] = array(
      'title' => t('Edit'),
      'href' => "$path/edit",
      'weight' => -5,
    );
    if (!$view->isEnabled()) {
97
98
99
100
101
      $definition['enable'] = array(
        'title' => t('Enable'),
        'ajax' => TRUE,
        'token' => TRUE,
        'href' => "$path/enable",
102
        'weight' => -10,
103
104
      );
    }
105
    else {
106
107
108
109
110
      $definition['disable'] = array(
        'title' => t('Disable'),
        'ajax' => TRUE,
        'token' => TRUE,
        'href' => "$path/disable",
111
        'weight' => 0,
112
113
114
115
116
117
118
      );
    }
    // This property doesn't exist yet.
    if (!empty($view->overridden)) {
      $definition['revert'] = array(
        'title' => t('Revert'),
        'href' => "$path/revert",
119
        'weight' => 5,
120
121
122
123
124
125
      );
    }
    else {
      $definition['delete'] = array(
        'title' => t('Delete'),
        'href' => "$path/delete",
126
        'weight' => 10,
127
128
      );
    }
129
130
131
132
133
134
    $definition['clone'] = array(
      'title' => t('Clone'),
      'href' => "$path/clone",
      'weight' => 15,
    );

135
136
137
138
    return $definition;
  }

  /**
139
140
141
142
143
144
145
146
147
148
149
150
   * 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';
      }
    }

151
152
153
    // Use the dropbutton #type.
    unset($build['#theme']);
    $build['#type'] = 'dropbutton';
154
155
156
157
158
159

    return $build;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityListController::render();
160
   */
161
  public function render() {
162
163
    $entities = $this->load();
    $list['#type'] = 'container';
164
    $list['#attached']['css'] = ViewFormControllerBase::getAdminCSS();
165
166
    $list['#attached']['library'][] = array('system', 'drupal.ajax');
    $list['#attributes']['id'] = 'views-entity-list';
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    $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.');

186
187
188
189
    return $list;
  }

}