Projet

Général

Profil

Paste
Télécharger (5,89 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_collection / field_collection.api.php @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains API documentation and examples for the Field collection module.
6
 */
7

    
8
/**
9
 * @addtogroup hooks
10
 * @{
11
 */
12

    
13
/**
14
 * Alter whether a field collection item is considered empty.
15
 *
16
 * This hook allows modules to determine whether a field collection is empty
17
 * before it is saved.
18
 *
19
 * @param bool $is_empty
20
 *   Whether or not the field should be considered empty.
21
 * @param FieldCollectionItemEntity $item
22
 *   The field collection we are currently operating on.
23
 */
24
function hook_field_collection_is_empty_alter(&$is_empty, FieldCollectionItemEntity $item) {
25
  if (isset($item->my_field) && empty($item->my_field)) {
26
    $is_empty = TRUE;
27
  }
28
}
29

    
30
/**
31
 * Acts on field collections being loaded from the database.
32
 *
33
 * This hook is invoked during field collection item loading, which is handled
34
 * by entity_load(), via the EntityCRUDController.
35
 *
36
 * @param array $entities
37
 *   An array of field collection item entities being loaded, keyed by id.
38
 *
39
 * @see hook_entity_load()
40
 */
41
function hook_field_collection_item_load(array $entities) {
42
  $result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($entities)));
43
  foreach ($result as $record) {
44
    $entities[$record->pid]->foo = $record->foo;
45
  }
46
}
47

    
48
/**
49
 * Responds when a field collection item is inserted.
50
 *
51
 * This hook is invoked after the field collection item is inserted into the
52
 * database.
53
 *
54
 * @param FieldCollectionItemEntity $field_collection_item
55
 *   The field collection item that is being inserted.
56
 *
57
 * @see hook_entity_insert()
58
 */
59
function hook_field_collection_item_insert(FieldCollectionItemEntity $field_collection_item) {
60
  db_insert('mytable')->fields(array(
61
    'id' => entity_id('field_collection_item', $field_collection_item),
62
    'extra' => print_r($field_collection_item, TRUE),
63
  ))->execute();
64
}
65

    
66
/**
67
 * Acts on a field collection item being inserted or updated.
68
 *
69
 * This hook is invoked before the field collection item is saved to the database.
70
 *
71
 * @param FieldCollectionItemEntity $field_collection_item
72
 *   The field collection item that is being inserted or updated.
73
 *
74
 * @see hook_entity_presave()
75
 */
76
function hook_field_collection_item_presave(FieldCollectionItemEntity $field_collection_item) {
77
  $field_collection_item->name = 'foo';
78
}
79

    
80
/**
81
 * Responds to a field collection item being updated.
82
 *
83
 * This hook is invoked after the field collection item has been updated in the
84
 * database.
85
 *
86
 * @param FieldCollectionItemEntity $field_collection_item
87
 *   The field collection item that is being updated.
88
 *
89
 * @see hook_entity_update()
90
 */
91
function hook_field_collection_item_update(FieldCollectionItemEntity $field_collection_item) {
92
  db_update('mytable')
93
    ->fields(array('extra' => print_r($field_collection_item, TRUE)))
94
    ->condition('id', entity_id('field_collection_item', $field_collection_item))
95
    ->execute();
96
}
97

    
98
/**
99
 * Responds to field collection item deletion.
100
 *
101
 * This hook is invoked after the field collection item has been removed from
102
 * the database.
103
 *
104
 * @param FieldCollectionItemEntity $field_collection_item
105
 *   The field collection item that is being deleted.
106
 *
107
 * @see hook_entity_delete()
108
 */
109
function hook_field_collection_item_delete(FieldCollectionItemEntity $field_collection_item) {
110
  db_delete('mytable')
111
    ->condition('pid', entity_id('field_collection_item', $field_collection_item))
112
    ->execute();
113
}
114

    
115
/**
116
 * Act on a field collection item that is being assembled before rendering.
117
 *
118
 * @param $field_collection_item
119
 *   The field collection item entity.
120
 * @param $view_mode
121
 *   The view mode the field collection item is rendered in.
122
 * @param $langcode
123
 *   The language code used for rendering.
124
 *
125
 * The module may add elements to $field_collection_item->content prior to
126
 * rendering. The structure of $field_collection_item->content is a renderable
127
 * array as expected by drupal_render().
128
 *
129
 * @see hook_entity_prepare_view()
130
 * @see hook_entity_view()
131
 */
132
function hook_field_collection_item_view($field_collection_item, $view_mode, $langcode) {
133
  $field_collection_item->content['my_additional_field'] = array(
134
    '#markup' => $additional_field,
135
    '#weight' => 10,
136
    '#theme' => 'mymodule_my_additional_field',
137
  );
138
}
139

    
140
/**
141
 * Alter the results of entity_view() for field collection items.
142
 *
143
  * This hook is called after the content has been assembled in a structured
144
 * array and may be used for doing processing which requires that the complete
145
 * field collection item content structure has been built.
146
 *
147
 * If the module wishes to act on the rendered HTML of the field collection item
148
 * rather than the structured content array, it may use this hook to add a
149
 * #post_render callback. See drupal_render() and theme() documentation
150
 * respectively for details.
151
 *
152
 * @param $build
153
 *   A renderable array representing the field collection item content.
154
 *
155
 * @see hook_entity_view_alter()
156
 */
157
function hook_field_collection_item_view_alter($build) {
158
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
159
    // Change its weight.
160
    $build['an_additional_field']['#weight'] = -10;
161

    
162
    // Add a #post_render callback to act on the rendered HTML of the entity.
163
    $build['#post_render'][] = 'my_module_post_render';
164
  }
165
}
166

    
167
/**
168
 * Alter the label for a field collection.
169
 *
170
 * @param FieldCollectionItemEntity $item
171
 *   The field collection item object.
172
 * @param $host
173
 *   The host entity of the field collection item.
174
 * @param $field
175
 *   The field information about the item.
176
 *
177
 * @return $label
178
 *   A string to represent the label for this item type.
179
 */
180
function hook_field_collection_item_label($item, $host, $field) {
181
  switch ($item->field_name) {
182
    case 'field_my_first_collection':
183
      $item_wrapper = entity_metadata_wrapper('field_collection_item', $item);
184

    
185
      $title = $item_wrapper->field_title->value();
186
      $author = $item_wrapper->field_author->value();
187

    
188
      return "{$title} by {$author}";
189
  }
190
}
191

    
192

    
193
/**
194
 * @}
195
 */