ViewStorage.php 9.03 KB
Newer Older
1
<?php
2 3 4

/**
 * @file
5
 * Definition of Drupal\views\ViewStorage.
6 7 8 9
 */

namespace Drupal\views;

10
use Drupal\config\ConfigEntityBase;
11

12
/**
13
 * Defines a ViewStorage configuration entity class.
14
 */
15
class ViewStorage extends ConfigEntityBase implements ViewStorageInterface {
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  /**
   * The name of the base table this view will use.
   *
   * @var string
   */
  public $base_table = 'node';

  /**
   * The name of the view.
   *
   * @var string
   */
  public $name = '';

  /**
   * The description of the view, which is used only in the interface.
   *
   * @var string
   */
  public $description = '';

  /**
   * The "tags" of a view.
   *
   * The tags are stored as a single string, though it is used as multiple tags
   * for example in the views overview.
   *
   * @var string
   */
  public $tag = '';

  /**
   * The human readable name of the view.
   *
   * @var string
   */
  public $human_name = '';

  /**
   * The core version the view was created for.
   *
   * @var int
   */
  public $core = DRUPAL_CORE_COMPATIBILITY;

  /**
   * The views API version this view was created by.
   *
   * @var string
   */
  public $api_version = VIEWS_API_VERSION;

  /**
   * Stores all display handlers of this view.
   *
   * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase
   * objects.
   *
   * @var array
   */
  public $display;

  /**
   * The name of the base field to use.
   *
   * @var string
   */
  public $base_field = 'nid';

  /**
   * Returns whether the view's status is disabled or not.
   *
   * This value is used for exported view, to provide some default views which
   * aren't enabled.
   *
   * @var bool
   */
  public $disabled = FALSE;

  /**
97
   * The UUID for this entity.
98
   *
99
   * @var string
100
   */
101
  public $uuid = NULL;
102

103
  /**
104
   * Stores a reference to the executable version of this view.
105
   *
106
   * @var Drupal\views\ViewExecutable
107
   */
108
  protected $executable;
109

110 111 112 113 114 115 116
  /**
   * The module implementing this view.
   *
   * @var string
   */
  public $module = 'views';

117 118 119 120 121 122 123
  /**
   * Stores the executable version of this view.
   *
   * @param Drupal\views\ViewExecutable $executable
   *   The executable version of this view.
   */
  public function setExecutable(ViewExecutable $executable) {
124 125 126
    $this->executable = $executable;
  }

127
  /**
128
   * Retrieves the executable version of this view.
129
   *
130 131 132
   * @param bool $reset
   *   Get a new Drupal\views\ViewExecutable instance.
   *
133 134
   * @return Drupal\views\ViewExecutable
   *   The executable version of this view.
135
   */
136 137
  public function getExecutable($reset = FALSE) {
    if (!isset($this->executable) || $reset) {
138 139
      $this->setExecutable(new ViewExecutable($this));
    }
140
    return $this->executable;
141 142 143
  }

  /**
144
   * Initializes the display.
145
   *
146
   * @todo Inspect calls to this and attempt to clean up.
147
   *
148 149
   * @param bool $reset
   *   If the display should be reset. Defaults to FALSE.
150
   *
151
   * @see Drupal\views\ViewExecutable::initDisplay()
152
   */
153
  public function initDisplay($reset = FALSE) {
154
    $this->getExecutable()->initDisplay($reset);
155 156
  }

157 158 159 160 161 162 163
  /**
   * Returns the name of the module implementing this view.
   */
  public function getModule() {
    return $this->module;
  }

164 165 166 167 168 169 170 171 172 173
  /**
   * Overrides Drupal\Core\Entity\EntityInterface::uri().
   */
  public function uri() {
    $info = $this->entityInfo();
    return array(
      'path' => $info['list path'],
    );
  }

damiankloip's avatar
damiankloip committed
174
  /**
175
   * Overrides Drupal\Core\Entity\EntityInterface::id().
damiankloip's avatar
damiankloip committed
176 177 178 179 180
   */
  public function id() {
    return $this->name;
  }

181 182 183 184 185
  /**
   * Implements Drupal\views\ViewStorageInterface::enable().
   */
  public function enable() {
    $this->disabled = FALSE;
186
    $this->save();
187 188 189 190 191 192 193
  }

  /**
   * Implements Drupal\views\ViewStorageInterface::disable().
   */
  public function disable() {
    $this->disabled = TRUE;
194
    $this->save();
195 196 197 198 199 200 201 202 203
  }

  /**
   * Implements Drupal\views\ViewStorageInterface::isEnabled().
   */
  public function isEnabled() {
    return !$this->disabled;
  }

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  /**
   * Return the human readable name for a view.
   *
   * When a certain view doesn't have a human readable name return the machine readable name.
   */
  public function getHumanName() {
    if (!empty($this->human_name)) {
      $human_name = $this->human_name;
    }
    else {
      $human_name = $this->name;
    }
    return $human_name;
  }

219 220 221 222 223 224 225 226 227 228 229 230 231
  /**
   * Perform automatic updates when loading or importing a view.
   *
   * Over time, some things about Views or Drupal data has changed.
   * this attempts to do some automatic updates that must happen
   * to ensure older views will at least try to work.
   */
  public function update() {
    // When views are converted automatically the base_table should be renamed
    // to have a working query.
    $this->base_table = views_move_table($this->base_table);
  }

232
  /**
233
   * Adds a new display handler to the view, automatically creating an ID.
234
   *
235 236 237 238 239 240 241 242 243 244 245 246
   * @param string $plugin_id
   *   (optional) The plugin type from the Views plugin annotation. Defaults to
   *   'page'.
   * @param string $title
   *   (optional) The title of the display. Defaults to NULL.
   * @param string $id
   *   (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults
   *   to NULL.
   *
   * @return string|false
   *   The key to the display in $view->display, or FALSE if no plugin ID was
   *   provided.
247
   */
248
  public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
249
    if (empty($plugin_id)) {
250 251 252
      return FALSE;
    }

253
    $plugin = views_get_plugin_definition('display', $plugin_id);
254 255 256 257 258
    if (empty($plugin)) {
      $plugin['title'] = t('Broken');
    }

    if (empty($id)) {
259
      $id = $this->generateDisplayId($plugin_id);
260

261 262
      // Generate a unique human-readable name by inspecting the counter at the
      // end of the previous display ID, e.g., 'page_1'.
263 264 265 266 267 268 269 270 271
      if ($id !== 'default') {
        preg_match("/[0-9]+/", $id, $count);
        $count = $count[0];
      }
      else {
        $count = '';
      }

      if (empty($title)) {
272 273 274
        // If there is no title provided, use the plugin title, and if there are
        // multiple displays, append the count.
        $title = $plugin['title'];
275
        if ($count > 1) {
276
          $title .= ' ' . $count;
277 278 279 280
        }
      }
    }

281
    $display_options = array(
282
      'display_plugin' => $plugin_id,
283 284
      'id' => $id,
      'display_title' => $title,
285 286
      'position' => NULL,
      'display_options' => array(),
287 288
    );

289 290
    // Add the display options to the view.
    $this->display[$id] = $display_options;
291 292 293 294
    return $id;
  }

  /**
295
   * Generates a display ID of a certain plugin type.
296
   *
297 298
   * @param string $plugin_id
   *   Which plugin should be used for the new display ID.
299
   */
300
  protected function generateDisplayId($plugin_id) {
301 302
    // 'default' is singular and is unique, so just go with 'default'
    // for it. For all others, start counting.
303
    if ($plugin_id == 'default') {
304 305
      return 'default';
    }
306 307
    // Initial ID.
    $id = $plugin_id . '_1';
308 309 310 311 312
    $count = 1;

    // Loop through IDs based upon our style plugin name until
    // we find one that is unused.
    while (!empty($this->display[$id])) {
313
      $id = $plugin_id . '_' . ++$count;
314 315 316 317 318 319
    }

    return $id;
  }

  /**
320 321 322 323 324 325 326 327 328 329 330 331
   * Creates a new display and a display handler for it.
   *
   * @param string $plugin_id
   *   (optional) The plugin type from the Views plugin annotation. Defaults to
   *   'page'.
   * @param string $title
   *   (optional) The title of the display. Defaults to NULL.
   * @param string $id
   *   (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults
   *   to NULL.
   *
   * @return Drupal\views\Plugin\views\display\DisplayPluginBase
332 333
   *   A reference to the new handler object.
   */
334 335
  public function &newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    $id = $this->addDisplay($plugin_id, $title, $id);
336
    return $this->getExecutable()->newDisplay($id);
337 338
  }

339 340 341 342 343 344 345
  /**
   * Gets a list of displays included in the view.
   *
   * @return array
   *   An array of display types that this view includes.
   */
  function getDisplaysList() {
346
    $manager = drupal_container()->get('plugin.manager.views.display');
347 348
    $displays = array();
    foreach ($this->display as $display) {
349 350 351
      $definition = $manager->getDefinition($display['display_plugin']);
      if (!empty($definition['admin'])) {
        $displays[$definition['admin']] = TRUE;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
      }
    }

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

  /**
   * Gets a list of paths assigned to the view.
   *
   * @return array
   *   An array of paths for this view.
   */
  public function getPaths() {
    $all_paths = array();
    if (empty($this->display)) {
      $all_paths[] = t('Edit this view to add a display.');
    }
    else {
      foreach ($this->display as $display) {
372 373
        if (!empty($display['display_options']['path'])) {
          $path = $display['display_options']['path'];
374 375 376 377 378 379 380 381 382 383 384 385 386
          if ($this->isEnabled() && strpos($path, '%') === FALSE) {
            $all_paths[] = l('/' . $path, $path);
          }
          else {
            $all_paths[] = check_plain('/' . $path);
          }
        }
      }
    }

    return array_unique($all_paths);
  }

387
}