flag_entity.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * @file
  4. * Contains the flag_entity class.
  5. */
  6. /**
  7. * Base entity flag handler.
  8. */
  9. class flag_entity extends flag_flag {
  10. /**
  11. * Adds additional options that are common for all entity types.
  12. */
  13. function options() {
  14. $options = parent::options();
  15. $options += array(
  16. // Output the flag in the entity links.
  17. // This is empty for now and will get overriden for different
  18. // entities.
  19. // @see hook_entity_view().
  20. 'show_in_links' => array(),
  21. // Output the flag as individual pseudofields.
  22. 'show_as_field' => FALSE,
  23. // Add a checkbox for the flag in the entity form.
  24. // @see hook_field_attach_form().
  25. 'show_on_form' => FALSE,
  26. 'access_author' => '',
  27. 'show_contextual_link' => FALSE,
  28. );
  29. return $options;
  30. }
  31. /**
  32. * Options form extras for the generic entity flag.
  33. */
  34. function options_form(&$form) {
  35. $bundles = array();
  36. $entity_info = entity_get_info($this->entity_type);
  37. foreach ($entity_info['bundles'] as $bundle_key => $bundle) {
  38. $bundles[$bundle_key] = check_plain($bundle['label']);
  39. }
  40. $form['access']['types'] = array(
  41. '#type' => 'checkboxes',
  42. '#title' => t('Bundles'),
  43. '#options' => $bundles,
  44. '#description' => t('Select the bundles that this flag may be used on. Leave blank to allow on all bundles for the entity type.'),
  45. '#default_value' => $this->types,
  46. );
  47. // Add checkboxes to show flag link on each entity view mode.
  48. $options = array();
  49. $defaults = array();
  50. $entity_view_modes = $entity_info['view modes'];
  51. foreach ($entity_view_modes as $name => $view_mode) {
  52. $options[$name] = t('Display on @name view mode', array('@name' => $view_mode['label']));
  53. $defaults[$name] = !empty($this->show_in_links[$name]) ? $name : 0;
  54. }
  55. // Select the first display option by default if this is a new flag.
  56. if (empty($this->fid)) {
  57. $first_view_mode_keys = array_keys($entity_view_modes);
  58. $first_view_mode = reset($first_view_mode_keys);
  59. $defaults[$first_view_mode] = $first_view_mode;
  60. }
  61. $form['display']['show_in_links'] = array(
  62. '#type' => 'checkboxes',
  63. '#title' => t('Display in entity links'),
  64. '#description' => t('Show the flag link with the other links on the entity.'),
  65. '#options' => $options,
  66. '#default_value' => $defaults,
  67. );
  68. $form['display']['show_as_field'] = array(
  69. '#type' => 'checkbox',
  70. '#title' => t('Display link as field'),
  71. '#description' => t('Show the flag link as a pseudofield, which can be ordered among other entity elements in the "Manage display" settings for the entity type.'),
  72. '#default_value' => isset($this->show_as_field) ? $this->show_as_field : TRUE,
  73. );
  74. if (empty($entity_info['fieldable'])) {
  75. $form['display']['show_as_field']['#disabled'] = TRUE;
  76. $form['display']['show_as_field']['#description'] = t("This entity type is not fieldable.");
  77. }
  78. $form['display']['show_on_form'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => t('Display checkbox on entity edit form'),
  81. '#default_value' => $this->show_on_form,
  82. '#weight' => 5,
  83. );
  84. // We use FieldAPI to put the flag checkbox on the entity form, so therefore
  85. // require the entity to be fielable. Since this is a potential DX
  86. // headscratcher for a developer wondering where this option has gone,
  87. // we disable it and explain why.
  88. if (empty($entity_info['fieldable'])) {
  89. $form['display']['show_on_form']['#disabled'] = TRUE;
  90. $form['display']['show_on_form']['#description'] = t('This is only possible on entities which are fieldable.');
  91. }
  92. $form['display']['show_contextual_link'] = array(
  93. '#type' => 'checkbox',
  94. '#title' => t('Display in contextual links'),
  95. '#default_value' => $this->show_contextual_link,
  96. '#description' => t('Note that not all entity types support contextual links.'),
  97. '#access' => module_exists('contextual'),
  98. '#weight' => 10,
  99. );
  100. }
  101. /**
  102. * Loads the entity object.
  103. */
  104. function _load_entity($entity_id) {
  105. if (is_numeric($entity_id)) {
  106. $entity = entity_load($this->entity_type, array($entity_id));
  107. return reset($entity);
  108. }
  109. return NULL;
  110. }
  111. /**
  112. * Checks whether the flag applies for the current entity bundle.
  113. */
  114. function applies_to_entity($entity) {
  115. $entity_info = entity_get_info($this->entity_type);
  116. // The following conditions are applied:
  117. // - if the types array is empty, the flag applies to all bundles and thus
  118. // to this entity.
  119. // - if the entity has no bundles, the flag applies to the entity.
  120. // - if the entity's bundle is in the list of types.
  121. if (empty($this->types) || empty($entity_info['entity keys']['bundle']) || in_array($entity->{$entity_info['entity keys']['bundle']}, $this->types)) {
  122. return TRUE;
  123. }
  124. return FALSE;
  125. }
  126. /**
  127. * Provides permissions for this flag.
  128. *
  129. * @return
  130. * An array of permissions for hook_permission().
  131. */
  132. function get_permissions() {
  133. // For entity flags, use the human label of the entity.
  134. $entity_info = entity_get_info($this->entity_type);
  135. $entity_label = $entity_info['label'];
  136. return array(
  137. "flag $this->name" => array(
  138. 'title' => t('Flag %entity entities as %flag_title', array(
  139. '%flag_title' => $this->title,
  140. '%entity' => $entity_label,
  141. )),
  142. ),
  143. "unflag $this->name" => array(
  144. 'title' => t('Unflag %entity entities as %flag_title', array(
  145. '%flag_title' => $this->title,
  146. '%entity' => $entity_label,
  147. )),
  148. ),
  149. );
  150. }
  151. /**
  152. * Invoke a Rules event in reaction to a flagging or unflagging.
  153. *
  154. * @param $action
  155. * Either 'flag' or 'unflag'.
  156. * @param $flagging
  157. * The flagging entity that is either newly created or about to be deleted.
  158. * @param $entity_id
  159. * The entity ID of entity being flagged or unflagged.
  160. * @param $account
  161. * The account performing the action.
  162. */
  163. protected function invoke_rules_event($action, $flagging, $entity_id, $account) {
  164. switch ($action) {
  165. case 'flag':
  166. $event_name = 'flag_flagged_' . $this->name;
  167. break;
  168. case 'unflag':
  169. $event_name = 'flag_unflagged_' . $this->name;
  170. break;
  171. }
  172. $variables = array(
  173. 'flag' => $this,
  174. 'flagged_' . $this->entity_type => $entity_id,
  175. 'flagging_user' => $account,
  176. 'flagging' => $flagging,
  177. );
  178. rules_invoke_event_by_args($event_name, $variables);
  179. }
  180. /**
  181. * Returns the entity id, if it already exists.
  182. */
  183. function get_entity_id($entity) {
  184. $entity_info = entity_get_info($this->entity_type);
  185. if ($entity && isset($entity->{$entity_info['entity keys']['id']})) {
  186. return $entity->{$entity_info['entity keys']['id']};
  187. }
  188. }
  189. /**
  190. * Determine whether the flag should show a flag link in entity links.
  191. */
  192. function shows_in_entity_links($view_mode) {
  193. // Check for settings for the given view mode.
  194. if (isset($this->show_in_links[$view_mode])) {
  195. return (bool) $this->show_in_links[$view_mode];
  196. }
  197. return FALSE;
  198. }
  199. /**
  200. * Returns token types for the current entity type.
  201. */
  202. function get_labels_token_types() {
  203. // The token type name might be different to the entity type name. If so,
  204. // an own flag entity handler can be used for overriding this.
  205. $entity_info = entity_get_info($this->entity_type);
  206. if (isset($entity_info['token type'])) {
  207. return array_merge(array($entity_info['token type']), parent::get_labels_token_types());
  208. }
  209. else {
  210. return array_merge(array($this->entity_type), parent::get_labels_token_types());
  211. }
  212. }
  213. /**
  214. * Replaces tokens.
  215. */
  216. function replace_tokens($label, $contexts, $options, $entity_id) {
  217. if ($entity_id && ($entity = $this->fetch_entity($entity_id))) {
  218. $contexts[$this->entity_type] = $entity;
  219. }
  220. return parent::replace_tokens($label, $contexts, $options, $entity_id);
  221. }
  222. /**
  223. * Returns a 'flag action' object.
  224. */
  225. function get_flag_action($entity_id) {
  226. $flag_action = parent::get_flag_action($entity_id);
  227. $entity = $this->fetch_entity($entity_id);
  228. $flag_action->content_title = entity_label($this->entity_type, $entity);
  229. $flag_action->content_url = $this->_flag_url($this->entity_type . '/' . $this->get_entity_id($entity));
  230. return $flag_action;
  231. }
  232. /**
  233. * Returns objects the action may possible need.
  234. */
  235. function get_relevant_action_objects($entity_id) {
  236. return array(
  237. $this->entity_type => $this->fetch_entity($entity_id),
  238. );
  239. }
  240. /**
  241. * Returns information for the Views integration.
  242. */
  243. function get_views_info() {
  244. $entity_info = entity_get_info($this->entity_type);
  245. if (!isset($entity_info['base table'])) {
  246. return NULL;
  247. }
  248. return array(
  249. 'views table' => $entity_info['base table'],
  250. 'join field' => $entity_info['entity keys']['id'],
  251. 'title field' => isset($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : '',
  252. 'title' => t('@entity_label flag', array('@entity_label' => $entity_info['label'])),
  253. 'help' => t('Limit results to only those entity flagged by a certain flag; Or display information about the flag set on a entity.'),
  254. 'counter title' => t('@entity_label flag counter', array('@entity_label' => $entity_info['label'])),
  255. 'counter help' => t('Include this to gain access to the flag counter field.'),
  256. );
  257. }
  258. }