Projet

Général

Profil

Paste
Télécharger (1,94 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / mappers / og.inc @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * On behalf implementation of Feeds mapping API for og.module.
6
 */
7

    
8
/**
9
 * Implements hook_feeds_prevalidate().
10
 *
11
 * Set the field mode of og reference fields to 'admin' when the 'authorize'
12
 * option is not checked.
13
 */
14
function og_feeds_prevalidate(FeedsSource $source, $entity, $item, $entity_id) {
15
  // Check if 'authorize' is enabled.
16
  $processor_config = $source->importer->processor->getConfig();
17
  if (!empty($processor_config['authorize'])) {
18
    // Authorize is enabled, so we will not set the 'field_mode'.
19
    return;
20
  }
21

    
22
  $entity_type = $entity->feeds_item->entity_type;
23

    
24
  // Not a real entity.
25
  if (!entity_get_info($entity_type)) {
26
    return;
27
  }
28

    
29
  // Gather the fields that Feeds is mapping to.
30
  $feeds_fields = array();
31
  foreach ($source->importer()->processor->getMappings() as $mapping) {
32
    list($field) = explode(':', $mapping['target']);
33
    $feeds_fields[$field] = TRUE;
34
  }
35

    
36
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
37

    
38
  foreach (field_info_instances($entity_type, $bundle) as $instance) {
39
    $field_name = $instance['field_name'];
40

    
41
    // Skip fields that Feeds isn't mapping to, and empty fields.
42
    if (!isset($feeds_fields[$field_name]) || empty($entity->$field_name) || !is_array($entity->$field_name)) {
43
      continue;
44
    }
45

    
46
    $info = field_info_field($field_name);
47

    
48
    // Check if the field is an entityreference field.
49
    if ($info['type'] != 'entityreference') {
50
      // Not an entityreference field. Continue to the next field.
51
      continue;
52
    }
53

    
54
    // Check if the field is controlled by og.
55
    if (empty($info['settings']['handler']) || $info['settings']['handler'] != 'og') {
56
      // Not an og reference field. Continue to the next field.
57
      continue;
58
    }
59

    
60
    // Set 'field_mode' of this og reference to 'admin'.
61
    foreach ($entity->$field_name as $language => &$values) {
62
      foreach ($values as &$value) {
63
        $value['field_mode'] = 'admin';
64
      }
65
    }
66

    
67
  }
68

    
69
}