ViewStorage.php 10.3 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

damiankloip's avatar
damiankloip committed
17
  /**
18
   * Overrides Drupal\entity\EntityInterface::id().
damiankloip's avatar
damiankloip committed
19 20 21 22 23
   */
  public function id() {
    return $this->name;
  }

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  /**
   * Implements Drupal\views\ViewStorageInterface::enable().
   */
  public function enable() {
    $this->disabled = FALSE;
  }

  /**
   * Implements Drupal\views\ViewStorageInterface::disable().
   */
  public function disable() {
    $this->disabled = TRUE;
  }

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

45
  /**
46
   * Adds a new display handler to the view, automatically creating an ID.
47
   *
48 49 50 51 52 53 54 55 56 57 58 59
   * @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.
60
   */
61
  public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
62
    if (empty($plugin_id)) {
63 64 65
      return FALSE;
    }

66
    $plugin = views_get_plugin_definition('display', $plugin_id);
67 68 69 70 71
    if (empty($plugin)) {
      $plugin['title'] = t('Broken');
    }

    if (empty($id)) {
72
      $id = $this->generateDisplayId($plugin_id);
73

74 75
      // Generate a unique human-readable name by inspecting the counter at the
      // end of the previous display ID, e.g., 'page_1'.
76 77 78 79 80 81 82 83 84
      if ($id !== 'default') {
        preg_match("/[0-9]+/", $id, $count);
        $count = $count[0];
      }
      else {
        $count = '';
      }

      if (empty($title)) {
85 86 87
        // If there is no title provided, use the plugin title, and if there are
        // multiple displays, append the count.
        $title = $plugin['title'];
88
        if ($count > 1) {
89
          $title .= ' ' . $count;
90 91 92 93
        }
      }
    }

94
    $display_options = array(
95
      'display_plugin' => $plugin_id,
96 97 98 99
      'id' => $id,
      'display_title' => $title,
    );

100
    // Create the new display object
101
    $display = new ViewDisplay($display_options);
102 103 104 105 106 107 108

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

  /**
109
   * Generates a display ID of a certain plugin type.
110
   *
111 112
   * @param string $plugin_id
   *   Which plugin should be used for the new display ID.
113
   */
114
  protected function generateDisplayId($plugin_id) {
115 116
    // 'default' is singular and is unique, so just go with 'default'
    // for it. For all others, start counting.
117
    if ($plugin_id == 'default') {
118 119
      return 'default';
    }
120 121
    // Initial ID.
    $id = $plugin_id . '_1';
122 123 124 125 126
    $count = 1;

    // Loop through IDs based upon our style plugin name until
    // we find one that is unused.
    while (!empty($this->display[$id])) {
127
      $id = $plugin_id . '_' . ++$count;
128 129 130 131 132 133
    }

    return $id;
  }

  /**
134
   * Generates a unique ID for an handler instance.
135
   *
136 137
   * These handler instances are typically fields, filters, sort criteria, or
   * arguments.
138
   *
139 140 141 142
   * @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.
143
   *
144 145 146 147 148
   * @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.
149
   */
150
  public static function generateItemId($requested_id, $existing_items) {
151 152 153 154 155 156 157 158 159
    $count = 0;
    $id = $requested_id;
    while (!empty($existing_items[$id])) {
      $id = $requested_id . '_' . ++$count;
    }
    return $id;
  }

  /**
160 161 162 163 164 165 166 167 168 169 170 171
   * 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
172 173
   *   A reference to the new handler object.
   */
174 175
  public function &newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    $id = $this->addDisplay($plugin_id, $title, $id);
176

177
    // Create a handler.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    $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;
  }

  /**
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
   * 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.
215
   *
216 217
   * @return string
   *   The unique ID for this handler instance.
218
   */
219
  public function addItem($display_id, $type, $table, $field, $options = array(), $id = NULL) {
220 221
    $types = View::viewsObjectTypes();
    $this->setDisplay($display_id);
222

223
    $fields = $this->display[$display_id]->handler->getOption($types[$type]['plural']);
224 225

    if (empty($id)) {
226
      $id = $this->generateItemId($field, $fields);
227 228
    }

229 230 231 232 233 234 235
    // 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(
236 237 238 239 240
      'id' => $id,
      'table' => $table,
      'field' => $field,
    ) + $options;

241
    $this->display[$display_id]->handler->setOption($types[$type]['plural'], $fields);
242 243 244 245 246

    return $id;
  }

  /**
247 248 249 250 251 252 253 254 255 256
   * 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.
257
   */
258
  public function getItems($type, $display_id = NULL) {
259
    $this->setDisplay($display_id);
260 261 262 263 264 265

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

    // Get info about the types so we can get the right data.
266
    $types = View::viewsObjectTypes();
267
    return $this->display[$display_id]->handler->getOption($types[$type]['plural']);
268 269 270
  }

  /**
271 272 273 274 275 276 277 278 279 280 281 282
   * 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.
283
   */
284
  public function getItem($display_id, $type, $id) {
285
    // Get info about the types so we can get the right data.
286
    $types = View::viewsObjectTypes();
287
    // Initialize the display
288
    $this->setDisplay($display_id);
289 290

    // Get the existing configuration
291
    $fields = $this->display[$display_id]->handler->getOption($types[$type]['plural']);
292 293 294 295 296

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

  /**
297 298 299 300 301 302 303 304 305 306
   * 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.
307
   *
308
   * @see set_item_option()
309
   */
310
  public function setItem($display_id, $type, $id, $item) {
311
    // Get info about the types so we can get the right data.
312
    $types = View::viewsObjectTypes();
313
    // Initialize the display.
314
    $this->setDisplay($display_id);
315

316
    // Get the existing configuration.
317
    $fields = $this->display[$display_id]->handler->getOption($types[$type]['plural']);
318 319 320 321 322 323 324 325
    if (isset($item)) {
      $fields[$id] = $item;
    }
    else {
      unset($fields[$id]);
    }

    // Store.
326
    $this->display[$display_id]->handler->setOption($types[$type]['plural'], $fields);
327 328 329
  }

  /**
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
   * 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.
346
   *
347
   * @see set_item()
348
   */
349 350
  public function setItemOption($display_id, $type, $id, $option, $value) {
    $item = $this->getItem($display_id, $type, $id);
351
    $item[$option] = $value;
352
    $this->setItem($display_id, $type, $id, $item);
353
  }
354

355
}