Projet

Général

Profil

Paste
Télécharger (6,42 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_collection / field_collection.api.php @ 5e632cae

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains API documentation and examples for the Field Collection.
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
70
 * database.
71
 *
72
 * @param FieldCollectionItemEntity $field_collection_item
73
 *   The field collection item that is being inserted or updated.
74
 *
75
 * @see hook_entity_presave()
76
 */
77
function hook_field_collection_item_presave(FieldCollectionItemEntity $field_collection_item) {
78
  $field_collection_item->name = 'foo';
79
}
80

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

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

    
116
/**
117
 * Responds to field collection item archiving.
118
 *
119
 * This hook is invoked after the field collection item has been archived while
120
 * removing it from its host entity.
121
 *
122
 * @param FieldCollectionItemEntity $field_collection_item
123
 *   The field collection item that is being archived.
124
 */
125
function hook_field_collection_item_archive(FieldCollectionItemEntity $field_collection_item) {
126
  db_update('mytable')
127
    ->fields(array('archived' => 1))
128
    ->condition('id', entity_id('field_collection_item', $field_collection_item))
129
    ->execute();
130
}
131

    
132
/**
133
 * Act on a field collection item that is being assembled before rendering.
134
 *
135
 * @param $field_collection_item
136
 *   The field collection item entity.
137
 * @param $view_mode
138
 *   The view mode the field collection item is rendered in.
139
 * @param $langcode
140
 *   The language code used for rendering.
141
 *
142
 * The module may add elements to $field_collection_item->content prior to
143
 * rendering. The structure of $field_collection_item->content is a renderable
144
 * array as expected by drupal_render().
145
 *
146
 * @see hook_entity_prepare_view()
147
 * @see hook_entity_view()
148
 */
149
function hook_field_collection_item_view($field_collection_item, $view_mode, $langcode) {
150
  $field_collection_item->content['my_additional_field'] = array(
151
    '#markup' => $additional_field,
152
    '#weight' => 10,
153
    '#theme' => 'mymodule_my_additional_field',
154
  );
155
}
156

    
157
/**
158
 * Alter the results of entity_view() for field collection items.
159
 *
160
 * This hook is called after the content has been assembled in a structured
161
 * array and may be used for doing processing which requires that the complete
162
 * field collection item content structure has been built.
163
 *
164
 * If the module wishes to act on the rendered HTML of the field collection item
165
 * rather than the structured content array, it may use this hook to add a
166
 * #post_render callback. See drupal_render() and theme() documentation
167
 * respectively for details.
168
 *
169
 * @param $build
170
 *   A renderable array representing the field collection item content.
171
 *
172
 * @see hook_entity_view_alter()
173
 */
174
function hook_field_collection_item_view_alter($build) {
175
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
176
    // Change its weight.
177
    $build['an_additional_field']['#weight'] = -10;
178

    
179
    // Add a #post_render callback to act on the rendered HTML of the entity.
180
    $build['#post_render'][] = 'my_module_post_render';
181
  }
182
}
183

    
184
/**
185
 * Alter the label for a field collection.
186
 *
187
 * @param FieldCollectionItemEntity $item
188
 *   The field collection item object.
189
 * @param $host
190
 *   The host entity of the field collection item.
191
 * @param $field
192
 *   The field information about the item.
193
 *
194
 * @return $label
195
 *   A string to represent the label for this item type.
196
 */
197
function hook_field_collection_item_label($item, $host, $field) {
198
  switch ($item->field_name) {
199
    case 'field_my_first_collection':
200
      $item_wrapper = entity_metadata_wrapper('field_collection_item', $item);
201

    
202
      $title = $item_wrapper->field_title->value();
203
      $author = $item_wrapper->field_author->value();
204

    
205
      return "{$title} by {$author}";
206
  }
207
}
208

    
209

    
210
/**
211
 * @}
212
 */