flag.info.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @file
  4. * Contains Entity API property information.
  5. */
  6. /**
  7. * Implements hook_entity_property_info_alter().
  8. *
  9. * We add properties thus:
  10. * - global flags:
  11. * - entity->flag_FLAGNAME, boolean.
  12. * - per-user flags:
  13. * - entity->flag_FLAGNAME_users, list of users.
  14. * - user->flag_FLAGNAME_flagged, list of user's flagged entities.
  15. */
  16. function flag_entity_property_info_alter(&$info) {
  17. foreach (flag_get_flags() as $flag) {
  18. if ($flag->global) {
  19. // Global flags.
  20. // Boolean property on entity type.
  21. // This can go on either the entity as a whole, or on bundles, depending
  22. // on whether the flag is limited by bundle.
  23. $property_definition = array(
  24. 'label' => t('Whether the entity is flagged with flag @flag', array(
  25. '@flag' => $flag->name,
  26. )),
  27. 'description' => t('Whether the entity is flagged with flag @flag.', array(
  28. '@flag' => $flag->name,
  29. )),
  30. 'type' => 'boolean',
  31. 'getter callback' => 'flag_properties_get_flagging_boolean',
  32. 'computed' => TRUE,
  33. 'flag_name' => $flag->name,
  34. );
  35. if (count($flag->types)) {
  36. // Bundle specific property.
  37. foreach ($flag->types as $type) {
  38. $info[$flag->entity_type]['bundles'][$type]['properties']['flag_' . $flag->name] = $property_definition;
  39. }
  40. }
  41. else {
  42. // Generic property, applies for all bundles.
  43. $info[$flag->entity_type]['properties']['flag_' . $flag->name] = $property_definition;
  44. }
  45. }
  46. else {
  47. // Per-user flags.
  48. // User property: list of flagged entities by the user.
  49. $info['user']['properties']['flag_' . $flag->name . '_flagged'] = array(
  50. 'label' => t('Flagged @entity-type with flag @flag', array(
  51. '@entity-type' => $flag->entity_type,
  52. '@flag' => $flag->name,
  53. )),
  54. 'description' => t('Returns a list of entities a user flagged with flag @flag.', array(
  55. '@flag' => $flag->name,
  56. )),
  57. 'type' => 'list<' . $flag->entity_type . '>',
  58. 'getter callback' => 'flag_properties_get_flagged_entities',
  59. 'computed' => TRUE,
  60. 'flag_name' => $flag->name,
  61. 'flag_entity_type' => $flag->entity_type,
  62. );
  63. $info['user']['properties']['flag_sid'] = array(
  64. 'label' => t('Flag user session identifier'),
  65. 'description' => t('Returns the sessions id used to distinguish anonymous users from each other.'),
  66. 'type' => 'text',
  67. 'getter callback' => 'flag_properties_get_user_sid',
  68. 'computed' => TRUE,
  69. );
  70. // Entity property: list of users who have flagged this entity.
  71. // This can go on either the entity as a whole, or on bundles, depending
  72. // on whether the flag is limited by bundle.
  73. $property_definition = array(
  74. 'label' => t('Users who flagged the entity with flag @flag', array(
  75. '@flag' => $flag->name,
  76. )),
  77. 'description' => t('Returns a list of users who flagged an entity with flag @flag.', array(
  78. '@flag' => $flag->name,
  79. )),
  80. 'type' => 'list<user>',
  81. 'getter callback' => 'flag_properties_get_flagging_users',
  82. 'computed' => TRUE,
  83. 'flag_name' => $flag->name,
  84. );
  85. if (count($flag->types)) {
  86. // Bundle specific property.
  87. foreach ($flag->types as $type) {
  88. $info[$flag->entity_type]['bundles'][$type]['properties']['flag_' . $flag->name . '_user'] = $property_definition;
  89. }
  90. }
  91. else {
  92. // Generic property, applies for all bundles.
  93. $info[$flag->entity_type]['properties']['flag_' . $flag->name . '_user'] = $property_definition;
  94. }
  95. }
  96. }
  97. }