entity.inc 10 KB

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