EntityType.php 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\EntityType.
 */

namespace Drupal\Core\Entity;

10
use Drupal\Component\Utility\String;
11
use Drupal\Component\Utility\Unicode;
12
use Drupal\Core\Entity\Exception\EntityTypeIdLengthException;
13
use Drupal\Core\StringTranslation\StringTranslationTrait;
14 15 16

/**
 * Provides an implementation of an entity type and its metadata.
17 18
 *
 * @ingroup entity_api
19 20 21
 */
class EntityType implements EntityTypeInterface {

22 23
  use StringTranslationTrait;

24 25 26 27 28
  /**
   * Indicates whether entities should be statically cached.
   *
   * @var bool
   */
29
  protected $static_cache = TRUE;
30 31 32 33 34 35

  /**
   * Indicates whether the rendered output of entities should be cached.
   *
   * @var bool
   */
36
  protected $render_cache = TRUE;
37 38 39 40 41 42

  /**
   * Indicates if the persistent cache of field data should be used.
   *
   * @var bool
   */
43
  protected $persistent_cache = TRUE;
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

  /**
   * An array of entity keys.
   *
   * @var array
   */
  protected $entity_keys = array();

  /**
   * The unique identifier of this entity type.
   *
   * @var string
   */
  protected $id;

  /**
   * The name of the provider of this entity type.
   *
   * @var string
   */
  protected $provider;

  /**
   * The name of the entity type class.
   *
   * @var string
   */
  protected $class;

73 74 75 76 77 78 79 80 81
  /**
   * The name of the original entity type class.
   *
   * This is only set if the class name is changed.
   *
   * @var string
   */
  protected $originalClass;

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  /**
   * An array of controllers.
   *
   * @var array
   */
  protected $controllers = array();

  /**
   * The name of the default administrative permission.
   *
   * @var string
   */
  protected $admin_permission;

  /**
   * The permission granularity level.
   *
99
   * The allowed values are respectively "entity_type" or "bundle".
100
   *
101
   * @var string
102
   */
103
  protected $permission_granularity = 'entity_type';
104 105 106 107

  /**
   * Indicates whether fields can be attached to entities of this type.
   *
108
   * @var bool
109
   */
110
  protected $fieldable = FALSE;
111 112 113 114 115 116 117 118 119 120 121

  /**
   * Link templates using the URI template syntax.
   *
   * @var array
   */
  protected $links = array();

  /**
   * The name of a callback that returns the label of the entity.
   *
122
   * @var string|null
123
   */
124
  protected $label_callback = NULL;
125 126 127 128 129 130

  /**
   * The name of the entity type which provides bundles.
   *
   * @var string
   */
131
  protected $bundle_entity_type = 'bundle';
132 133 134 135

  /**
   * The name of the entity type for which bundles are provided.
   *
136
   * @var string|null
137
   */
138
  protected $bundle_of = NULL;
139 140 141 142

  /**
   * The human-readable name of the entity bundles, e.g. Vocabulary.
   *
143
   * @var string|null
144
   */
145
  protected $bundle_label = NULL;
146 147 148 149

  /**
   * The name of the entity type's base table.
   *
150
   * @var string|null
151
   */
152
  protected $base_table = NULL;
153 154 155 156

  /**
   * The name of the entity type's revision data table.
   *
157
   * @var string|null
158
   */
159
  protected $revision_data_table = NULL;
160 161 162 163

  /**
   * The name of the entity type's revision table.
   *
164
   * @var string|null
165
   */
166
  protected $revision_table = NULL;
167 168 169 170

  /**
   * The name of the entity type's data table.
   *
171
   * @var string|null
172
   */
173
  protected $data_table = NULL;
174 175 176 177 178 179 180 181 182 183 184 185 186

  /**
   * Indicates whether entities of this type have multilingual support.
   *
   * @var bool
   */
  protected $translatable = FALSE;

  /**
   * The human-readable name of the type.
   *
   * @var string
   */
187
  protected $label = '';
188 189 190 191

  /**
   * A callable that can be used to provide the entity URI.
   *
192
   * @var callable|null
193
   */
194
  protected $uri_callback = NULL;
195

196 197 198 199 200 201 202 203 204 205
  /**
   * The machine name of the entity type group.
   */
  protected $group;

