entity.inc 9.9 KB

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