entity.property.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /**
  3. * @file
  4. * Provides API functions around hook_entity_property_info(). Also see
  5. * entity.info.inc, which cares for providing entity property info for all core
  6. * entity types.
  7. */
  8. /**
  9. * Get the entity property info array of an entity type.
  10. *
  11. * @param $entity_type
  12. * The entity type, e.g. node, for which the info shall be returned, or NULL
  13. * to return an array with info about all types.
  14. *
  15. * @see hook_entity_property_info()
  16. * @see hook_entity_property_info_alter()
  17. */
  18. function entity_get_property_info($entity_type = NULL) {
  19. // Use the advanced drupal_static() pattern, since this is called very often.
  20. static $drupal_static_fast;
  21. if (!isset($drupal_static_fast)) {
  22. $drupal_static_fast['info'] = &drupal_static(__FUNCTION__);
  23. }
  24. $info = &$drupal_static_fast['info'];
  25. // hook_entity_property_info() includes translated strings, so each language
  26. // is cached separately.
  27. $langcode = $GLOBALS['language']->language;
  28. if (empty($info)) {
  29. if ($cache = cache_get("entity_property_info:$langcode")) {
  30. $info = $cache->data;
  31. }
  32. else {
  33. $info = module_invoke_all('entity_property_info');
  34. // Let other modules alter the entity info.
  35. drupal_alter('entity_property_info', $info);
  36. cache_set("entity_property_info:$langcode", $info);
  37. }
  38. }
  39. return empty($entity_type) ? $info : (isset($info[$entity_type]) ? $info[$entity_type] : array());
  40. }
  41. /**
  42. * Returns the default information for an entity property.
  43. *
  44. * @return
  45. * An array of optional property information keys mapped to their defaults.
  46. *
  47. * @see hook_entity_property_info()
  48. */
  49. function entity_property_info_defaults() {
  50. return array(
  51. 'type' => 'text',
  52. 'getter callback' => 'entity_property_verbatim_get',
  53. );
  54. }
  55. /**
  56. * Gets an array of info about all properties of a given entity type.
  57. *
  58. * In contrast to entity_get_property_info(), this function returns info about
  59. * all properties the entity might have, thus it adds an all properties assigned
  60. * to entity bundles.
  61. *
  62. * @param $entity_type
  63. * (optiona) The entity type to return properties for.
  64. *
  65. * @return
  66. * An array of info about properties. If the type is ommitted, all known
  67. * properties are returned.
  68. */
  69. function entity_get_all_property_info($entity_type = NULL) {
  70. if (!isset($entity_type)) {
  71. // Retrieve all known properties.
  72. $properties = array();
  73. foreach (entity_get_info() as $entity_type => $info) {
  74. $properties += entity_get_all_property_info($entity_type);
  75. }
  76. return $properties;
  77. }
  78. // Else retrieve the properties of the given entity type only.
  79. $info = entity_get_property_info($entity_type);
  80. $info += array('properties' => array(), 'bundles' => array());
  81. // Add all bundle properties.
  82. foreach ($info['bundles'] as $bundle => $bundle_info) {
  83. $bundle_info += array('properties' => array());
  84. $info['properties'] += $bundle_info['properties'];
  85. }
  86. return $info['properties'];
  87. }
  88. /**
  89. * Queries for entities having the given property value.
  90. *
  91. * @param $entity_type
  92. * The type of the entity.
  93. * @param $property
  94. * The name of the property to query for.
  95. * @param $value
  96. * A single property value or an array of possible values to query for.
  97. * @param $limit
  98. * Limit the numer of results. Defaults to 30.
  99. *
  100. * @return
  101. * An array of entity ids or NULL if there is no information how to query for
  102. * the given property.
  103. */
  104. function entity_property_query($entity_type, $property, $value, $limit = 30) {
  105. $properties = entity_get_all_property_info($entity_type);
  106. $info = $properties[$property] + array('type' => 'text', 'queryable' => !empty($properties[$property]['schema field']));
  107. // We still support the deprecated query callback, so just add in EFQ-based
  108. // callbacks in case 'queryable' is set to TRUE and make use of the callback.
  109. if ($info['queryable'] && empty($info['query callback'])) {
  110. $info['query callback'] = !empty($info['field']) ? 'entity_metadata_field_query' : 'entity_metadata_table_query';
  111. }
  112. $type = $info['type'];
  113. // Make sure an entity or a list of entities are passed on as identifiers
  114. // with the help of the wrappers. For that ensure the data type matches the
  115. // passed on value(s).
  116. if (is_array($value) && !entity_property_list_extract_type($type)) {
  117. $type = 'list<' . $type . '>';
  118. }
  119. elseif (!is_array($value) && entity_property_list_extract_type($type)) {
  120. $type = entity_property_list_extract_type($type);
  121. }
  122. $wrapper = entity_metadata_wrapper($type, $value);
  123. $value = $wrapper->value(array('identifier' => TRUE));
  124. if (!empty($info['query callback'])) {
  125. return $info['query callback']($entity_type, $property, $value, $limit);
  126. }
  127. }
  128. /**
  129. * Resets the cached information of hook_entity_property_info().
  130. */
  131. function entity_property_info_cache_clear() {
  132. drupal_static_reset('entity_get_property_info');
  133. // Clear all languages.
  134. cache_clear_all('entity_property_info:', 'cache', TRUE);
  135. }
  136. /**
  137. * Implements hook_hook_info().
  138. */
  139. function entity_hook_info() {
  140. $hook_info['entity_property_info'] = array(
  141. 'group' => 'info',
  142. );
  143. $hook_info['entity_property_info_alter'] = array(
  144. 'group' => 'info',
  145. );
  146. return $hook_info;
  147. }
  148. /**
  149. * Implements hook_field_info_alter().
  150. * Defines default property types for core field types.
  151. */
  152. function entity_field_info_alter(&$field_info) {
  153. if (module_exists('number')) {
  154. $field_info['number_integer']['property_type'] = 'integer';
  155. $field_info['number_decimal']['property_type'] = 'decimal';
  156. $field_info['number_float']['property_type'] = 'decimal';
  157. }
  158. if (module_exists('text')) {
  159. $field_info['text']['property_type'] = 'text';
  160. $field_info['text']['property_callbacks'][] = 'entity_metadata_field_text_property_callback';
  161. $field_info['text_long']['property_type'] = 'text';
  162. $field_info['text_long']['property_callbacks'][] = 'entity_metadata_field_text_property_callback';
  163. $field_info['text_with_summary']['property_type'] = 'field_item_textsummary';
  164. $field_info['text_with_summary']['property_callbacks'][] = 'entity_metadata_field_text_property_callback';
  165. }
  166. if (module_exists('list')) {
  167. $field_info['list_integer']['property_type'] = 'integer';
  168. $field_info['list_boolean']['property_type'] = 'boolean';
  169. $field_info['list_float']['property_type'] = 'decimal';
  170. $field_info['list_text']['property_type'] = 'text';
  171. }
  172. if (module_exists('taxonomy')) {
  173. $field_info['taxonomy_term_reference']['property_type'] = 'taxonomy_term';
  174. $field_info['taxonomy_term_reference']['property_callbacks'][] = 'entity_metadata_field_term_reference_callback';
  175. }
  176. if (module_exists('file')) {
  177. // The callback specifies a custom data structure matching the file field
  178. // items. We introduce a custom type name for this data structure.
  179. $field_info['file']['property_type'] = 'field_item_file';
  180. $field_info['file']['property_callbacks'][] = 'entity_metadata_field_file_callback';
  181. }
  182. if (module_exists('image')) {
  183. // The callback specifies a custom data structure matching the image field
  184. // items. We introduce a custom type name for this data structure.
  185. $field_info['image']['property_type'] = 'field_item_image';
  186. $field_info['image']['property_callbacks'][] = 'entity_metadata_field_file_callback';
  187. $field_info['image']['property_callbacks'][] = 'entity_metadata_field_image_callback';
  188. }
  189. }
  190. /**
  191. * Implements hook_field_create_instance().
  192. * Clear the cache when a field instance changed.
  193. */
  194. function entity_field_create_instance() {
  195. entity_property_info_cache_clear();
  196. }
  197. /**
  198. * Implements hook_field_delete_instance().
  199. * Clear the cache when a field instance changed.
  200. */
  201. function entity_field_delete_instance() {
  202. entity_property_info_cache_clear();
  203. }
  204. /**
  205. * Implements hook_field_update_instance().
  206. * Clear the cache when a field instance changed.
  207. */
  208. function entity_field_update_instance() {
  209. entity_property_info_cache_clear();
  210. }
  211. /**
  212. * Verifies that the given data can be safely used as the given type regardless
  213. * of the PHP variable type of $data. Example: the string "15" is a valid
  214. * integer, but "15nodes" is not.
  215. *
  216. * @return
  217. * Whether the data is valid for the given type.
  218. */
  219. function entity_property_verify_data_type($data, $type) {
  220. // As this may be called very often statically cache the entity info using
  221. // the fast pattern.
  222. static $drupal_static_fast;
  223. if (!isset($drupal_static_fast)) {
  224. // Make use of the same static as entity info.
  225. entity_get_info();
  226. $drupal_static_fast['entity_info'] = &drupal_static('entity_get_info');
  227. }
  228. $info = &$drupal_static_fast['entity_info'];
  229. // First off check for entities, which may be represented by their ids too.
  230. if (isset($info[$type])) {
  231. if (is_object($data)) {
  232. return TRUE;
  233. }
  234. elseif (isset($info[$type]['entity keys']['name'])) {
  235. return entity_property_verify_data_type($data, 'token');
  236. }
  237. return entity_property_verify_data_type($data, empty($info[$type]['fieldable']) ? 'text' : 'integer');
  238. }
  239. switch ($type) {
  240. case 'site':
  241. case 'unknown':
  242. return TRUE;
  243. case 'date':
  244. case 'duration':
  245. case 'integer':
  246. return is_numeric($data) && strpos($data, '.') === FALSE;
  247. case 'decimal':
  248. return is_numeric($data);
  249. case 'text':
  250. return is_scalar($data);
  251. case 'token':
  252. return is_scalar($data) && preg_match('!^[a-z][a-z0-9_]*$!', $data);
  253. case 'boolean':
  254. return is_scalar($data) && (is_bool($data) || $data == 0 || $data == 1);
  255. case 'uri':
  256. return valid_url($data, TRUE);
  257. case 'list':
  258. return (is_array($data) && array_values($data) == $data) || (is_object($data) && $data instanceof EntityMetadataArrayObject);
  259. case 'entity':
  260. return is_object($data) && $data instanceof EntityDrupalWrapper;
  261. default:
  262. case 'struct':
  263. return is_object($data) || is_array($data);
  264. }
  265. }
  266. /**
  267. * Creates the entity object for an array of given property values.
  268. *
  269. * @param $entity_type
  270. * The entity type to create an entity for.
  271. * @param $values
  272. * An array of values as described by the entity's property info. All entity
  273. * properties of the given entity type that are marked as required, must be
  274. * present.
  275. * If the passed values have no matching property, their value will be
  276. * assigned to the entity directly, without the use of the metadata-wrapper
  277. * property.
  278. *
  279. * @return EntityDrupalWrapper
  280. * An EntityDrupalWrapper wrapping the newly created entity or FALSE, if
  281. * there were no information how to create the entity.
  282. */
  283. function entity_property_values_create_entity($entity_type, $values = array()) {
  284. if (entity_type_supports($entity_type, 'create')) {
  285. $info = entity_get_info($entity_type);
  286. // Create the initial entity by passing the values for all 'entity keys'
  287. // to entity_create().
  288. $entity_keys = array_filter($info['entity keys']);
  289. $creation_values = array_intersect_key($values, array_flip($entity_keys));
  290. // In case the bundle key does not match the property that sets it, ensure
  291. // the bundle key is initialized somehow, so entity_extract_ids()
  292. // does not bail out during wrapper creation.
  293. if (!empty($info['entity keys']['bundle'])) {
  294. $creation_values += array($info['entity keys']['bundle'] => FALSE);
  295. }
  296. $entity = entity_create($entity_type, $creation_values);
  297. // Now set the remaining values using the wrapper.
  298. $wrapper = entity_metadata_wrapper($entity_type, $entity);
  299. foreach ($values as $key => $value) {
  300. if (!in_array($key, $info['entity keys'])) {
  301. if (isset($wrapper->$key)) {
  302. $wrapper->$key->set($value);
  303. }
  304. else {
  305. $entity->$key = $value;
  306. }
  307. }
  308. }
  309. // @todo: Once we require Drupal 7.7 or later, verify the entity has
  310. // now a valid bundle and throw the EntityMalformedException if not.
  311. return $wrapper;
  312. }
  313. return FALSE;
  314. }
  315. /**
  316. * Extracts the contained type for a list type string like list<date>.
  317. *
  318. * @return
  319. * The contained type or FALSE, if the given type string is no list.
  320. */
  321. function entity_property_list_extract_type($type) {
  322. if (strpos($type, 'list<') === 0 && $type[strlen($type)-1] == '>') {
  323. return substr($type, 5, -1);
  324. }
  325. return FALSE;
  326. }
  327. /**
  328. * Extracts the innermost type for a type string like list<list<date>>.
  329. *
  330. * @param $type
  331. * The type to examine.
  332. *
  333. * @return
  334. * For list types, the innermost type. The type itself otherwise.
  335. */
  336. function entity_property_extract_innermost_type($type) {
  337. while (strpos($type, 'list<') === 0 && $type[strlen($type)-1] == '>') {
  338. $type = substr($type, 5, -1);
  339. }
  340. return $type;
  341. }
  342. /**
  343. * Gets the property just as it is set in the data.
  344. */
  345. function entity_property_verbatim_get($data, array $options, $name, $type, $info) {
  346. $name = isset($info['schema field']) ? $info['schema field'] : $name;
  347. if ((is_array($data) || (is_object($data) && $data instanceof ArrayAccess)) && isset($data[$name])) {
  348. return $data[$name];
  349. }
  350. elseif (is_object($data) && isset($data->$name)) {
  351. // Incorporate i18n_string translations. We may rely on the entity class
  352. // here as its usage is required by the i18n integration.
  353. if (isset($options['language']) && !empty($info['i18n string'])) {
  354. return $data->getTranslation($name, $options['language']->language);
  355. }
  356. else {
  357. return $data->$name;
  358. }
  359. }
  360. return NULL;
  361. }
  362. /**
  363. * Date values are converted from ISO strings to timestamp if needed.
  364. */
  365. function entity_property_verbatim_date_get($data, array $options, $name, $type, $info) {
  366. $name = isset($info['schema field']) ? $info['schema field'] : $name;
  367. return is_numeric($data[$name]) ? $data[$name] : strtotime($data[$name], REQUEST_TIME);
  368. }
  369. /**
  370. * Sets the property to the given value. May be used as 'setter callback'.
  371. */
  372. function entity_property_verbatim_set(&$data, $name, $value, $langcode, $type, $info) {
  373. $name = isset($info['schema field']) ? $info['schema field'] : $name;
  374. if (is_array($data) || (is_object($data) && $data instanceof ArrayAccess)) {
  375. $data[$name] = $value;
  376. }
  377. elseif (is_object($data)) {
  378. $data->$name = $value;
  379. }
  380. }
  381. /**
  382. * Gets the property using the getter method (named just like the property).
  383. */
  384. function entity_property_getter_method($object, array $options, $name) {
  385. // Remove any underscores as classes are expected to use CamelCase.
  386. $method = strtr($name, array('_' => ''));
  387. return $object->$method();
  388. }
  389. /**
  390. * Sets the property to the given value using the setter method. May be used as
  391. * 'setter callback'.
  392. */
  393. function entity_property_setter_method($object, $name, $value) {
  394. // Remove any underscores as classes are expected to use CamelCase.
  395. $method = 'set' . strtr($name, array('_' => ''));
  396. // Invoke the setProperty() method where 'Property' is the property name.
  397. $object->$method($value);
  398. }
  399. /**
  400. * Getter callback for getting an array. Makes sure it's numerically indexed.
  401. */
  402. function entity_property_get_list($data, array $options, $name) {
  403. return isset($data->$name) ? array_values($data->$name) : array();
  404. }
  405. /**
  406. * A validation callback ensuring the passed integer is positive.
  407. */
  408. function entity_property_validate_integer_positive($value) {
  409. return $value > 0;
  410. }
  411. /**
  412. * A validation callback ensuring the passed integer is non-negative.
  413. */
  414. function entity_property_validate_integer_non_negative($value) {
  415. return $value >= 0;
  416. }
  417. /**
  418. * A simple auto-creation callback for array based data structures.
  419. */
  420. function entity_property_create_array($property_name, $context) {
  421. return array();
  422. }
  423. /**
  424. * Flattens the given options in single dimensional array.
  425. * We don't depend on options module, so we cannot use options_array_flatten().
  426. *
  427. * @see options_array_flatten()
  428. */
  429. function entity_property_options_flatten($options) {
  430. $result = array();
  431. foreach ($options as $key => $value) {
  432. if (is_array($value)) {
  433. $result += $value;
  434. }
  435. else {
  436. $result[$key] = $value;
  437. }
  438. }
  439. return $result;
  440. }
  441. /**
  442. * Defines info for the properties of the text_formatted data structure.
  443. */
  444. function entity_property_text_formatted_info() {
  445. return array(
  446. 'value' => array(
  447. 'type' => 'text',
  448. 'label' => t('Text'),
  449. 'sanitized' => TRUE,
  450. 'getter callback' => 'entity_metadata_field_text_get',
  451. 'setter callback' => 'entity_property_verbatim_set',
  452. 'setter permission' => 'administer nodes',
  453. 'raw getter callback' => 'entity_property_verbatim_get',
  454. ),
  455. 'summary' => array(
  456. 'type' => 'text',
  457. 'label' => t('Summary'),
  458. 'sanitized' => TRUE,
  459. 'getter callback' => 'entity_metadata_field_text_get',
  460. 'setter callback' => 'entity_property_verbatim_set',
  461. 'setter permission' => 'administer nodes',
  462. 'raw getter callback' => 'entity_property_verbatim_get',
  463. ),
  464. 'format' => array(
  465. 'type' => 'token',
  466. 'label' => t('Text format'),
  467. 'options list' => 'entity_metadata_field_text_formats',
  468. 'getter callback' => 'entity_property_verbatim_get',
  469. ),
  470. );
  471. }
  472. /**
  473. * Defines info for the properties of the field_item_textsummary data structure.
  474. */
  475. function entity_property_field_item_textsummary_info() {
  476. return array(
  477. 'value' => array(
  478. 'type' => 'text',
  479. 'label' => t('Text'),
  480. 'setter callback' => 'entity_property_verbatim_set',
  481. ),
  482. 'summary' => array(
  483. 'type' => 'text',
  484. 'label' => t('Summary'),
  485. 'setter callback' => 'entity_property_verbatim_set',
  486. ),
  487. );
  488. }
  489. /**
  490. * Defines info for the properties of the file-field item data structure.
  491. */
  492. function entity_property_field_item_file_info() {
  493. $properties['file'] = array(
  494. 'type' => 'file',
  495. 'label' => t('The file.'),
  496. 'getter callback' => 'entity_metadata_field_file_get',
  497. 'setter callback' => 'entity_metadata_field_file_set',
  498. 'required' => TRUE,
  499. );
  500. $properties['description'] = array(
  501. 'type' => 'text',
  502. 'label' => t('The file description'),
  503. 'setter callback' => 'entity_property_verbatim_set',
  504. );
  505. $properties['display'] = array(
  506. 'type' => 'boolean',
  507. 'label' => t('Whether the file is being displayed.'),
  508. 'setter callback' => 'entity_property_verbatim_set',
  509. );
  510. return $properties;
  511. }
  512. /**
  513. * Defines info for the properties of the image-field item data structure.
  514. */
  515. function entity_property_field_item_image_info() {
  516. $properties['file'] = array(
  517. 'type' => 'file',
  518. 'label' => t('The image file.'),
  519. 'getter callback' => 'entity_metadata_field_file_get',
  520. 'setter callback' => 'entity_metadata_field_file_set',
  521. 'required' => TRUE,
  522. );
  523. $properties['alt'] = array(
  524. 'type' => 'text',
  525. 'label' => t('The "Alt" attribute text'),
  526. 'setter callback' => 'entity_property_verbatim_set',
  527. );
  528. $properties['title'] = array(
  529. 'type' => 'text',
  530. 'label' => t('The "Title" attribute text'),
  531. 'setter callback' => 'entity_property_verbatim_set',
  532. );
  533. return $properties;
  534. }
  535. /**
  536. * Previously, hook_entity_property_info() has been provided by the removed
  537. * entity metadata module. To provide backward compatibility for provided
  538. * helpers that may be specified in hook_entity_property_info(), the following
  539. * (deprecated) functions are provided.
  540. */
  541. /**
  542. * Deprecated.
  543. * Do not make use of this function, instead use the new one.
  544. */
  545. function entity_metadata_verbatim_get($data, array $options, $name) {
  546. return entity_property_verbatim_get($data, $options, $name);
  547. }
  548. /**
  549. * Deprecated.
  550. * Do not make use of this function, instead use the new one.
  551. */
  552. function entity_metadata_verbatim_set($data, $name, $value) {
  553. return entity_property_verbatim_set($data, $name, $value);
  554. }
  555. /**
  556. * Deprecated.
  557. * Do not make use of this function, instead use the new one.
  558. */
  559. function entity_metadata_getter_method($object, array $options, $name) {
  560. return entity_property_getter_method($object, $options, $name);
  561. }
  562. /**
  563. * Deprecated.
  564. * Do not make use of this function, instead use the new one.
  565. */
  566. function entity_metadata_setter_method($object, $name, $value) {
  567. entity_property_setter_method($object, $name, $value);
  568. }
  569. /**
  570. * Deprecated.
  571. * Do not make use of this function, instead use the new one.
  572. */
  573. function entity_metadata_get_list($data, array $options, $name) {
  574. return entity_property_get_list($data, $options, $name);
  575. }
  576. /**
  577. * Deprecated.
  578. * Do not make use of this function, instead use the new one.
  579. */
  580. function entity_metadata_validate_integer_positive($value) {
  581. return entity_property_validate_integer_positive($value);
  582. }
  583. /**
  584. * Deprecated.
  585. * Do not make use of this function, instead use the new one.
  586. */
  587. function entity_metadata_validate_integer_non_negative($value) {
  588. return entity_property_validate_integer_non_negative($value);
  589. }
  590. /**
  591. * Deprecated.
  592. * Do not make use of this function, instead use the new one.
  593. */
  594. function entity_metadata_text_formatted_properties() {
  595. return entity_property_text_formatted_info();
  596. }