  /**
   * The human-readable name of the entity type group.
   */
  protected $group_label;

206 207 208 209 210 211 212
  /**
   * The route name used by field UI to attach its management pages.
   *
   * @var string
   */
  protected $field_ui_base_route;

213 214 215 216 217
  /**
   * Constructs a new EntityType.
   *
   * @param array $definition
   *   An array of values from the annotation.
218 219 220
   *
   * @throws \Drupal\Core\Entity\Exception\EntityTypeIdLengthException
   *   Thrown when attempting to instantiate an entity type with too long ID.
221 222
   */
  public function __construct($definition) {
223 224 225 226 227 228 229 230 231 232
    // Throw an exception if the entity type ID is longer than 32 characters.
    if (Unicode::strlen($definition['id']) > static::ID_MAX_LENGTH) {
      throw new EntityTypeIdLengthException(String::format(
        'Attempt to create an entity type with an ID longer than @max characters: @id.', array(
          '@max' => static::ID_MAX_LENGTH,
          '@id' => $definition['id'],
        )
      ));
    }

233 234 235
    foreach ($definition as $property => $value) {
      $this->{$property} = $value;
    }
236 237 238 239 240 241 242

    // Ensure defaults.
    $this->entity_keys += array(
      'revision' => '',
      'bundle' => ''
    );
    $this->controllers += array(
243
      'access' => 'Drupal\Core\Entity\EntityAccessControlHandler',
244
    );
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  }

