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
  /**
   * Implements Drupal\views\ViewStorageInterface::enable().
   */
  public function enable() {
    $this->disabled = FALSE;
29
    $this->save();
30 31 32 33 34 35 36
  }

  /**
   * Implements Drupal\views\ViewStorageInterface::disable().
   */
  public function disable() {
    $this->disabled = TRUE;
37
    $this->save();
38 39 40 41 42 43 44 45 46
  }

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

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

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

    if (empty($id)) {
74
      $id = $this->generateDisplayId($plugin_id);
75

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

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

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

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

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

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

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

    return $id;
  }

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

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

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

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

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

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

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

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

    return $id;
  }

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

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

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

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

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

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

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

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

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

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

357
}