ViewListController.php 3.98 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
22
23
  public function load() {
    $entities = parent::load();
    uasort($entities, function ($a, $b) {
24
25
26
27
28
29
30
      $a_enabled = $a->isEnabled();
      $b_enabled = $b->isEnabled();
      if ($a_enabled != $b_enabled) {
        return $a_enabled < $b_enabled;
      }
      return $a->id() > $b->id();
    });
31
    return $entities;
32
33
34
  }

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

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

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

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

  /**
130
131
132
133
134
135
136
137
138
139
140
141
   * 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';
      }
    }

142
143
144
    // Use the dropbutton #type.
    unset($build['#theme']);
    $build['#type'] = 'dropbutton';
145
146
147
148
149
150

    return $build;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityListController::render();
151
   */
152
153
  public function render() {
    $list = parent::render();
154
    $list['#attached']['css'] = ViewUI::getAdminCSS();
155
156
    $list['#attached']['library'][] = array('system', 'drupal.ajax');
    $list['#attributes']['id'] = 'views-entity-list';
157
158
159
160
    return $list;
  }

}