entity.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * @file
  4. * Provides a base class for entities.
  5. */
  6. /**
  7. * A common class for entities.
  8. *
  9. * It's suggested, but not required, to extend this class and to override
  10. * __construct() in order to specify a fixed entity type.
  11. *
  12. * For providing an entity label and URI it is suggested to override the
  13. * defaultLabel() and defaultUri() methods, and to specify the
  14. * entity_class_label() and entity_class_uri() as respective callbacks in
  15. * hook_entity_info(). That way modules are able to override your defaults
  16. * by altering the hook_entity_info() callbacks, while $entity->label() and
  17. * $entity->uri() reflect this changes as well.
  18. *
  19. * Defaults for entity properties can be easily defined by adding class
  20. * properties, e.g.:
  21. * @code
  22. * public $name = '';
  23. * public $count = 0;
  24. * @endcode
  25. */
  26. class Entity {
  27. protected $entityType;
  28. protected $entityInfo;
  29. protected $idKey, $nameKey, $statusKey;
  30. /**
  31. * Creates a new entity.
  32. *
  33. * @see entity_create()
  34. */
  35. public function __construct(array $values = array(), $entityType = NULL) {
  36. if (empty($entityType)) {
  37. throw new Exception('Cannot create an instance of Entity without a specified entity type.');
  38. }
  39. $this->entityType = $entityType;
  40. $this->setUp();
  41. // Set initial values.
  42. foreach ($values as $key => $value) {
  43. $this->$key = $value;
  44. }
  45. }
  46. /**
  47. * Set up the object instance on construction or unserializiation.
  48. */
  49. protected function setUp() {
  50. $this->entityInfo = entity_get_info($this->entityType);
  51. $this->idKey = $this->entityInfo['entity keys']['id'];
  52. $this->nameKey = isset($this->entityInfo['entity keys']['name']) ? $this->entityInfo['entity keys']['name'] : $this->idKey;
  53. $this->statusKey = empty($info['entity keys']['status']) ? 'status' : $info['entity keys']['status'];
  54. }
  55. /**
  56. * Returns the internal, numeric identifier.
  57. *
  58. * Returns the numeric identifier, even if the entity type has specified a
  59. * name key. In the latter case, the numeric identifier is supposed to be used
  60. * when dealing generically with entities or internally to refer to an entity,
  61. * i.e. in a relational database. If unsure, use Entity:identifier().
  62. */
  63. public function internalIdentifier() {
  64. return isset($this->{$this->idKey}) ? $this->{$this->idKey} : NULL;
  65. }
  66. /**
  67. * Returns the entity identifier, i.e. the entities name or numeric id.
  68. *
  69. * @return
  70. * The identifier of the entity. If the entity type makes use of a name key,
  71. * the name is returned, else the numeric id.
  72. *
  73. * @see entity_id()
  74. */
  75. public function identifier() {
  76. return isset($this->{$this->nameKey}) ? $this->{$this->nameKey} : NULL;
  77. }
  78. /**
  79. * Returns the info of the type of the entity.
  80. *
  81. * @see entity_get_info()
  82. */
  83. public function entityInfo() {
  84. return $this->entityInfo;
  85. }
  86. /**
  87. * Returns the type of the entity.
  88. */
  89. public function entityType() {
  90. return $this->entityType;
  91. }
  92. /**
  93. * Returns the bundle of the entity.
  94. *
  95. * @return
  96. * The bundle of the entity. Defaults to the entity type if the entity type
  97. * does not make use of different bundles.
  98. */
  99. public function bundle() {
  100. return !empty($this->entityInfo['entity keys']['bundle']) ? $this->{$this->entityInfo['entity keys']['bundle']} : $this->entityType;
  101. }
  102. /**
  103. * Returns the label of the entity.
  104. *
  105. * Modules may alter the label by specifying another 'label callback' using
  106. * hook_entity_info_alter().
  107. *
  108. * @see entity_label()
  109. */
  110. public function label() {
  111. if (isset($this->entityInfo['label callback']) && $this->entityInfo['label callback'] == 'entity_class_label') {
  112. return $this->defaultLabel();
  113. }
  114. return entity_label($this->entityType, $this);
  115. }
  116. /**
  117. * Defines the entity label if the 'entity_class_label' callback is used.
  118. *
  119. * Specify 'entity_class_label' as 'label callback' in hook_entity_info() to
  120. * let the entity label point to this method. Override this in order to
  121. * implement a custom default label.
  122. */
  123. protected function defaultLabel() {
  124. // Add in the translated specified label property.
  125. return $this->getTranslation($this->entityInfo['entity keys']['label']);
  126. }
  127. /**
  128. * Returns the uri of the entity just as entity_uri().
  129. *
  130. * Modules may alter the uri by specifying another 'uri callback' using
  131. * hook_entity_info_alter().
  132. *
  133. * @see entity_uri()
  134. */
  135. public function uri() {
  136. if (isset($this->entityInfo['uri callback']) && $this->entityInfo['uri callback'] == 'entity_class_uri') {
  137. return $this->defaultUri();
  138. }
  139. return entity_uri($this->entityType, $this);
  140. }
  141. /**
  142. * Override this in order to implement a custom default URI and specify
  143. * 'entity_class_uri' as 'uri callback' hook_entity_info().
  144. */
  145. protected function defaultUri() {
  146. return array('path' => 'default/' . $this->identifier());
  147. }
  148. /**
  149. * Checks if the entity has a certain exportable status.
  150. *
  151. * @param $status
  152. * A status constant, i.e. one of ENTITY_CUSTOM, ENTITY_IN_CODE,
  153. * ENTITY_OVERRIDDEN or ENTITY_FIXED.
  154. *
  155. * @return
  156. * For exportable entities TRUE if the entity has the status, else FALSE.
  157. * In case the entity is not exportable, NULL is returned.
  158. *
  159. * @see entity_has_status()
  160. */
  161. public function hasStatus($status) {
  162. if (!empty($this->entityInfo['exportable'])) {
  163. return isset($this->{$this->statusKey}) && ($this->{$this->statusKey} & $status) == $status;
  164. }
  165. }
  166. /**
  167. * Permanently saves the entity.
  168. *
  169. * @see entity_save()
  170. */
  171. public function save() {
  172. return entity_get_controller($this->entityType)->save($this);
  173. }
  174. /**
  175. * Permanently deletes the entity.
  176. *
  177. * @see entity_delete()
  178. */
  179. public function delete() {
  180. $id = $this->identifier();
  181. if (isset($id)) {
  182. entity_get_controller($this->entityType)->delete(array($id));
  183. }
  184. }
  185. /**
  186. * Exports the entity.
  187. *
  188. * @see entity_export()
  189. */
  190. public function export($prefix = '') {
  191. return entity_get_controller($this->entityType)->export($this, $prefix);
  192. }
  193. /**
  194. * Generate an array for rendering the entity.
  195. *
  196. * @see entity_view()
  197. */
  198. public function view($view_mode = 'full', $langcode = NULL, $page = NULL) {
  199. return entity_get_controller($this->entityType)->view(array($this), $view_mode, $langcode, $page);
  200. }
  201. /**
  202. * Builds a structured array representing the entity's content.
  203. *
  204. * @see entity_build_content()
  205. */
  206. public function buildContent($view_mode = 'full', $langcode = NULL) {
  207. return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode);
  208. }
  209. /**
  210. * Gets the raw, translated value of a property or field.
  211. *
  212. * Supports retrieving field translations as well as i18n string translations.
  213. *
  214. * Note that this returns raw data values, which might not reflect what
  215. * has been declared for hook_entity_property_info() as no 'getter callbacks'
  216. * are invoked or no referenced entities are loaded. For retrieving values
  217. * reflecting the property info make use of entity metadata wrappers, see
  218. * entity_metadata_wrapper().
  219. *
  220. * @param $property_name
  221. * The name of the property to return; e.g., 'title'.
  222. * @param $langcode
  223. * (optional) The language code of the language to which the value should
  224. * be translated. If set to NULL, the default display language is being
  225. * used.
  226. *
  227. * @return
  228. * The raw, translated property value; or the raw, un-translated value if no
  229. * translation is available.
  230. *
  231. * @todo Implement an analogous setTranslation() method for updating.
  232. */
  233. public function getTranslation($property, $langcode = NULL) {
  234. $all_info = entity_get_all_property_info($this->entityType);
  235. $property_info = $all_info[$property];
  236. if (!empty($property_info['translatable'])) {
  237. if (!empty($property_info['field'])) {
  238. return field_get_items($this->entityType, $this, $property, $langcode);
  239. }
  240. elseif (!empty($property_info['i18n string'])) {
  241. $name = $this->entityInfo['module'] . ':' . $this->entityType . ':' . $this->identifier() . ':' . $property;
  242. return entity_i18n_string($name, $this->$property, $langcode);
  243. }
  244. }
  245. return $this->$property;
  246. }
  247. /**
  248. * Checks whether the entity is the default revision.
  249. *
  250. * @return Boolean
  251. *
  252. * @see entity_revision_is_default()
  253. */
  254. public function isDefaultRevision() {
  255. if (!empty($this->entityInfo['entity keys']['revision'])) {
  256. $key = !empty($this->entityInfo['entity keys']['default revision']) ? $this->entityInfo['entity keys']['default revision'] : 'default_revision';
  257. return !empty($this->$key);
  258. }
  259. return TRUE;
  260. }
  261. /**
  262. * Magic method to only serialize what's necessary.
  263. */
  264. public function __sleep() {
  265. $vars = get_object_vars($this);
  266. unset($vars['entityInfo'], $vars['idKey'], $vars['nameKey'], $vars['statusKey']);
  267. // Also key the returned array with the variable names so the method may
  268. // be easily overridden and customized.
  269. return drupal_map_assoc(array_keys($vars));
  270. }
  271. /**
  272. * Magic method to invoke setUp() on unserialization.
  273. */
  274. public function __wakeup() {
  275. $this->setUp();
  276. }
  277. }
  278. /**
  279. * These classes are deprecated by "Entity" and are only here for backward
  280. * compatibility reasons.
  281. */
  282. class EntityDB extends Entity {}
  283. class EntityExtendable extends Entity {}
  284. class EntityDBExtendable extends Entity {}