ViewStorage.php 13.7 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 97 98 99 100 101 102 103 104 105 106 107 108 109
  /**
   * 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;

  /**
   * @todo
   */
  public $executable;

  public function setExecutable($executable) {
    $this->executable = $executable;
  }

  function __call($name, $arguments) {
    if (isset($executable) && method_exists($this->executable, $name)) {
      return call_user_func_array(array($this->executable, $name), $arguments);
    }
  }
110

damiankloip's avatar
damiankloip committed
111
  /**
112
   * Overrides Drupal\Core\Entity\EntityInterface::id().
damiankloip's avatar
damiankloip committed
113 114 115 116 117
   */
  public function id() {
    return $this->name;
  }

118 119 120 121 122
  /**
   * Implements Drupal\views\ViewStorageInterface::enable().
   */
  public function enable() {
    $this->disabled = FALSE;
123
    $this->save();
124 125 126 127 128 129 130
  }

  /**
   * Implements Drupal\views\ViewStorageInterface::disable().
   */
  public function disable() {
    $this->disabled = TRUE;
131
    $this->save();
132 133 134 135 136 137 138 139 140
  }

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

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  /**
   * 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;
  }

156
  /**
157
   * Adds a new display handler to the view, automatically creating an ID.
158
   *
159 160 161 162 163 164 165 166 167 168 169 170
   * @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.
171
   */
172
  public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
173
    if (empty($plugin_id)) {
174 175 176
      return FALSE;
    }

177
    $plugin = views_get_plugin_definition('display', $plugin_id);
178 179 180 181 182
    if (empty($plugin)) {
      $plugin['title'] = t('Broken');
    }

    if (empty($id)) {
183
      $id = $this->generateDisplayId($plugin_id);
184

185 186
      // Generate a unique human-readable name by inspecting the counter at the
      // end of the previous display ID, e.g., 'page_1'.
187 188 189 190 191 192 193 194 195
      if ($id !== 'default') {
        preg_match("/[0-9]+/", $id, $count);
        $count = $count[0];
      }
      else {
        $count = '';
      }

      if (empty($title)) {
196 197 198
        // If there is no title provided, use the plugin title, and if there are
        // multiple displays, append the count.
        $title = $plugin['title'];
199
        if ($count > 1) {
200
          $title .= ' ' . $count;
201 202 203 204
        }
      }
    }

205
    $display_options = array(
206
      'display_plugin' => $plugin_id,
207 208 209 210
      'id' => $id,
      'display_title' => $title,
    );

211
    // Create the new display object
212
    $display = new ViewDisplay($display_options);
213 214 215 216 217 218 219

    // Add the new display object to the view.
    $this->display[$id] = $display;
    return $id;
  }

  /**
220
   * Generates a display ID of a certain plugin type.
221
   *
222 223
   * @param string $plugin_id
   *   Which plugin should be used for the new display ID.
224
   */
225
  protected function generateDisplayId($plugin_id) {
226 227
    // 'default' is singular and is unique, so just go with 'default'
    // for it. For all others, start counting.
228
    if ($plugin_id == 'default') {
229 230
      return 'default';
    }
231 232
    // Initial ID.
    $id = $plugin_id . '_1';
233 234 235 236 237
    $count = 1;

    // Loop through IDs based upon our style plugin name until
    // we find one that is unused.
    while (!empty($this->display[$id])) {
238
      $id = $plugin_id . '_' . ++$count;
239 240 241 242 243 244
    }

    return $id;
  }

  /**
245
   * Generates a unique ID for an handler instance.
246
   *
247 248
   * These handler instances are typically fields, filters, sort criteria, or
   * arguments.
249
   *
250 251 252 253
   * @param string $requested_id
   *   The requested ID for the handler instance.
   * @param array $existing_items
   *   An array of existing handler instancess, keyed by their IDs.
254
   *
255 256 257 258 259
   * @return string
   *   A unique ID. This will be equal to $requested_id if no handler instance
   *   with that ID already exists. Otherwise, it will be appended with an
   *   integer to make it unique, e.g., "{$requested_id}_1",
   *   "{$requested_id}_2", etc.
260
   */
261
  public static function generateItemId($requested_id, $existing_items) {
262 263 264 265 266 267 268 269 270
    $count = 0;
    $id = $requested_id;
    while (!empty($existing_items[$id])) {
      $id = $requested_id . '_' . ++$count;
    }
    return $id;
  }

  /**
271 272 273 274 275 276 277 278 279 280 281 282
   * 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
283 284
   *   A reference to the new handler object.
   */
285 286
  public function &newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    $id = $this->addDisplay($plugin_id, $title, $id);
287

288
    // Create a handler.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
    if (empty($this->display[$id]->handler)) {
      // provide a 'default' handler as an emergency. This won't work well but
      // it will keep things from crashing.
      $this->display[$id]->handler = views_get_plugin('display', 'default');
    }

    if (!empty($this->display[$id]->handler)) {
      // Initialize the new display handler with data.
      $this->display[$id]->handler->init($this, $this->display[$id]);
      // If this is NOT the default display handler, let it know which is
      if ($id != 'default') {
        $this->display[$id]->handler->default_display = &$this->display['default']->handler;
      }
    }

    return $this->display[$id]->handler;
  }

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  /**
   * Gets a list of displays included in the view.
   *
   * @return array
   *   An array of display types that this view includes.
   */
  function getDisplaysList() {
    $this->initDisplay();

    $displays = array();
    foreach ($this->display as $display) {
      if (!empty($display->handler->definition['admin'])) {
        $displays[$display->handler->definition['admin']] = TRUE;
      }
    }

    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 {
      $this->initDisplay();   // Make sure all the handlers are set up
      foreach ($this->display as $display) {
        if (!empty($display->handler) && $display->handler->hasPath()) {
          $path = $display->handler->getOption('path');
          if ($this->isEnabled() && strpos($path, '%') === FALSE) {
            $all_paths[] = l('/' . $path, $path);
          }
          else {
            $all_paths[] = check_plain('/' . $path);
          }
        }
      }
    }

    return array_unique($all_paths);
  }

