Projet

Général

Profil

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

root / drupal7 / sites / all / modules / field_collection / field_collection.api.php @ a1cafe7e

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

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

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

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

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

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

    
193

    
194
/**
195
 * @}
196
 */