Projet

Général

Profil

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

root / drupal7 / sites / all / modules / entity / theme / entity.theme.inc @ 7d7b5830

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($variables['content'])) {
84
    $variables['content'] = entity_property_default_render_value_by_type($element['#entity_wrapped']->{$element['#property_name']});
85
  }
86
}
87

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

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

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

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

    
169
  $variables['title'] = check_plain(entity_label($entity_type, $entity));
170

    
171
  $uri = entity_uri($entity_type, $entity);
172
  $variables['url'] = $uri && !empty($uri['path']) ? url($uri['path'], $uri['options']) : FALSE;
173

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

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

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

    
195
  // Gather css classes.
196
  $variables['classes_array'][] = drupal_html_class('entity-' . $entity_type);
197
  $variables['classes_array'][] = drupal_html_class($entity_type . '-' . $bundle);
198

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

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