  /**
   * {@inheritdoc}
   */
  public function get($property) {
    return isset($this->{$property}) ? $this->{$property} : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function set($property, $value) {
    $this->{$property} = $value;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isStaticallyCacheable() {
266
    return $this->static_cache;
267 268 269 270 271 272
  }

  /**
   * {@inheritdoc}
   */
  public function isRenderCacheable() {
273
    return $this->render_cache;
274 275 276 277 278
  }

  /**
   * {@inheritdoc}
   */
279
  public function isPersistentlyCacheable() {
280
    return $this->persistent_cache;
281 282 283 284 285 286
  }

  /**
   * {@inheritdoc}
   */
  public function getKeys() {
287
    return $this->entity_keys;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  }

  /**
   * {@inheritdoc}
   */
  public function getKey($key) {
    $keys = $this->getKeys();
    return isset($keys[$key]) ? $keys[$key] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function hasKey($key) {
    $keys = $this->getKeys();
    return !empty($keys[$key]);
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->id;
  }

  /**
   * {@inheritdoc}
   */
  public function getProvider() {
    return $this->provider;
  }

  /**
   * {@inheritdoc}
   */
  public function getClass() {
    return $this->class;
  }

327 328 329 330 331 332 333
  /**
   * {@inheritdoc}
   */
  public function getOriginalClass() {
    return $this->originalClass ?: $this->class;
  }

334 335 336 337
  /**
   * {@inheritdoc}
   */
  public function setClass($class) {
338 339 340 341 342
    if (!$this->originalClass && $this->class) {
      // If the original class is currently not set, set it to the current
      // class, assume that is the original class name.
      $this->originalClass = $this->class;
    }
343 344 345 346 347 348 349 350 351 352 353 354 355 356
    $this->class = $class;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isSubclassOf($class) {
    return is_subclass_of($this->getClass(), $class);
  }

  /**
   * {@inheritdoc}
   */
357
  public function getControllerClasses() {
358
    return $this->controllers;
359 360 361 362 363
  }

  /**
   * {@inheritdoc}
   */
364 365 366 367 368
  public function getControllerClass($controller_type, $nested = FALSE) {
    if ($this->hasControllerClass($controller_type, $nested)) {
      $controllers = $this->getControllerClasses();
      return $nested ? $controllers[$controller_type][$nested] : $controllers[$controller_type];
    }
369 370 371 372 373
  }

  /**
   * {@inheritdoc}
   */
374
  public function setControllerClass($controller_type, $value) {
375 376 377 378 379 380 381
    $this->controllers[$controller_type] = $value;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  public function hasControllerClass($controller_type, $nested = FALSE) {
    $controllers = $this->getControllerClasses();
    if (!isset($controllers[$controller_type]) || ($nested && !isset($controllers[$controller_type][$nested]))) {
      return FALSE;
    }
    $controller = $controllers[$controller_type];
    if ($nested) {
      $controller = $controller[$nested];
    }
    return class_exists($controller);
  }

  /**
   * {@inheritdoc}
   */
  public function getStorageClass() {
    return $this->getControllerClass('storage');
  }

  /**
   * {@inheritdoc}
   */
  public function setStorageClass($class) {
    $this->controllers['storage'] = $class;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormClass($operation) {
    return $this->getControllerClass('form', $operation);
413 414 415 416 417
  }

  /**
   * {@inheritdoc}
   */
418
  public function setFormClass($operation, $class) {
419 420 421 422 423 424 425
    $this->controllers['form'][$operation] = $class;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
426 427 428 429 430 431 432
  public function hasFormClasses() {
    return !empty($this->controllers['form']);
  }

  /**
   * {@inheritdoc}
   */
433 434
  public function getListBuilderClass() {
    return $this->getControllerClass('list_builder');
435 436 437 438 439
  }

  /**
   * {@inheritdoc}
   */
440 441
  public function setListBuilderClass($class) {
    $this->controllers['list_builder'] = $class;
442 443 444
    return $this;
  }

445 446 447
  /**
   * {@inheritdoc}
   */
448 449
  public function hasListBuilderClass() {
    return $this->hasControllerClass('list_builder');
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  }

  /**
   * {@inheritdoc}
   */
  public function getViewBuilderClass() {
    return $this->getControllerClass('view_builder');
  }

  /**
   * {@inheritdoc}
   */
  public function setViewBuilderClass($class) {
    $this->controllers['view_builder'] = $class;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasViewBuilderClass() {
    return $this->hasControllerClass('view_builder');
  }

  /**
   * {@inheritdoc}
   */
477
  public function getAccessControlClass() {
478 479 480 481 482 483 484 485 486 487 488
    return $this->getControllerClass('access');
  }

  /**
   * {@inheritdoc}
   */
  public function setAccessClass($class) {
    $this->controllers['access'] = $class;
    return $this;
  }

489 490 491 492 493 494 495 496 497 498 499
  /**
   * {@inheritdoc}
   */
  public function getAdminPermission() {
    return $this->admin_permission ?: FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getPermissionGranularity() {
500
    return $this->permission_granularity;
501 502 503 504 505 506
  }

  /**
   * {@inheritdoc}
   */
  public function isFieldable() {
507
    return $this->fieldable;
508 509 510 511 512 513
  }

  /**
   * {@inheritdoc}
   */
  public function getLinkTemplates() {
514
    return $this->links;
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
  }

  /**
   * {@inheritdoc}
   */
  public function getLinkTemplate($key) {
    $links = $this->getLinkTemplates();
    return isset($links[$key]) ? $links[$key] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function hasLinkTemplate($key) {
    $links = $this->getLinkTemplates();
    return isset($links[$key]);
  }

  /**
   * {@inheritdoc}
   */
  public function setLinkTemplate($key, $route_name) {
    $this->links[$key] = $route_name;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabelCallback() {
545
    return $this->label_callback;
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
  }

  /**
   * {@inheritdoc}
   */
  public function setLabelCallback($callback) {
    $this->label_callback = $callback;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasLabelCallback() {
    return isset($this->label_callback);
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleEntityType() {
567
    return $this->bundle_entity_type;
568 569 570 571 572 573
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleOf() {
574
    return $this->bundle_of;
575 576 577 578 579 580
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleLabel() {
581
    return $this->bundle_label;
582 583 584 585 586 587
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseTable() {
588
    return $this->base_table;
589 590 591 592 593 594 595 596 597
  }

  /**
   * {@inheritdoc}
   */
  public function isTranslatable() {
    return !empty($this->translatable);
  }

598 599 600 601 602 603 604 605
  /**
   * {@inheritdoc}
   */
  public function isRevisionable() {
    // Entity types are revisionable if a revision key has been specified.
    return $this->hasKey('revision');
  }

606 607 608 609
  /**
   * {@inheritdoc}
   */
  public function getConfigPrefix() {
610
    return FALSE;
611 612 613 614 615 616
  }

  /**
   * {@inheritdoc}
   */
  public function getRevisionDataTable() {
617
    return $this->revision_data_table;
618 619 620 621 622 623
  }

  /**
   * {@inheritdoc}
   */
  public function getRevisionTable() {
624
    return $this->revision_table;
625 626 627 628 629 630
  }

  /**
   * {@inheritdoc}
   */
  public function getDataTable() {
631
    return $this->data_table;
632 633 634 635 636 637
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
638
    return $this->label;
639 640 641 642 643 644 645 646 647 648 649 650 651
  }

  /**
   * {@inheritdoc}
   */
  public function getLowercaseLabel() {
    return Unicode::strtolower($this->getLabel());
  }

  /**
   * {@inheritdoc}
   */
  public function getUriCallback() {
652
    return $this->uri_callback;
653 654 655 656 657 658 659 660 661 662
  }

  /**
   * {@inheritdoc}
   */
  public function setUriCallback($callback) {
    $this->uri_callback = $callback;
    return $this;
  }

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
  /**
   * {@inheritdoc}
   */
  public function getGroup() {
    return $this->group;
  }


  /**
   * {@inheritdoc}
   */
  public function getGroupLabel() {
    return !empty($this->group_label) ? $this->group_label : $this->t('Other', array(), array('context' => 'Entity type group'));
  }

678
}