Skip to content
Snippets Groups Projects
Commit a2bce8f3 authored by e0ipso's avatar e0ipso Committed by Wolfgang Ziegler // fago
Browse files

Issue #2420551 by e0ipso, fago: Add EntityInterface to allow programming to interfaces.

parent a9790ee0
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,168 @@
* Provides a base class for entities.
*/
/**
* Interface for class based entities.
*/
interface EntityInterface {
/**
* Returns the internal, numeric identifier.
*
* Returns the numeric identifier, even if the entity type has specified a
* name key. In the latter case, the numeric identifier is supposed to be used
* when dealing generically with entities or internally to refer to an entity,
* i.e. in a relational database. If unsure, use Entity:identifier().
*/
public function internalIdentifier();
/**
* Returns the entity identifier, i.e. the entities name or numeric id.
*
* @return
* The identifier of the entity. If the entity type makes use of a name key,
* the name is returned, else the numeric id.
*
* @see entity_id()
*/
public function identifier();
/**
* Returns the info of the type of the entity.
*
* @see entity_get_info()
*/
public function entityInfo();
/**
* Returns the type of the entity.
*/
public function entityType();
/**
* Returns the bundle of the entity.
*
* @return
* The bundle of the entity. Defaults to the entity type if the entity type
* does not make use of different bundles.
*/
public function bundle();
/**
* Returns the EntityMetadataWrapper of the entity.
*
* @return EntityDrupalWrapper
* An EntityMetadataWrapper containing the entity.
*/
public function wrapper();
/**
* Returns the label of the entity.
*
* Modules may alter the label by specifying another 'label callback' using
* hook_entity_info_alter().
*
* @see entity_label()
*/
public function label();
/**
* Returns the uri of the entity just as entity_uri().
*
* Modules may alter the uri by specifying another 'uri callback' using
* hook_entity_info_alter().
*
* @see entity_uri()
*/
public function uri();
/**
* Checks if the entity has a certain exportable status.
*
* @param $status
* A status constant, i.e. one of ENTITY_CUSTOM, ENTITY_IN_CODE,
* ENTITY_OVERRIDDEN or ENTITY_FIXED.
*
* @return bool
* For exportable entities TRUE if the entity has the status, else FALSE.
* In case the entity is not exportable, NULL is returned.
*
* @see entity_has_status()
*/
public function hasStatus($status);
/**
* Permanently saves the entity.
*
* @see entity_save()
*/
public function save();
/**
* Permanently deletes the entity.
*
* @see entity_delete()
*/
public function delete();
/**
* Exports the entity.
*
* @see entity_export()
*/
public function export($prefix = '');
/**
* Generate an array for rendering the entity.
*
* @see entity_view()
*/
public function view($view_mode = 'full', $langcode = NULL, $page = NULL);
/**
* Builds a structured array representing the entity's content.
*
* @see entity_build_content()
*/
public function buildContent($view_mode = 'full', $langcode = NULL);
/**
* Gets the raw, translated value of a property or field.
*
* Supports retrieving field translations as well as i18n string translations.
*
* Note that this returns raw data values, which might not reflect what
* has been declared for hook_entity_property_info() as no 'getter callbacks'
* are invoked or no referenced entities are loaded. For retrieving values
* reflecting the property info make use of entity metadata wrappers, see
* entity_metadata_wrapper().
*
* @param $property
* The name of the property to return; e.g., 'title'.
* @param $langcode
* (optional) The language code of the language to which the value should
* be translated. If set to NULL, the default display language is being
* used.
*
* @return
* The raw, translated property value; or the raw, un-translated value if no
* translation is available.
*
* @todo Implement an analogous setTranslation() method for updating.
*/
public function getTranslation($property, $langcode = NULL);
/**
* Checks whether the entity is the default revision.
*
* @return Boolean
*
* @see entity_revision_is_default()
*/
public function isDefaultRevision();
}
/**
* A common class for entities.
*
......@@ -25,7 +187,7 @@
* public $count = 0;
* @endcode
*/
class Entity {
class Entity implements EntityInterface {
protected $entityType;
protected $entityInfo;
......@@ -34,9 +196,7 @@ class Entity {
protected $wrapper;
/**
* Creates a new entity.
*
* @see entity_create()
* {@inheritdoc}
*/
public function __construct(array $values = array(), $entityType = NULL) {
if (empty($entityType)) {
......@@ -61,62 +221,42 @@ class Entity {
}
/**
* Returns the internal, numeric identifier.
*
* Returns the numeric identifier, even if the entity type has specified a
* name key. In the latter case, the numeric identifier is supposed to be used
* when dealing generically with entities or internally to refer to an entity,
* i.e. in a relational database. If unsure, use Entity:identifier().
* {@inheritdoc}
*/
public function internalIdentifier() {
return isset($this->{$this->idKey}) ? $this->{$this->idKey} : NULL;
}
/**
* Returns the entity identifier, i.e. the entities name or numeric id.
*
* @return
* The identifier of the entity. If the entity type makes use of a name key,
* the name is returned, else the numeric id.
*
* @see entity_id()
* {@inheritdoc}
*/
public function identifier() {
return isset($this->{$this->nameKey}) ? $this->{$this->nameKey} : NULL;
}
/**
* Returns the info of the type of the entity.
*
* @see entity_get_info()
* {@inheritdoc}
*/
public function entityInfo() {
return $this->entityInfo;
}
/**
* Returns the type of the entity.
* {@inheritdoc}
*/
public function entityType() {
return $this->entityType;
}
/**
* Returns the bundle of the entity.
*
* @return
* The bundle of the entity. Defaults to the entity type if the entity type
* does not make use of different bundles.
* {@inheritdoc}
*/
public function bundle() {
return !empty($this->entityInfo['entity keys']['bundle']) ? $this->{$this->entityInfo['entity keys']['bundle']} : $this->entityType;
}
/**
* Returns the EntityMetadataWrapper of the entity.
*
* @return EntityDrupalWrapper
* An EntityMetadataWrapper containing the entity.
* {@inheritdoc}
*/
public function wrapper() {
if (empty($this->wrapper)) {
......@@ -130,12 +270,7 @@ class Entity {
}
/**
* Returns the label of the entity.
*
* Modules may alter the label by specifying another 'label callback' using
* hook_entity_info_alter().
*
* @see entity_label()
* {@inheritdoc}
*/
public function label() {
// If the default label flag is enabled, this is being invoked recursively.
......@@ -165,12 +300,7 @@ class Entity {
}
/**
* Returns the uri of the entity just as entity_uri().
*
* Modules may alter the uri by specifying another 'uri callback' using
* hook_entity_info_alter().
*
* @see entity_uri()
* {@inheritdoc}
*/
public function uri() {
if (isset($this->entityInfo['uri callback']) && $this->entityInfo['uri callback'] == 'entity_class_uri') {
......@@ -188,17 +318,7 @@ class Entity {
}
/**
* Checks if the entity has a certain exportable status.
*
* @param $status
* A status constant, i.e. one of ENTITY_CUSTOM, ENTITY_IN_CODE,
* ENTITY_OVERRIDDEN or ENTITY_FIXED.
*
* @return
* For exportable entities TRUE if the entity has the status, else FALSE.
* In case the entity is not exportable, NULL is returned.
*
* @see entity_has_status()
* {@inheritdoc}
*/
public function hasStatus($status) {
if (!empty($this->entityInfo['exportable'])) {
......@@ -207,18 +327,14 @@ class Entity {
}
/**
* Permanently saves the entity.
*
* @see entity_save()
* {@inheritdoc}
*/
public function save() {
return entity_get_controller($this->entityType)->save($this);
}
/**
* Permanently deletes the entity.
*
* @see entity_delete()
* {@inheritdoc}
*/
public function delete() {
$id = $this->identifier();
......@@ -228,55 +344,28 @@ class Entity {
}
/**
* Exports the entity.
*
* @see entity_export()
* {@inheritdoc}
*/
public function export($prefix = '') {
return entity_get_controller($this->entityType)->export($this, $prefix);
}
/**
* Generate an array for rendering the entity.
*
* @see entity_view()
* {@inheritdoc}
*/
public function view($view_mode = 'full', $langcode = NULL, $page = NULL) {
return entity_get_controller($this->entityType)->view(array($this), $view_mode, $langcode, $page);
}
/**
* Builds a structured array representing the entity's content.
*
* @see entity_build_content()
* {@inheritdoc}
*/
public function buildContent($view_mode = 'full', $langcode = NULL) {
return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode);
}
/**
* Gets the raw, translated value of a property or field.
*
* Supports retrieving field translations as well as i18n string translations.
*
* Note that this returns raw data values, which might not reflect what
* has been declared for hook_entity_property_info() as no 'getter callbacks'
* are invoked or no referenced entities are loaded. For retrieving values
* reflecting the property info make use of entity metadata wrappers, see
* entity_metadata_wrapper().
*
* @param $property_name
* The name of the property to return; e.g., 'title'.
* @param $langcode
* (optional) The language code of the language to which the value should
* be translated. If set to NULL, the default display language is being
* used.
*
* @return
* The raw, translated property value; or the raw, un-translated value if no
* translation is available.
*
* @todo Implement an analogous setTranslation() method for updating.
* {@inheritdoc}
*/
public function getTranslation($property, $langcode = NULL) {
$all_info = entity_get_all_property_info($this->entityType);
......@@ -296,11 +385,7 @@ class Entity {
}
/**
* Checks whether the entity is the default revision.
*
* @return Boolean
*
* @see entity_revision_is_default()
* {@inheritdoc}
*/
public function isDefaultRevision() {
if (!empty($this->entityInfo['entity keys']['revision'])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment