Projet

Général

Profil

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

root / drupal7 / sites / all / modules / field_collection / field_collection.tokens.inc @ a1cafe7e

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
  $host['delta'] = array(
32
    'name' => t('Host entity item delta'),
33
    'description' => t('The delta of the reference pointing to this field collection item.'),
34
  );
35

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

    
45
  return array(
46
    'types' => array('host' => $type),
47
    'tokens' => array('host' => $host),
48
  );
49
}
50

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

    
69

    
70
/**
71
 * Implements hook_tokens().
72
 */
73
function field_collection_tokens($type, $tokens, array $data = array(), array $options = array()) {
74
  $replacements = array();
75

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

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

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

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

    
137
  if ($host_entity_types === FALSE) {
138
    $host_entity_types = array();
139

    
140
    if (function_exists('entity_get_info')) {
141
      $entity_types = entity_get_info();
142
    }
143

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