entity.inc 12 KB

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