entity.theme.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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($element['#content'])) {
  74. $variables['content'] = entity_property_default_render_value_by_type($element['#entity_wrapped']->{$element['#property_name']});
  75. }
  76. else {
  77. $variables['content'] = $element['#content'];
  78. }
  79. }
  80. /**
  81. * Renders a property using simple defaults based upon the property type.
  82. *
  83. * @return string
  84. */
  85. function entity_property_default_render_value_by_type(EntityMetadataWrapper $property) {
  86. // If there is an options list or entity label, render that by default.
  87. if ($label = $property->label()) {
  88. if ($property instanceof EntityDrupalWrapper && $uri = entity_uri($property->type(), $property->value())) {
  89. return l($label, $uri['path'], $uri['options']);
  90. }
  91. else {
  92. return check_plain($label);
  93. }
  94. }
  95. switch ($property->type()) {
  96. case 'boolean':
  97. return $property->value() ? t('yes') : t('no');
  98. default:
  99. return check_plain($property->value());
  100. }
  101. }
  102. /**
  103. * Theme process function for theme_entity_property().
  104. *
  105. * Taken over from template_process_field()
  106. *
  107. * @see theme_entity_property()
  108. */
  109. function template_process_entity_property(&$variables, $hook) {
  110. $element = $variables['elements'];
  111. // The default theme implementation is a function, so template_process() does
  112. // not automatically run, so we need to flatten the classes and attributes
  113. // here. For best performance, only call drupal_attributes() when needed, and
  114. // note that template_preprocess_field() does not initialize the
  115. // *_attributes_array variables.
  116. $variables['attributes'] = empty($variables['attributes_array']) ? '' : drupal_attributes($variables['attributes_array']);
  117. $variables['title_attributes'] = empty($variables['title_attributes_array']) ? '' : drupal_attributes($variables['title_attributes_array']);
  118. $variables['content_attributes'] = empty($variables['content_attributes_array']) ? '' : drupal_attributes($variables['content_attributes_array']);
  119. }
  120. /**
  121. * Themes the exportable status of an entity.
  122. */
  123. function theme_entity_status($variables) {
  124. $status = $variables['status'];
  125. $html = $variables['html'];
  126. if (($status & ENTITY_FIXED) == ENTITY_FIXED) {
  127. $label = t('Fixed');
  128. $help = t('The configuration is fixed and cannot be changed.');
  129. return $html ? "<span class='entity-status-fixed' title='$help'>" . $label . "</span>" : $label;
  130. }
  131. elseif (($status & ENTITY_OVERRIDDEN) == ENTITY_OVERRIDDEN) {
  132. $label = t('Overridden');
  133. $help = t('This configuration is provided by a module, but has been changed.');
  134. return $html ? "<span class='entity-status-overridden' title='$help'>" . $label . "</span>" : $label;
  135. }
  136. elseif ($status & ENTITY_IN_CODE) {
  137. $label = t('Default');
  138. $help = t('A module provides this configuration.');
  139. return $html ? "<span class='entity-status-default' title='$help'>" . $label . "</span>" : $label;
  140. }
  141. elseif ($status & ENTITY_CUSTOM) {
  142. $label = t('Custom');
  143. $help = t('A custom configuration by a user.');
  144. return $html ? "<span class='entity-status-custom' title='$help'>" . $label . "</span>" : $label;
  145. }
  146. }
  147. /**
  148. * Process variables for entity.tpl.php.
  149. */
  150. function template_preprocess_entity(&$variables) {
  151. $variables['view_mode'] = $variables['elements']['#view_mode'];
  152. $entity_type = $variables['elements']['#entity_type'];
  153. $variables['entity_type'] = $entity_type;
  154. $entity = $variables['elements']['#entity'];
  155. $variables[$variables['elements']['#entity_type']] = $entity;
  156. $info = entity_get_info($entity_type);
  157. $variables['title'] = check_plain(entity_label($entity_type, $entity));
  158. $uri = entity_uri($entity_type, $entity);
  159. $variables['url'] = $uri && !empty($uri['path']) ? url($uri['path'], $uri['options']) : FALSE;
  160. if (isset($variables['elements']['#page'])) {
  161. // If set by the caller, respect the page property.
  162. $variables['page'] = $variables['elements']['#page'];
  163. }
  164. else {
  165. // Else, try to automatically detect it.
  166. $variables['page'] = $uri && !empty($uri['path']) && $uri['path'] == $_GET['q'];
  167. }
  168. // Helpful $content variable for templates.
  169. $variables['content'] = array();
  170. foreach (element_children($variables['elements']) as $key) {
  171. $variables['content'][$key] = $variables['elements'][$key];
  172. }
  173. if (!empty($info['fieldable'])) {
  174. // Make the field variables available with the appropriate language.
  175. field_attach_preprocess($entity_type, $entity, $variables['content'], $variables);
  176. }
  177. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  178. // Gather css classes.
  179. $variables['classes_array'][] = drupal_html_class('entity-' . $entity_type);
  180. $variables['classes_array'][] = drupal_html_class($entity_type . '-' . $bundle);
  181. // Add RDF type and about URI.
  182. if (module_exists('rdf')) {
  183. $variables['attributes_array']['about'] = empty($uri['path']) ? NULL: url($uri['path']);
  184. $variables['attributes_array']['typeof'] = empty($entity->rdf_mapping['rdftype']) ? NULL : $entity->rdf_mapping['rdftype'];
  185. }
  186. // Add suggestions.
  187. $variables['theme_hook_suggestions'][] = $entity_type;
  188. $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle;
  189. $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle . '__' . $variables['view_mode'];
  190. if ($id = entity_id($entity_type, $entity)) {
  191. $variables['theme_hook_suggestions'][] = $entity_type . '__' . $id;
  192. }
  193. }