Projet

Général

Profil

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

root / drupal7 / sites / all / modules / field_collection / field_collection.tokens.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides host entity tokens for field_collection.module
6
 */
7

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function field_collection_token_info() {
12
  $type = array(
13
    'name' => t('Field collection host entity'),
14
    'description' => t('Tokens related to field collection host entities.'),
15
    'needs-data' => 'field_collection_item',
16
  );
17

    
18
  // Simple tokens.
19
  $host['type'] = array(
20
    'name' => t('Host entity type'),
21
    'description' => t('The entity type of the host. Common types are <em>node</em> and <em>user</em>.'),
22
  );
23
  $host['bundle'] = array(
24
    'name' => t('Host entity bundle'),
25
    'description' => t('For <em>node</em> entity types this is the content type, otherwise available as <code>[node:content-type:machine-name]</code>.'),
26
  );
27
  $host['id'] = array(
28
    'name' => t('Host entity ID'),
29
    'description' => t('The entity ID of the host. For nodes this is <code>nid</code>, for users <code>uid</code>.'),
30
  );
31

    
32
  // Chained tokens.
33
  foreach (field_collection_host_entity_types() as $entity_type => $entity_info) {
34
    $host[$entity_type] = array(
35
      'name' => t('Entity: @entity_type', array('@entity_type' => $entity_info['label'])),
36
      'description' => t('Host entity tokens when it is of type %entity_type', array('%entity_type' => $entity_info['label'])),
37
      'type' => $entity_type,
38
    );
39
  }
40

    
41
  return array(
42
    'types' => array('host' => $type),
43
    'tokens' => array('host' => $host),
44
  );
45
}
46

    
47
/**
48
 * Implements hook_token_info_alter().
49
 *
50
 * Inject an additional 'host' token to the 'field_collection_item' token type.
51
 */
52
function field_collection_token_info_alter(&$data) {
53
  $data['types']['field_collection_item'] = array(
54
    'name' => t('Field collection'),
55
    'description' => t('Tokens related to field collection.'),
56
    'needs-data' => 'field_collection_item',
57
  );
58
  $data['tokens']['field_collection_item']['host'] = array(
59
    'name' => t('Host entity'),
60
    'description' => t('The host entity of this field collection item.'),
61
    'type' => 'host',
62
  );
63
}
64

    
65

    
66
/**
67
 * Implements hook_tokens().
68
 */
69
function field_collection_tokens($type, $tokens, array $data = array(), array $options = array()) {
70
  $replacements = array();
71

    
72
  // Provide a complete set of tokens for type == 'host', and a supplementary
73
  // token 'host' for type == 'field_collection_item'.
74
  if (($type == 'field_collection_item' or $type == 'host') and !empty($data['field_collection_item'])) {
75
    $collection = $data['field_collection_item'];
76
    // When saving revisions, only $collection->original has valid state about
77
    // its host entity.
78
    if (!empty($collection->original)) {
79
      $collection = $collection->original;
80
    }
81

    
82
    if ($type == 'field_collection_item') {
83
      if (!empty($tokens['host'])) {
84
        $replacements[$tokens['host']] = $collection->hostEntityId();
85
      }
86
      if ($host_tokens = token_find_with_prefix($tokens, 'host')) {
87
        $replacements += token_generate('host', $host_tokens, $data, $options);
88
      }
89
    }
90

    
91
    // $type == 'host'
92
    else {
93
      // Mapping between token and the FieldCollectionItemEntity method used to
94
      // retrieve the token with.
95
      $token_method_map = array(
96
        'type' => 'hostEntityType',
97
        'bundle' => 'hostEntityBundle',
98
        'id' => 'hostEntityId',
99
      );
100
      $entity_types = field_collection_host_entity_types();
101
      foreach ($tokens as $name => $orig) {
102
        if (isset($token_method_map[$name])) {
103
          $replacements[$orig] = $collection->{$token_method_map[$name]}();
104
        }
105
        // This replaces e.g. [host:node] and [host:user] with their respective
106
        // nid and uid.
107
        if (!empty($entity_types[$name])) {
108
          $replacements[$orig] = $collection->hostEntityId();
109
        }
110
      }
111
      foreach ($entity_types as $entity_type => $entity_info) {
112
        if ($entity_tokens = token_find_with_prefix($tokens, $entity_type)) {
113
          $host = $collection->hostEntity();
114
          $replacements += token_generate($entity_type, $entity_tokens, array($entity_type => $host), $options);
115
        }
116
      }
117
    }
118
  }
119
  return $replacements;
120
}
121

    
122
/**
123
 * Entity types that serve as host for field collections.
124
 *
125
 * @return array
126
 *   The list of entities as provided by entity_get_info(), filtered by field
127
 *   collection usage.
128
 */
129
function field_collection_host_entity_types() {
130
  $host_entity_types = &drupal_static(__FUNCTION__, FALSE);
131

    
132
  if ($host_entity_types === FALSE) {
133
    $host_entity_types = array();
134

    
135
    if (function_exists('entity_get_info')) {
136
      $entity_types = entity_get_info();
137
    }
138

    
139
    // Look for all field instances, filter them by type == 'field_collection'
140
    // and map the entity type it's connected to to the returned list.
141
    foreach (field_info_field_map() as $field_instance) {
142
      if ($field_instance['type'] == 'field_collection') {
143
        foreach (array_keys($field_instance['bundles']) as $entity_type) {
144
          if (!isset($host_entity_types[$entity_type])) {
145
            // No need to test for existence in $entity_types. If it's not there
146
            // your site is broken.
147
            $host_entity_types[$entity_type] = $entity_types[$entity_type];
148
          }
149
        }
150
      }
151
    }
152
  }
153
  return $host_entity_types;
154
}