View.php 9 KB
Newer Older
1
<?php
2 3 4

/**
 * @file
5
 * Definition of Drupal\views\Plugin\Core\Entity\View.
6 7
 */

8
namespace Drupal\views\Plugin\Core\Entity;
9

10
use Drupal\Core\Config\Entity\ConfigEntityBase;
11
use Drupal\views_ui\ViewUI;
12 13 14 15
use Drupal\views\ViewStorageInterface;
use Drupal\views\ViewExecutable;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
16

17
/**
18 19 20 21 22 23 24 25
 * Defines a View configuration entity class.
 *
 * @Plugin(
 *   id = "view",
 *   label = @Translation("View"),
 *   module = "views",
 *   controller_class = "Drupal\views\ViewStorageController",
 *   list_controller_class = "Drupal\views_ui\ViewListController",
26 27 28
 *   form_controller_class = {
 *     "edit" = "Drupal\views_ui\ViewEditFormController",
 *     "add" = "Drupal\views_ui\ViewAddFormController",
29 30
 *     "preview" = "Drupal\views_ui\ViewPreviewFormController",
 *     "clone" = "Drupal\views_ui\ViewCloneFormController"
31
 *   },
32 33 34 35 36 37 38 39
 *   config_prefix = "views.view",
 *   fieldable = FALSE,
 *   entity_keys = {
 *     "id" = "name",
 *     "label" = "human_name",
 *     "uuid" = "uuid"
 *   }
 * )
40
 */
41
class View extends ConfigEntityBase implements ViewStorageInterface {
42

43 44 45 46 47
  /**
   * The name of the base table this view will use.
   *
   * @var string
   */
48
  protected $base_table = 'node';
49 50 51 52 53 54 55 56 57 58 59 60 61

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

  /**
   * The description of the view, which is used only in the interface.
   *
   * @var string
   */
62
  protected $description = '';
63 64 65 66 67 68 69 70 71

  /**
   * 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
   */
72
  protected $tag = '';
73 74 75 76 77 78 79 80 81 82 83 84 85

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

  /**
   * The core version the view was created for.
   *
   * @var int
   */
86
  protected $core = DRUPAL_CORE_COMPATIBILITY;
87 88 89 90 91 92

  /**
   * The views API version this view was created by.
   *
   * @var string
   */
93
  protected $api_version = VIEWS_API_VERSION;
94 95 96 97 98 99 100 101 102

  /**
   * Stores all display handlers of this view.
   *
   * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase
   * objects.
   *
   * @var array
   */
103
  protected $display;
104 105 106 107 108 109

  /**
   * The name of the base field to use.
   *
   * @var string
   */
110
  protected $base_field = 'nid';
111 112 113 114 115 116 117 118 119

  /**
   * 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
   */
120
  protected $disabled = FALSE;
121 122

  /**
123
   * The UUID for this entity.
124
   *
125
   * @var string
126
   */
127
  public $uuid = NULL;
128

129
  /**
130
   * Stores a reference to the executable version of this view.
131
   *
132
   * @var Drupal\views\ViewExecutable
133
   */
134
  protected $executable;
135

136 137 138 139 140
  /**
   * The module implementing this view.
   *
   * @var string
   */
141
  protected $module = 'views';
142

143
  /**
144
   * Overrides Drupal\Core\Entity\EntityInterface::get().
145
   */
146 147 148 149
  public function get($property_name, $langcode = NULL) {
    // Ensure that an executable View is available.
    if ($property_name == 'executable' && !isset($this->{$property_name})) {
      $this->set('executable', new ViewExecutable($this));
150 151
    }

152
    return parent::get($property_name, $langcode);
153 154
  }

155 156 157 158 159
  /**
   * Overrides Drupal\Core\Entity\EntityInterface::uri().
   */
  public function uri() {
    return array(
160
      'path' => 'admin/structure/views/view/' . $this->id(),
161 162 163
    );
  }

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

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
  /**
   * 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() {
200 201
    if (!$human_name = $this->get('human_name')) {
      $human_name = $this->get('name');
202 203 204 205
    }
    return $human_name;
  }

206
  /**
207
   * Adds a new display handler to the view, automatically creating an ID.
208
   *
209 210 211 212 213 214 215 216 217 218 219 220
   * @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.
221
   */
222
  public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
223
    if (empty($plugin_id)) {
224 225 226
      return FALSE;
    }

227
    $plugin = drupal_container()->get('plugin.manager.views.display')->getDefinition($plugin_id);
228 229 230 231 232
    if (empty($plugin)) {
      $plugin['title'] = t('Broken');
    }

    if (empty($id)) {
233
      $id = $this->generateDisplayId($plugin_id);
234

235 236
      // Generate a unique human-readable name by inspecting the counter at the
      // end of the previous display ID, e.g., 'page_1'.
237 238 239 240 241 242 243 244 245
      if ($id !== 'default') {
        preg_match("/[0-9]+/", $id, $count);
        $count = $count[0];
      }
      else {
        $count = '';
      }

      if (empty($title)) {
246 247 248
        // If there is no title provided, use the plugin title, and if there are
        // multiple displays, append the count.
        $title = $plugin['title'];
249
        if ($count > 1) {
250
          $title .= ' ' . $count;
251 252 253 254
        }
      }
    }

255
    $display_options = array(
256
      'display_plugin' => $plugin_id,
257 258
      'id' => $id,
      'display_title' => $title,
259 260
      'position' => NULL,
      'display_options' => array(),
261 262
    );

263 264
    // Add the display options to the view.
    $this->display[$id] = $display_options;
265 266 267 268
    return $id;
  }

  /**
269
   * Generates a display ID of a certain plugin type.
270
   *
271 272
   * @param string $plugin_id
   *   Which plugin should be used for the new display ID.
273
   */
274
  protected function generateDisplayId($plugin_id) {
275 276
    // 'default' is singular and is unique, so just go with 'default'
    // for it. For all others, start counting.
277
    if ($plugin_id == 'default') {
278 279
      return 'default';
    }
280 281
    // Initial ID.
    $id = $plugin_id . '_1';
282 283 284 285 286
    $count = 1;

    // Loop through IDs based upon our style plugin name until
    // we find one that is unused.
    while (!empty($this->display[$id])) {
287
      $id = $plugin_id . '_' . ++$count;
288 289 290 291 292 293
    }

    return $id;
  }

  /**
294 295 296 297 298 299 300 301 302 303 304 305
   * 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
306 307
   *   A reference to the new handler object.
   */
308 309
  public function &newDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    $id = $this->addDisplay($plugin_id, $title, $id);
310
    return $this->get('executable')->newDisplay($id);
311 312
  }

313 314 315 316 317 318 319 320 321 322 323 324 325
  /**
   * Retrieves a specific display's configuration by reference.
   *
   * @param string $display_id
   *   The display ID to retrieve, e.g., 'default', 'page_1', 'block_2'.
   *
   * @return array
   *   A reference to the specified display configuration.
   */
  public function &getDisplay($display_id) {
    return $this->display[$display_id];
  }

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

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

    return array_unique($all_paths);
  }

374
}