
So say you have to build a custom sitemap for a Drupal 7 site that makes heavy use of Field Collections, and you need to recursively extract every item of a certain field type on a DíA” single entity for the sitemap. Oh, and you need to wholesale mlb jerseys be able to selectively transform some items in the process.
Yep, you guessed it – I’ve run across this one before. We had a series of content types + that used heavily-nested field collections and a custom rebooted! sitemap requirement for the images that good ol’ XML Sitemap just wouldn’t accommodate. So we built one. ? Note wholesale nfl jerseys that this one was geared toward grabbing images and altering the Brightcove module’s thumbnails to conform to our sitemap code’s requirements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/**
* Implements test callback for function
*/
function _MODULE_test_node_fields($node, $field_type) {
$items = MODULE_get_node_fields($node, ‘node’, $node->type, $field_type);
if (function_exists(‘kpr’)) {
return kpr($items, TRUE, $field_type);
}
else {
return ‘<pre>’ . print_r($items, TRUE) . ‘</pre>’;
}
}
/**
* Function for recursive traversal of fields on an entity
*/
function MODULE_get_node_fields($entity, $entity_type, $bundle_name, $field_type) {
$ret_items = array();
// Retrieve all field information for this entity type + bundle and iterate over fields
$fields = field_info_instances($entity_type, $bundle_name);
//dpm($fields, $entity_type . ‘ : ‘ . $bundle_name);
foreach (array_keys($fields) as $field_name) {
// Attempt to retrieve values for field
$items = field_get_items($entity_type, $entity, $field_name);
if ($items) {
// Retrieve detailed field info and see if this is the field type we’re searching for
$field = field_info_field($field_name);
if ($field_type == $field[‘type’]) {
// Call alter hooks to transform item data, if necessary and available
drupal_alter(‘MODULE_get_node_fields_items_’ . $field_type, $items, $field, $fields[$field_name]);
$ret_items = array_merge($ret_items, $items);
}
// If this is a field collection, recurse into it
elseif (‘field_collection’ == $field[‘type’]) {
foreach ($items as $item) {
$fc_entity = field_collection_item_load($item[‘value’]);
$ret_items = array_merge($ret_items, _MODULE_get_node_fields($fc_entity, ‘field_collection_item’, $field_name, $field_type));
}
}
}
}
return $ret_items;
}
/**
* Implements hook_MODULE_get_node_fields_item_FIELDTYPE_alter
*/
function MODULE_MODULE_get_node_fields_items_brightcove_field_alter(&$items, $field, $field_info) {
if (BRIGHTCOVE_VIDEO_WIDGET == $field_info[‘widget’][‘type’]) {
foreach ($items as &$item) {
$item = brightcove_video_load($item[‘brightcove_id’]);
}
}
elseif (BRIGHTCOVE_PLAYLIST_WIDGET == $field_info[‘widget’][‘type’]) {
$videos = array();
foreach ($items as $item) {
$playlist = brightcove_playlist_load($item[‘brightcove_id’]);
$videos = array_merge($videos, $playlist->videos);
}
$items = $videos;
}
}
|