fivestar.api.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Provides API documentation for the fivestar module.
  5. */
  6. /**
  7. * Implements hook_fivestar_widgets().
  8. *
  9. * This hook allows other modules to create additional custom widgets for
  10. * the fivestar module.
  11. *
  12. * @return array
  13. * An array of key => value pairs suitable for inclusion as the #options in a
  14. * select or radios form element. Each key must be the location of a css
  15. * file for a fivestar widget. Each value should be the name of the widget.
  16. *
  17. * @see fivestar_fivestar_widgets()
  18. */
  19. function hook_fivestar_widgets() {
  20. // Letting fivestar know about my Cool and Awesome Stars.
  21. $widgets = array(
  22. 'path/to/my/awesome/fivestar/css.css' => 'Awesome Stars',
  23. 'path/to/my/cool/fivestar/css.css' => 'Cool Stars',
  24. );
  25. return $widgets;
  26. }
  27. /**
  28. * Implements hook_fivestar_access().
  29. *
  30. * This hook is called before every vote is cast through Fivestar. It allows
  31. * modules to allow or deny voting on any type of entity, such as nodes, users,
  32. * or comments.
  33. *
  34. * @param $entity_type
  35. * Type entity.
  36. * @param $id
  37. * Identifier within the type.
  38. * @param $tag
  39. * The VotingAPI tag string.
  40. * @param $uid
  41. * The user ID trying to cast the vote.
  42. *
  43. * @return bool|null
  44. * Returns TRUE if voting is supported on this object.
  45. * Returns NULL if voting is not supported on this object by this module.
  46. * If needing to absolutely deny all voting on this object, regardless
  47. * of permissions defined in other modules, return FALSE. Note if all
  48. * modules return NULL, stating no preference, then access will be denied.
  49. *
  50. * @see fivestar_validate_target()
  51. * @see fivestar_fivestar_access()
  52. */
  53. function hook_fivestar_access($entity_type, $id, $tag, $uid) {
  54. if ($uid == 1) {
  55. // We are never going to allow the admin user case a fivestar vote.
  56. return FALSE;
  57. }
  58. }
  59. /**
  60. * Implements hook_fivestar_target_info().
  61. *
  62. * @param $field
  63. * The field structure for the operation.
  64. * @param $instance
  65. * The instance structures for the $field.
  66. *
  67. * @return array
  68. * An array of key => value pairs. Each key must be unique the identifier for
  69. * this target selection. The Value is an array of key => value pairs for a
  70. * title and a callback function. The title value is used for displaying in
  71. * the #options array of the target selection option. The callback function
  72. * is used when trying to decided which target the current vote should be
  73. * cast against.
  74. *
  75. * @see fivestar_get_targets()
  76. * @see fivestar_fivestar_target_info()
  77. */
  78. function hook_fivestar_target_info($field, $instance) {
  79. $entity_type = $instance['entity_type'];
  80. $bundle = $instance['bundle'];
  81. $options = array(
  82. // Declase a new Target Type.
  83. // This will allow users to vote on a Node and have the vote cast against the
  84. // node's author instead of the actual node.
  85. 'example_node_author' => array(
  86. 'title' => t('Node Author'),
  87. 'callback' => '_example_target_node_author',
  88. ),
  89. );
  90. return $options;
  91. }
  92. /**
  93. * Define a custom voting behavior for this target selection type.
  94. *
  95. * Invoked from fivestar_get_targets().
  96. *
  97. * @param $entity
  98. * The entity for the operation.
  99. * @param $field
  100. * The field structure for the operation.
  101. * @param $instance
  102. * The instance structure for $field on $entity's bundle.
  103. * @param $langcode
  104. * The language associated with $items.
  105. *
  106. * @return array
  107. * An array of key => value pairs. The return array must contain an entity_id
  108. * key and a entity_type key. The value os the entity_id and entity_type is
  109. * what the fivestar vote is going to be cast against when a user has selected
  110. * this option as the target selection.
  111. *
  112. * @see _fivestar_target_comment_parent_node()
  113. * @see _fivestar_target_node_reference()
  114. * @see fivestar_get_targets()
  115. * @see hook_fivestar_target_info()
  116. */
  117. function _example_target_node_author($entity, $field, $instance, $langcode) {
  118. $target = array(
  119. 'entity_id' => 2,
  120. 'entity_type' => 'user',
  121. );
  122. return $target;
  123. }