entity.theme.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @file
  4. * Holds entity module's theme functions.
  5. */
  6. /**
  7. * Returns HTML for an entity property.
  8. *
  9. * This is the default theme implementation to display the value of a property.
  10. * This function can be overridden with varying levels of specificity. For
  11. * example, for a property named 'title' displayed on the 'article' bundle,
  12. * any of the following functions will override this default implementation.
  13. * The first of these functions that exists is used:
  14. * - THEMENAME_property__body__article()
  15. * - THEMENAME_property__article()
  16. * - THEMENAME_property__body()
  17. * - THEMENAME_property()
  18. *
  19. * @param $variables
  20. * An associative array containing:
  21. * - label: A boolean indicating to show or hide the property label.
  22. * - title_attributes: A string containing the attributes for the title.
  23. * - label: The label for the property.
  24. * - content_attributes: A string containing the attributes for the content's
  25. * div.
  26. * - content: The rendered property value.
  27. * - attributes: A string containing the attributes for the wrapping div.
  28. *
  29. * @ingroup themeable
  30. */
  31. function theme_entity_property($variables) {
  32. $output = '';
  33. // Render the label, if it's not hidden.
  34. if (!$variables['label_hidden']) {
  35. $output .= '<div' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
  36. }
  37. // Render the content.
  38. $content_suffix = '';
  39. if (!$variables['label_hidden'] || $variables['content_attributes']) {
  40. $output .= '<div' . $variables['content_attributes'] . '>';
  41. $content_suffix = '</div>';
  42. }
  43. $output .= $variables['content'] . $content_suffix;
  44. // Render the top-level DIV.
  45. return '<div' . $variables['attributes'] . '>' . $output . '</div>';
  46. }
  47. /**
  48. * Theme preprocess function for theme_entity_property().
  49. *
  50. * @see theme_entity_property()
  51. */
  52. function template_preprocess_entity_property(&$variables, $hook) {
  53. $element = $variables['elements'];
  54. $variables += array(
  55. 'theme_hook_suggestions' => array(),
  56. 'attributes_array' => array(),
  57. );
  58. // Generate variables from element properties.
  59. foreach (array('label_hidden', 'label', 'property_name') as $name) {
  60. $variables[$name] = check_plain($element['#' . $name]);
  61. }
  62. $variables['title_attributes_array']['class'][] = 'entity-property-label';
  63. $variables['attributes_array'] = array_merge($variables['attributes_array'], isset($element['#attributes']) ? $element['#attributes'] : array());
  64. $variables['property_name_css'] = strtr($element['#property_name'], '_', '-');
  65. $variables['attributes_array']['class'][] = 'entity-property';
  66. $variables['attributes_array']['class'][] = 'entity-property-' . $variables['property_name_css'];
  67. // Add specific suggestions that can override the default implementation.
  68. $variables['theme_hook_suggestions'] += array(
  69. 'entity_property__' . $element['#property_name'],
  70. 'entity_property__' . $element['#entity_type'] . '__' . $element['#property_name'],
  71. );
  72. // Populate the content with sensible defaults.
  73. if (!isset($variables['content'])) {
  74. $variables['content'] = entity_property_default_render_value_by_type($element['#entity_wrapped']->{$element['#property_name']});
  75. }
  76. }
  77. /**
  78. * Renders a property using simple defaults based upon the property type.
  79. *
  80. * @return string
  81. */
  82. function entity_property_default_render_value_by_type(EntityMetadataWrapper $property) {
  83. // If there is an options list or entity label, render that by default.
  84. if ($label = $property->label()) {
  85. if ($property instanceof EntityDrupalWrapper && $uri = entity_uri($property->type(), $property->value())) {
  86. return l($label, $uri['path'], $uri['options']);
  87. }
  88. else {
  89. return check_plain($label);
  90. }
  91. }
  92. switch ($property->type()) {
  93. case 'boolean':
  94. return $property->value() ? t('yes') : t('no');
  95. default:
  96. return check_plain($property->value());
  97. }
  98. }
  99. /**
  100. * Theme process function for theme_entity_property().
  101. *
  102. * Taken over from template_process_field()
  103. *
  104. * @see theme_entity_property()
  105. */
  106. function template_process_entity_property(&$variables, $hook) {
  107. $element = $variables['elements'];
  108. // The default theme implementation is a function, so template_process() does
  109. // not automatically run, so we need to flatten the classes and attributes
  110. // here. For best performance, only call drupal_attributes() when needed, and
  111. // note that template_preprocess_field() does not initialize the
  112. // *_attributes_array variables.
  113. $variables['attributes'] = empty($variables['attributes_array']) ? '' : drupal_attributes($variables['attributes_array']);
  114. $variables['title_attributes'] = empty($variables['title_attributes_array']) ? '' : drupal_attributes($variables['title_attributes_array']);
  115. $variables['content_attributes'] = empty($variables['content_attributes_array']) ? '' : drupal_attributes($variables['content_attributes_array']);
  116. }
  117. /**
  118. * Themes the exportable status of an entity.
  119. */
  120. function theme_entity_status($variables) {
  121. $status = $variables['status'];
  122. $html = $variables['html'];
  123. if (($status & ENTITY_FIXED) == ENTITY_FIXED) {
  124. $label = t('Fixed');
  125. $help = t('The configuration is fixed and cannot be changed.');
  126. return $html ? "<span class='entity-status-fixed' title='$help'>" . $label . "</span>" : $label;
  127. }
  128. elseif (($status & ENTITY_OVERRIDDEN) == ENTITY_OVERRIDDEN) {
  129. $label = t('Overridden');
  130. $help = t('This configuration is provided by a module, but has been changed.');
  131. return $html ? "<span class='entity-status-overridden' title='$help'>" . $label . "</span>" : $label;
  132. }
  133. elseif ($status & ENTITY_IN_CODE) {
  134. $label = t('Default');
  135. $help = t('A module provides this configuration.');
  136. return $html ? "<span class='entity-status-default' title='$help'>" . $label . "</span>" : $label;
  137. }
  138. elseif ($status & ENTITY_CUSTOM) {
  139. $label = t('Custom');
  140. $help = t('A custom configuration by a user.');
  141. return $html ? "<span class='entity-status-custom' title='$help'>" . $label . "</span>" : $label;
  142. }
  143. }
  144. /**
  145. * Process variables for entity.tpl.php.
  146. */
  147. function template_preprocess_entity(&$variables) {
  148. $variables['view_mode'] = $variables['elements']['#view_mode'];
  149. $entity_type = $variables['elements']['#entity_type'];
  150. $variables['entity_type'] = $entity_type;
  151. $entity = $variables['elements']['#entity'];
  152. $variables[$variables['elements']['#entity_type']] = $entity;
  153. $info = entity_get_info($entity_type);
  154. $variables['title'] = check_plain(entity_label($entity_type, $entity));
  155. $uri = entity_uri($entity_type, $entity);
  156. $variables['url'] = $uri && !empty($uri['path']) ? url($uri['path'], $uri['options']) : FALSE;
  157. if (isset($variables['elements']['#page'])) {
  158. // If set by the caller, respect the page property.
  159. $variables['page'] = $variables['elements']['#page'];
  160. }
  161. else {
  162. // Else, try to automatically detect it.
  163. $variables['page'] = $uri && !empty($uri['path']) && $uri['path'] == $_GET['q'];
  164. }
  165. // Helpful $content variable for templates.
  166. $variables['content'] = array();
  167. foreach (element_children($variables['elements']) as $key) {
  168. $variables['content'][$key] = $variables['elements'][$key];
  169. }
  170. if (!empty($info['fieldable'])) {
  171. // Make the field variables available with the appropriate language.
  172. field_attach_preprocess($entity_type, $entity, $variables['content'], $variables);
  173. }
  174. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  175. // Gather css classes.
  176. $variables['classes_array'][] = drupal_html_class('entity-' . $entity_type);
  177. $variables['classes_array'][] = drupal_html_class($entity_type . '-' . $bundle);
  178. // Add RDF type and about URI.
  179. if (module_exists('rdf')) {
  180. $variables['attributes_array']['about'] = empty($uri['path']) ? NULL: url($uri['path']);
  181. $variables['attributes_array']['typeof'] = empty($entity->rdf_mapping['rdftype']) ? NULL : $entity->rdf_mapping['rdftype'];
  182. }
  183. // Add suggestions.
  184. $variables['theme_hook_suggestions'][] = $entity_type;
  185. $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle;
  186. $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle . '__' . $variables['view_mode'];
  187. if ($id = entity_id($entity_type, $entity)) {
  188. $variables['theme_hook_suggestions'][] = $entity_type . '__' . $id;
  189. }
  190. }