flag.info.inc 3.7 KB

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