359
  /**
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
   * Adds an instance of a handler to the view.
   *
   * Items may be fields, filters, sort criteria, or arguments.
   *
   * @param string $display_id
   *   The machine name of the display.
   * @param string $type
   *   The type of handler being added.
   * @param string $table
   *   The name of the table this handler is from.
   * @param string $field
   *   The name of the field this handler is from.
   * @param array $options
   *   (optional) Extra options for this instance. Defaults to an empty array.
   * @param string $id
   *   (optional) A unique ID for this handler instance. Defaults to NULL, in
   *   which case one will be generated.
377
   *
378 379
   * @return string
   *   The unique ID for this handler instance.
380
   */
381
  public function addItem($display_id, $type, $table, $field, $options = array(), $id = NULL) {
382
    $types = ViewExecutable::viewsHandlerTypes();
383
    $this->setDisplay($display_id);
384

385
    $fields = $this->display[$display_id]->handler->getOption($types[$type]['plural']);
386 387

    if (empty($id)) {
388
      $id = $this->generateItemId($field, $fields);
389 390
    }

391 392 393 394 395 396 397
    // If the desired type is not found, use the original value directly.
    $handler_type = !empty($types[$type]['type']) ? $types[$type]['type'] : $type;

    // @todo This variable is never used.
    $handler = views_get_handler($table, $field, $handler_type);

    $fields[$id] = array(
398 399 400 401 402
      'id' => $id,
      'table' => $table,
      'field' => $field,
    ) + $options;

403
    $this->display[$display_id]->handler->setOption($types[$type]['plural'], $fields);
404 405 406 407 408

    return $id;
  }

  /**
409 410 411 412 413 414 415 416 417 418
   * Gets an array of handler instances for the current display.
   *
   * @param string $type
   *   The type of handlers to retrieve.
   * @param string $display_id
   *   (optional) A specific display machine name to use. If NULL, the current
   *   display will be used.
   *
   * @return array
   *   An array of handler instances of a given type for this display.
419
   */
420
  public function getItems($type, $display_id = NULL) {
421
    $this->setDisplay($display_id);
422 423 424 425 426 427

    if (!isset($display_id)) {
      $display_id = $this->current_display;
    }

    // Get info about the types so we can get the right data.
428
    $types = ViewExecutable::viewsHandlerTypes();
429
    return $this->display[$display_id]->handler->getOption($types[$type]['plural']);
430 431 432
  }

  /**
433 434 435 436 437 438 439 440 441 442 443 444
   * Gets the configuration of a handler instance on a given display.
   *
   * @param string $display_id
   *   The machine name of the display.
   * @param string $type
   *   The type of handler to retrieve.
   * @param string $id
   *   The ID of the handler to retrieve.
   *
   * @return array|null
   *   Either the handler instance's configuration, or NULL if the handler is
   *   not used on the display.
445
   */
446
  public function getItem($display_id, $type, $id) {
447
    // Get info about the types so we can get the right data.
448
    $types = ViewExecutable::viewsHandlerTypes();
449
    // Initialize the display
450
    $this->setDisplay($display_id);
451 452

    // Get the existing configuration
453
    $fields = $this->display[$display_id]->handler->getOption($types[$type]['plural']);
454 455 456 457 458

    return isset($fields[$id]) ? $fields[$id] : NULL;
  }

  /**
459 460 461 462 463 464 465 466 467 468
   * Sets the configuration of a handler instance on a given display.
   *
   * @param string $display_id
   *   The machine name of the display.
   * @param string $type
   *   The type of handler being set.
   * @param string $id
   *   The ID of the handler being set.
   * @param array|null $item
   *   An array of configuration for a handler, or NULL to remove this instance.
469
   *
470
   * @see set_item_option()
471
   */
472
  public function setItem($display_id, $type, $id, $item) {
473
    // Get info about the types so we can get the right data.
474
    $types = ViewExecutable::viewsHandlerTypes();
475
    // Initialize the display.
476
    $this->setDisplay($display_id);
477

478
    // Get the existing configuration.
479
    $fields = $this->display[$display_id]->handler->getOption($types[$type]['plural']);
480 481 482 483 484 485 486 487
    if (isset($item)) {
      $fields[$id] = $item;
    }
    else {
      unset($fields[$id]);
    }

    // Store.
488
    $this->display[$display_id]->handler->setOption($types[$type]['plural'], $fields);
489 490 491
  }

  /**
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
   * Sets an option on a handler instance.
   *
   * Use this only if you have just 1 or 2 options to set; if you have many,
   * consider getting the handler instance, adding the options and using
   * set_item() directly.
   *
   * @param string $display_id
   *   The machine name of the display.
   * @param string $type
   *   The type of handler being set.
   * @param string $id
   *   The ID of the handler being set.
   * @param string $option
   *   The configuration key for the value being set.
   * @param mixed $value
   *   The value being set.
508
   *
509
   * @see set_item()
510
   */
511 512
  public function setItemOption($display_id, $type, $id, $option, $value) {
    $item = $this->getItem($display_id, $type, $id);
513
    $item[$option] = $value;
514
    $this->setItem($display_id, $type, $id, $item);
515
  }
516

517
}