field_collection.api.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Contains API documentation and examples for the Field collection module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Alter whether a field collection item is considered empty.
  12. *
  13. * This hook allows modules to determine whether a field collection is empty
  14. * before it is saved.
  15. *
  16. * @param boolean $empty
  17. * Whether or not the field should be considered empty.
  18. * @param FieldCollectionItemEntity $item
  19. * The field collection we are currently operating on.
  20. */
  21. function hook_field_collection_is_empty_alter(&$is_empty, FieldCollectionItemEntity $item) {
  22. if (isset($item->my_field) && empty($item->my_field)) {
  23. $is_empty = TRUE;
  24. }
  25. }
  26. /**
  27. * Acts on field collections being loaded from the database.
  28. *
  29. * This hook is invoked during field collection item loading, which is handled
  30. * by entity_load(), via the EntityCRUDController.
  31. *
  32. * @param array $entities
  33. * An array of field collection item entities being loaded, keyed by id.
  34. *
  35. * @see hook_entity_load()
  36. */
  37. function hook_field_collection_item_load(array $entities) {
  38. $result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($entities)));
  39. foreach ($result as $record) {
  40. $entities[$record->pid]->foo = $record->foo;
  41. }
  42. }
  43. /**
  44. * Responds when a field collection item is inserted.
  45. *
  46. * This hook is invoked after the field collection item is inserted into the
  47. * database.
  48. *
  49. * @param FieldCollectionItemEntity $field_collection_item
  50. * The field collection item that is being inserted.
  51. *
  52. * @see hook_entity_insert()
  53. */
  54. function hook_field_collection_item_insert(FieldCollectionItemEntity $field_collection_item) {
  55. db_insert('mytable')->fields(array(
  56. 'id' => entity_id('field_collection_item', $field_collection_item),
  57. 'extra' => print_r($field_collection_item, TRUE),
  58. ))->execute();
  59. }
  60. /**
  61. * Acts on a field collection item being inserted or updated.
  62. *
  63. * This hook is invoked before the field collection item is saved to the database.
  64. *
  65. * @param FieldCollectionItemEntity $field_collection_item
  66. * The field collection item that is being inserted or updated.
  67. *
  68. * @see hook_entity_presave()
  69. */
  70. function hook_field_collection_item_presave(FieldCollectionItemEntity $field_collection_item) {
  71. $field_collection_item->name = 'foo';
  72. }
  73. /**
  74. * Responds to a field collection item being updated.
  75. *
  76. * This hook is invoked after the field collection item has been updated in the
  77. * database.
  78. *
  79. * @param FieldCollectionItemEntity $field_collection_item
  80. * The field collection item that is being updated.
  81. *
  82. * @see hook_entity_update()
  83. */
  84. function hook_field_collection_item_update(FieldCollectionItemEntity $field_collection_item) {
  85. db_update('mytable')
  86. ->fields(array('extra' => print_r($field_collection_item, TRUE)))
  87. ->condition('id', entity_id('field_collection_item', $field_collection_item))
  88. ->execute();
  89. }
  90. /**
  91. * Responds to field collection item deletion.
  92. *
  93. * This hook is invoked after the field collection item has been removed from
  94. * the database.
  95. *
  96. * @param FieldCollectionItemEntity $field_collection_item
  97. * The field collection item that is being deleted.
  98. *
  99. * @see hook_entity_delete()
  100. */
  101. function hook_field_collection_item_delete(FieldCollectionItemEntity $field_collection_item) {
  102. db_delete('mytable')
  103. ->condition('pid', entity_id('field_collection_item', $field_collection_item))
  104. ->execute();
  105. }
  106. /**
  107. * Act on a field collection item that is being assembled before rendering.
  108. *
  109. * @param $field_collection_item
  110. * The field collection item entity.
  111. * @param $view_mode
  112. * The view mode the field collection item is rendered in.
  113. * @param $langcode
  114. * The language code used for rendering.
  115. *
  116. * The module may add elements to $field_collection_item->content prior to
  117. * rendering. The structure of $field_collection_item->content is a renderable
  118. * array as expected by drupal_render().
  119. *
  120. * @see hook_entity_prepare_view()
  121. * @see hook_entity_view()
  122. */
  123. function hook_field_collection_item_view($field_collection_item, $view_mode, $langcode) {
  124. $field_collection_item->content['my_additional_field'] = array(
  125. '#markup' => $additional_field,
  126. '#weight' => 10,
  127. '#theme' => 'mymodule_my_additional_field',
  128. );
  129. }
  130. /**
  131. * Alter the results of entity_view() for field collection items.
  132. *
  133. * This hook is called after the content has been assembled in a structured
  134. * array and may be used for doing processing which requires that the complete
  135. * field collection item content structure has been built.
  136. *
  137. * If the module wishes to act on the rendered HTML of the field collection item
  138. * rather than the structured content array, it may use this hook to add a
  139. * #post_render callback. See drupal_render() and theme() documentation
  140. * respectively for details.
  141. *
  142. * @param $build
  143. * A renderable array representing the field collection item content.
  144. *
  145. * @see hook_entity_view_alter()
  146. */
  147. function hook_field_collection_item_view_alter($build) {
  148. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  149. // Change its weight.
  150. $build['an_additional_field']['#weight'] = -10;
  151. // Add a #post_render callback to act on the rendered HTML of the entity.
  152. $build['#post_render'][] = 'my_module_post_render';
  153. }
  154. }
  155. /**
  156. * Alter the label for a field collection.
  157. *
  158. * @param FieldCollectionItemEntity $item
  159. * The field collection item object.
  160. * @param $host
  161. * The host entity of the field collection item.
  162. * @param $field
  163. * The field information about the item.
  164. *
  165. * @return $label
  166. * A string to represent the label for this item type.
  167. */
  168. function hook_field_collection_item_label($item, $host, $field) {
  169. switch ($item->field_name) {
  170. case 'field_my_first_collection':
  171. $item_wrapper = entity_metadata_wrapper('field_collection_item', $item);
  172. $title = $item_wrapper->field_title->value();
  173. $author = $item_wrapper->field_author->value();
  174. return "{$title} by {$author}";
  175. }
  176. }
  177. /**
  178. * @}
  179. */