ViewStorage.php 8.87 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 98 99
   * Stores a reference to the executable version of this view.
   *
   * @var Drupal\views\ViewExecutable
100 101 102
   */
  public $executable;

103 104 105 106 107 108 109 110 111
  /**
   * A copy of the original entity.
   *
   * @todo This should be moved to Drupal\Core\Entity\Entity.
   *
   * @var Drupal\Core\Entity\EntityInterface
   */
  public $original;

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

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

129
  /**
130
   * Retrieves the executable version of this view.
131
   *
132 133
   * @return Drupal\views\ViewExecutable
   *   The executable version of this view.
134
   */
135
  public function getExecutable() {
136 137 138
    if (!isset($this->executable)) {
      $this->setExecutable(new ViewExecutable($this));
    }
139
    return $this->executable;
140 141 142
  }

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

damiankloip's avatar
damiankloip committed
164
  /**
165
   * Overrides Drupal\Core\Entity\EntityInterface::id().
damiankloip's avatar
damiankloip committed
166 167 168 169 170
   */
  public function id() {
    return $this->name;
  }

171 172 173 174 175
  /**
   * Implements Drupal\views\ViewStorageInterface::enable().
   */
  public function enable() {
    $this->disabled = FALSE;
176
    $this->save();
177 178 179 180 181 182 183
  }

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

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

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  /**
   * 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;
  }

209 210 211 212 213 214 215 216 217 218 219 220 221
  /**
   * 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);
  }

222
  /**
223
   * Adds a new display handler to the view, automatically creating an ID.
224
   *
225 226 227 228 229 230 231 232 233 234 235 236
   * @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.
237
   */
238
  public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
239
    if (empty($plugin_id)) {
240 241 242
      return FALSE;
    }

243
    $plugin = views_get_plugin_definition('display', $plugin_id);
244 245 246 247 248
    if (empty($plugin)) {
      $plugin['title'] = t('Broken');
    }

    if (empty($id)) {
249
      $id = $this->generateDisplayId($plugin_id);
250

251 252
      // Generate a unique human-readable name by inspecting the counter at the
      // end of the previous display ID, e.g., 'page_1'.
253 254 255 256 257 258 259 260 261
      if ($id !== 'default') {
        preg_match("/[0-9]+/", $id, $count);
        $count = $count[0];
      }
      else {
        $count = '';
      }

      if (empty($title)) {
262 263 264
        // If there is no title provided, use the plugin title, and if there are
        // multiple displays, append the count.
        $title = $plugin['title'];
265
        if ($count > 1) {
266
          $title .= ' ' . $count;
267 268 269 270
        }
      }
    }

271
    $display_options = array(
272
      'display_plugin' => $plugin_id,
273 274
      'id' => $id,
      'display_title' => $title,
275 276
      'position' => NULL,
      'display_options' => array(),
277 278
    );

279 280
    // Add the display options to the view.
    $this->display[$id] = $display_options;
281 282 283 284
    return $id;
  }

  /**
285
   * Generates a display ID of a certain plugin type.
286
   *
287 288
   * @param string $plugin_id
   *   Which plugin should be used for the new display ID.
289
   */
290
  protected function generateDisplayId($plugin_id) {
291 292
    // 'default' is singular and is unique, so just go with 'default'
    // for it. For all others, start counting.
293
    if ($plugin_id == 'default') {
294 295
      return 'default';
    }
296 297
    // Initial ID.
    $id = $plugin_id . '_1';
298 299 300 301 302
    $count = 1;

    // Loop through IDs based upon our style plugin name until
    // we find one that is unused.
    while (!empty($this->display[$id])) {
303
      $id = $plugin_id . '_' . ++$count;
304 305 306 307 308 309
    }

    return $id;
  }

  /**
310 311 312 313 314 315 316 317 318 319 320 321
   * 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
322 323
   *   A reference to the new handler object.
   */
324 325
  public function &newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    $id = $this->addDisplay($plugin_id, $title, $id);
326 327
    $this->getExecutable();
    return $this->executable->newDisplay($id);
328 329
  }

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

    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) {
363 364
        if (!empty($display['display_options']['path'])) {
          $path = $display['display_options']['path'];
365 366 367 368 369 370 371 372 373 374 375 376 377
          if ($this->isEnabled() && strpos($path, '%') === FALSE) {
            $all_paths[] = l('/' . $path, $path);
          }
          else {
            $all_paths[] = check_plain('/' . $path);
          }
        }
      }
    }

    return array_unique($all_paths);
  }

378
}