Projet

Général

Profil

Paste
Télécharger (8,21 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / theme / entity.theme.inc @ a2bb1a14

1
<?php
2

    
3
/**
4
 * @file
5
 * Holds entity module's theme functions.
6
 */
7

    
8
/**
9
 * Returns HTML for an entity property.
10
 *
11
 * This is the default theme implementation to display the value of a property.
12
 * This function can be overridden with varying levels of specificity. For
13
 * example, for a property named 'title' displayed on the 'article' bundle,
14
 * any of the following functions will override this default implementation.
15
 * The first of these functions that exists is used:
16
 * - THEMENAME_property__body__article()
17
 * - THEMENAME_property__article()
18
 * - THEMENAME_property__body()
19
 * - THEMENAME_property()
20
 *
21
 * @param $variables
22
 *   An associative array containing:
23
 *   - label: A boolean indicating to show or hide the property label.
24
 *   - title_attributes: A string containing the attributes for the title.
25
 *   - label: The label for the property.
26
 *   - content_attributes: A string containing the attributes for the content's
27
 *     div.
28
 *   - content: The rendered property value.
29
 *   - attributes: A string containing the attributes for the wrapping div.
30
 *
31
 * @ingroup themeable
32
 */
33
function theme_entity_property($variables) {
34
  $output = '';
35

    
36
  // Render the label, if it's not hidden.
37
  if (!$variables['label_hidden']) {
38
    $output .= '<div' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
39
  }
40

    
41
  // Render the content.
42
  $content_suffix = '';
43
  if (!$variables['label_hidden'] || $variables['content_attributes']) {
44
    $output .= '<div' . $variables['content_attributes'] . '>';
45
    $content_suffix = '</div>';
46
  }
47
  $output .= $variables['content'] . $content_suffix;
48

    
49
  // Render the top-level DIV.
50
  return '<div' . $variables['attributes'] . '>' . $output . '</div>';
51
}
52

    
53
/**
54
 * Theme preprocess function for theme_entity_property().
55
 *
56
 * @see theme_entity_property()
57
 */
58
function template_preprocess_entity_property(&$variables, $hook) {
59
  $element = $variables['elements'];
60

    
61
  $variables += array(
62
    'theme_hook_suggestions' => array(),
63
    'attributes_array' => array(),
64
  );
65
  // Generate variables from element properties.
66
  foreach (array('label_hidden', 'label', 'property_name') as $name) {
67
    $variables[$name] = check_plain($element['#' . $name]);
68
  }
69
  $variables['title_attributes_array']['class'][] = 'entity-property-label';
70
  $variables['attributes_array'] = array_merge($variables['attributes_array'], isset($element['#attributes']) ? $element['#attributes'] : array());
71

    
72
  $variables['property_name_css'] = strtr($element['#property_name'], '_', '-');
73
  $variables['attributes_array']['class'][] = 'entity-property';
74
  $variables['attributes_array']['class'][] = 'entity-property-' . $variables['property_name_css'];
75

    
76
  // Add specific suggestions that can override the default implementation.
77
  $variables['theme_hook_suggestions'] += array(
78
    'entity_property__' . $element['#property_name'],
79
    'entity_property__' . $element['#entity_type'] . '__' . $element['#property_name'],
80
  );
81

    
82
  // Populate the content with sensible defaults.
83
  if (!isset($element['#content'])) {
84
    $variables['content'] = entity_property_default_render_value_by_type($element['#entity_wrapped']->{$element['#property_name']});
85
  }
86
  else {
87
    $variables['content'] = $element['#content'];
88
  }
89
}
90

    
91
/**
92
 * Renders a property using simple defaults based upon the property type.
93
 *
94
 * @return string
95
 */
96
function entity_property_default_render_value_by_type(EntityMetadataWrapper $property) {
97
  // If there is an options list or entity label, render that by default.
98
  if ($label = $property->label()) {
99
    if ($property instanceof EntityDrupalWrapper && $uri = entity_uri($property->type(), $property->value())) {
100
      return l($label, $uri['path'], $uri['options']);
101
    }
102
    else {
103
      return check_plain($label);
104
    }
105
  }
106
  switch ($property->type()) {
107
    case 'boolean':
108
      return $property->value() ? t('yes') : t('no');
109
    default:
110
      return check_plain($property->value());
111
  }
112
}
113

    
114
/**
115
 * Theme process function for theme_entity_property().
116
 *
117
 * Taken over from template_process_field()
118
 *
119
 * @see theme_entity_property()
120
 */
121
function template_process_entity_property(&$variables, $hook) {
122
  $element = $variables['elements'];
123
  // The default theme implementation is a function, so template_process() does
124
  // not automatically run, so we need to flatten the classes and attributes
125
  // here. For best performance, only call drupal_attributes() when needed, and
126
  // note that template_preprocess_field() does not initialize the
127
  // *_attributes_array variables.
128
  $variables['attributes'] = empty($variables['attributes_array']) ? '' : drupal_attributes($variables['attributes_array']);
129
  $variables['title_attributes'] = empty($variables['title_attributes_array']) ? '' : drupal_attributes($variables['title_attributes_array']);
130
  $variables['content_attributes'] = empty($variables['content_attributes_array']) ? '' : drupal_attributes($variables['content_attributes_array']);
131
}
132

    
133
/**
134
 * Themes the exportable status of an entity.
135
 */
136
function theme_entity_status($variables) {
137
  $status = $variables['status'];
138
  $html = $variables['html'];
139
  if (($status & ENTITY_FIXED) == ENTITY_FIXED) {
140
    $label = t('Fixed');
141
    $help = t('The configuration is fixed and cannot be changed.');
142
    return $html ? "<span class='entity-status-fixed' title='$help'>" . $label . "</span>" : $label;
143
  }
144
  elseif (($status & ENTITY_OVERRIDDEN) == ENTITY_OVERRIDDEN) {
145
    $label = t('Overridden');
146
    $help = t('This configuration is provided by a module, but has been changed.');
147
    return $html ? "<span class='entity-status-overridden' title='$help'>" . $label . "</span>" : $label;
148
  }
149
  elseif ($status & ENTITY_IN_CODE) {
150
    $label = t('Default');
151
    $help = t('A module provides this configuration.');
152
    return $html ? "<span class='entity-status-default' title='$help'>" . $label . "</span>" : $label;
153
  }
154
  elseif ($status & ENTITY_CUSTOM) {
155
    $label = t('Custom');
156
    $help = t('A custom configuration by a user.');
157
    return $html ? "<span class='entity-status-custom' title='$help'>" . $label . "</span>" : $label;
158
  }
159
}
160

    
161
/**
162
 * Process variables for entity.tpl.php.
163
 */
164
function template_preprocess_entity(&$variables) {
165
  $variables['view_mode'] = $variables['elements']['#view_mode'];
166
  $entity_type = $variables['elements']['#entity_type'];
167
  $variables['entity_type'] = $entity_type;
168
  $entity = $variables['elements']['#entity'];
169
  $variables[$variables['elements']['#entity_type']] = $entity;
170
  $info = entity_get_info($entity_type);
171

    
172
  $variables['title'] = check_plain(entity_label($entity_type, $entity));
173

    
174
  $uri = entity_uri($entity_type, $entity);
175
  $variables['url'] = $uri && !empty($uri['path']) ? url($uri['path'], $uri['options']) : FALSE;
176

    
177
  if (isset($variables['elements']['#page'])) {
178
    // If set by the caller, respect the page property.
179
    $variables['page'] = $variables['elements']['#page'];
180
  }
181
  else {
182
    // Else, try to automatically detect it.
183
    $variables['page'] = $uri && !empty($uri['path']) && $uri['path'] == $_GET['q'];
184
  }
185

    
186
  // Helpful $content variable for templates.
187
  $variables['content'] = array();
188
  foreach (element_children($variables['elements']) as $key) {
189
    $variables['content'][$key] = $variables['elements'][$key];
190
  }
191

    
192
  if (!empty($info['fieldable'])) {
193
    // Make the field variables available with the appropriate language.
194
    field_attach_preprocess($entity_type, $entity, $variables['content'], $variables);
195
  }
196
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
197

    
198
  // Gather css classes.
199
  $variables['classes_array'][] = drupal_html_class('entity-' . $entity_type);
200
  $variables['classes_array'][] = drupal_html_class($entity_type . '-' . $bundle);
201

    
202
  // Add RDF type and about URI.
203
  if (module_exists('rdf')) {
204
    $variables['attributes_array']['about'] = empty($uri['path']) ? NULL: url($uri['path']);
205
    $variables['attributes_array']['typeof'] = empty($entity->rdf_mapping['rdftype']) ? NULL : $entity->rdf_mapping['rdftype'];
206
  }
207

    
208
  // Add suggestions.
209
  $variables['theme_hook_suggestions'][] = $entity_type;
210
  $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle;
211
  $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle . '__' . $variables['view_mode'];
212
  if ($id = entity_id($entity_type, $entity)) {
213
    $variables['theme_hook_suggestions'][] = $entity_type . '__' . $id;
214
  }
215
}