Skip to content

Instantly share code, notes, and snippets.

@damiankloip
Created January 2, 2014 16:32
Show Gist options
  • Save damiankloip/8221935 to your computer and use it in GitHub Desktop.
Save damiankloip/8221935 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityType.
*/
namespace Drupal\Core\Entity;
use Drupal\Component\Utility\Unicode;
/**
* Provides an implementation of an entity type and its metadata.
*/
class EntityType implements EntityTypeInterface {
/**
* Indicates whether entities should be statically cached.
*
* @var bool
*/
protected $static_cache;
/**
* Indicates whether the rendered output of entities should be cached.
*
* @var bool
*/
protected $render_cache;
/**
* Indicates if the persistent cache of field data should be used.
*
* @var bool
*/
protected $field_cache;
/**
* 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;
/**
* 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.
*
* The allowed values are respectively "entity_type", "bundle" or FALSE.
*
* @var string|bool
*/
protected $permission_granularity;
/**
* An array describing how the Field API can extract the information it needs
* from the bundle objects for this type (e.g Vocabulary objects for terms;
* not applicable for nodes):
* - bundle: The name of the property that contains the name of the bundle
* object.
*
* This entry can be omitted if this type's bundles do not exist as standalone
* objects.
*
* @var array
*/
protected $bundle_keys = array();
/**
* Indicates whether fields can be attached to entities of this type.
*
* @var bool (optional)
*/
protected $fieldable;
/**
* Link templates using the URI template syntax.
*
* @var array
*/
protected $links = array();
/**
* The name of a callback that returns the label of the entity.
*
* @var string
*/
protected $label_callback;
/**
* The name of the entity type which provides bundles.
*
* @var string
*/
protected $bundle_entity_type;
/**
* The name of the entity type for which bundles are provided.
*
* @var string
*/
protected $bundle_of;
/**
* The human-readable name of the entity bundles, e.g. Vocabulary.
*
* @var string
*/
protected $bundle_label;
/**
* The name of the entity type's base table.
*
* @var string
*/
protected $base_table;
/**
* The name of the entity type's revision data table.
*
* @var string
*/
protected $revision_data_table;
/**
* The name of the entity type's revision table.
*
* @var string
*/
protected $revision_table;
/**
* The name of the entity type's data table.
*
* @var string
*/
protected $data_table;
/**
* Indicates whether entities of this type have multilingual support.
*
* @var bool
*/
protected $translatable = FALSE;
/**
* Returns the config prefix used by the configuration entity type.
*
* @var string
*/
protected $config_prefix;
/**
* The human-readable name of the type.
*
* @var string
*/
protected $label;
/**
* A callable that can be used to provide the entity URI.
*
* @var callable
*/
protected $uri_callback;
/**
* Constructs a new EntityType.
*
* @param array $definition
* An array of values from the annotation.
*/
public function __construct($definition) {
$this->definition = $definition;
}
/**
* {@inheritdoc}
*/
public function get($property, $default = NULL) {
return isset($this->definition[$property]) ? $this->definition[$property] : $default;
}
/**
* {@inheritdoc}
*/
public function set($property, $value) {
$this->definition[$property] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function isStaticallyCacheable() {
return $this->get('static_cache', TRUE);
}
/**
* {@inheritdoc}
*/
public function isRenderCacheable() {
return $this->get('render_cache', TRUE);
}
/**
* {@inheritdoc}
*/
public function isFieldDataCacheable() {
return $this->get('field_cache', TRUE);
}
/**
* {@inheritdoc}
*/
public function getKeys() {
return $this->get('entity_keys') + array('revision' => '', 'bundle' => '');
}
/**
* {@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->get('id');
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return $this->get('provider');
}
/**
* {@inheritdoc}
*/
public function getClass() {
return $this->get('class');
}
/**
* {@inheritdoc}
*/
public function setClass($class) {
$this->set('class', $class);
return $this;
}
/**
* {@inheritdoc}
*/
public function isSubclassOf($class) {
return is_subclass_of($this->getClass(), $class);
}
/**
* {@inheritdoc}
*/
public function getControllers() {
return $this->get('controllers') + array(
'access' => 'Drupal\Core\Entity\EntityAccessController',
);
}
/**
* {@inheritdoc}
*/
public function getController($controller_type) {
$controllers = $this->getControllers();
return $controllers[$controller_type];
}
/**
* {@inheritdoc}
*/
public function setController($controller_type, $value) {
$controllers = $this->getControllers();
$controllers[$controller_type] = $value;
$this->set('controllers', $controllers);
return $this;
}
/**
* {@inheritdoc}
*/
public function hasController($controller_type) {
$controllers = $this->getControllers();
return isset($controllers[$controller_type]);
}
/**
* {@inheritdoc}
*/
public function setForm($operation, $class) {
$this->controllers['form'][$operation] = $class;
return $this;
}
/**
* {@inheritdoc}
*/
public function setList($class) {
$this->controllers['list'] = $class;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAdminPermission() {
return $this->get('admin_permission', FALSE);
}
/**
* {@inheritdoc}
*/
public function getPermissionGranularity() {
return $this->get('permission_granularity', 'entity_type');
}
/**
* {@inheritdoc}
*/
public function getBundleKeys() {
return $this->get('bundle_keys', array());
}
/**
* {@inheritdoc}
*/
public function getBundleKey($name) {
return isset($this->bundle_keys[$name]) ? $this->bundle_keys[$name] : FALSE;
}
/**
* {@inheritdoc}
*/
public function isFieldable() {
return $this->get('fieldable', FALSE);
}
/**
* {@inheritdoc}
*/
public function getLinkTemplates() {
return $this->get('links', array());
}
/**
* {@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() {
return $this->get('label_callback', FALSE);
}
/**
* {@inheritdoc}
*/
public function setLabelCallback($callback) {
$this->set('label_callback', $callback);
return $this;
}
/**
* {@inheritdoc}
*/
public function hasLabelCallback() {
return isset($this->label_callback);
}
/**
* {@inheritdoc}
*/
public function getBundleEntityType() {
return $this->get('bundle_entity_type', 'bundle');
}
/**
* {@inheritdoc}
*/
public function getBundleOf() {
return $this->get('bundle_of', FALSE);
}
/**
* {@inheritdoc}
*/
public function getBundleLabel() {
return $this->get('bundle_label', FALSE);
}
/**
* {@inheritdoc}
*/
public function getBaseTable() {
return $this->get('base_table', FALSE);
}
/**
* {@inheritdoc}
*/
public function isTranslatable() {
return $this->get('translatable', FALSE);
}
/**
* {@inheritdoc}
*/
public function getConfigPrefix() {
return $this->get('config_prefix', FALSE);
}
/**
* {@inheritdoc}
*/
public function getRevisionDataTable() {
return $this->get('revision_data_table', FALSE);
}
/**
* {@inheritdoc}
*/
public function getRevisionTable() {
return $this->get('revision_table', FALSE);
}
/**
* {@inheritdoc}
*/
public function getDataTable() {
return $this->get('data_table', FALSE);
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->get('label', '');
}
/**
* {@inheritdoc}
*/
public function getLowercaseLabel() {
return Unicode::strtolower($this->getLabel());
}
/**
* {@inheritdoc}
*/
public function getUriCallback() {
return $this->get('uri_callback', FALSE);
}
/**
* {@inheritdoc}
*/
public function setUriCallback($callback) {
$this->set('uri_callback', $callback);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment