entity_field.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * @file
  4. * Handle rendering entity fields as panes.
  5. */
  6. $plugin = array(
  7. 'title' => t('Entity field'),
  8. 'defaults' => array('label' => 'title', 'formatter' => '', 'delta_limit' => 0, 'delta_offset' => '0', 'delta_reversed' => FALSE),
  9. 'content type' => 'ctools_entity_field_content_type_content_type',
  10. );
  11. /**
  12. * Just one subtype.
  13. *
  14. * Ordinarily this function is meant to get just one subtype. However, we are
  15. * using it to deal with the fact that we have changed the subtype names. This
  16. * lets us translate the name properly.
  17. */
  18. function ctools_entity_field_content_type_content_type($subtype) {
  19. $types = ctools_entity_field_content_type_content_types();
  20. if (isset($types[$subtype])) {
  21. return $types[$subtype];
  22. }
  23. }
  24. /**
  25. * Return all field content types available.
  26. */
  27. function ctools_entity_field_content_type_content_types() {
  28. $types = &drupal_static(__FUNCTION__, array());
  29. if (!empty($types)) {
  30. return $types;
  31. }
  32. $cache_key = 'ctools_entity_field_content_type_content_types';
  33. if ($cache = cache_get($cache_key)) {
  34. $types = $cache->data;
  35. if (!empty($types)) {
  36. return $types;
  37. }
  38. }
  39. // This will hold all the individual field content types.
  40. $context_types = array();
  41. $entities = entity_get_info();
  42. $description = t('Field on the referenced entity.');
  43. $styles = t('Formatter Styles');
  44. $categories = array();
  45. foreach ($entities as $entity_type => $entity) {
  46. $category = t(ucfirst($entity_type));
  47. $categories[$entity_type] = $category;
  48. foreach ($entity['bundles'] as $type => $bundle) {
  49. foreach (field_info_instances($entity_type, $type) as $field_name => $field) {
  50. if (!isset($types[$entity_type . ':' . $field_name])) {
  51. $label = t($field['label']);
  52. $types[$entity_type . ':' . $field_name] = array(
  53. 'category' => $category,
  54. 'icon' => 'icon_field.png',
  55. 'title' => t('Field: @widget_label (@field_name)', array(
  56. '@widget_label' => $label,
  57. '@field_name' => $field_name,
  58. )),
  59. 'description' => $description,
  60. 'edit form' => array(
  61. 'ctools_entity_field_content_type_formatter_options' => array(
  62. 'default' => TRUE,
  63. 'title' => t('Formatter options for: @widget_label (@field_name)', array(
  64. '@widget_label' => $label,
  65. '@field_name' => $field_name,
  66. )),
  67. ),
  68. 'ctools_entity_field_content_type_formatter_styles' => $styles,
  69. ),
  70. );
  71. }
  72. $context_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
  73. }
  74. }
  75. }
  76. // Create the required context for each field related to the bundle types.
  77. foreach ($types as $key => $field_content_type) {
  78. list($entity_type, $field_name) = explode(':', $key, 2);
  79. $types[$key]['required context'] = new ctools_context_required($categories[$entity_type], $entity_type, array(
  80. 'type' => array_keys($context_types[$key]['types']),
  81. ));
  82. unset($context_types[$key]['types']);
  83. }
  84. cache_set($cache_key, $types);
  85. return $types;
  86. }
  87. /**
  88. * Render the custom content type.
  89. */
  90. function ctools_entity_field_content_type_render($subtype, $conf, $panel_args, $context) {
  91. if (empty($context) || empty($context->data)) {
  92. return;
  93. }
  94. // Get a shortcut to the entity.
  95. $entity = $context->data;
  96. list($entity_type, $field_name) = explode(':', $subtype, 2);
  97. // Load the entity type's information for this field.
  98. $ids = entity_extract_ids($entity_type, $entity);
  99. $field = field_info_instance($entity_type, $field_name, $ids[2]);
  100. // Do not render if the entity type does not have this field.
  101. if (empty($field)) {
  102. return;
  103. }
  104. $language = field_language($entity_type, $entity, $field_name);
  105. if (empty($conf['label']) || $conf['label'] == 'title') {
  106. $label = 'hidden';
  107. $conf['label'] = 'title';
  108. }
  109. else {
  110. $label = $conf['label'];
  111. }
  112. $field_settings = array(
  113. 'label' => $label,
  114. 'type' => $conf['formatter'],
  115. // Pass all entity field panes settings to field display settings.
  116. 'pane_settings' => $conf,
  117. );
  118. // Get the field output, and the title.
  119. if (!empty($conf['formatter_settings'])) {
  120. $field_settings['settings'] = $conf['formatter_settings'];
  121. }
  122. $all_values = field_get_items($entity_type, $entity, $field_name, $language);
  123. if (!is_array($all_values)) {
  124. // Do not render if the field is empty.
  125. return;
  126. }
  127. // Reverse values.
  128. if (isset($conf['delta_reversed']) && $conf['delta_reversed']) {
  129. $all_values = array_reverse($all_values, TRUE);
  130. }
  131. if (isset($conf['delta_limit'])) {
  132. $offset = intval($conf['delta_offset']);
  133. $limit = !empty($conf['delta_limit']) ? $conf['delta_limit'] : NULL;
  134. $all_values = array_slice($all_values, $offset, $limit, TRUE);
  135. }
  136. $clone = clone $entity;
  137. $clone->{$field_name}[$language] = $all_values;
  138. $field_output = field_view_field($entity_type, $clone, $field_name, $field_settings, $language);
  139. if (!empty($field_output) && !empty($conf['override_title'])) {
  140. $field_output['#title'] = filter_xss_admin($conf['override_title_text']);
  141. }
  142. // Build the content type block.
  143. $block = new stdClass();
  144. $block->module = 'entity_field';
  145. if ($conf['label'] == 'title' && isset($field_output['#title'])) {
  146. $block->title = $field_output['#title'];
  147. }
  148. $block->content = $field_output;
  149. $block->delta = $ids[0];
  150. return $block;
  151. }
  152. /**
  153. * Returns an edit form for custom type settings.
  154. */
  155. function ctools_entity_field_content_type_formatter_options($form, &$form_state) {
  156. if (empty($form_state['conf']['formatter_settings'])) {
  157. $form_state['conf']['formatter_settings'] = array();
  158. }
  159. $conf = $form_state['conf'];
  160. $subtype = $form_state['subtype_name'];
  161. list($entity_type, $field_name) = explode(':', $subtype, 2);
  162. $field = field_info_field($field_name);
  163. module_load_include('inc', 'field_ui', 'field_ui.admin');
  164. $formatter_options = field_ui_formatter_options($field['type']);
  165. $field_label_options = array(
  166. 'title' => t('Pane title'),
  167. 'above' => t('Above'),
  168. 'inline' => t('Inline'),
  169. 'hidden' => t('Hidden'),
  170. );
  171. $form['label'] = array(
  172. '#type' => 'select',
  173. '#title' => t('Label'),
  174. '#options' => $field_label_options,
  175. '#default_value' => $conf['label'],
  176. );
  177. $form['formatter'] = array(
  178. '#type' => 'select',
  179. '#title' => t('Select a formatter'),
  180. '#options' => $formatter_options,
  181. '#default_value' => $conf['formatter'],
  182. );
  183. return $form;
  184. }
  185. function ctools_entity_field_content_type_formatter_options_submit($form, &$form_state) {
  186. $form_state['conf']['formatter'] = $form_state['values']['formatter'];
  187. $form_state['conf']['label'] = $form_state['values']['label'];
  188. }
  189. function ctools_entity_field_content_type_formatter_styles($form, &$form_state) {
  190. if (!$form_state['conf']['formatter_settings']) {
  191. $form_state['conf']['formatter_settings'] = array();
  192. }
  193. $conf = $form_state['conf'];
  194. $subtype = $form_state['subtype_name'];
  195. list($entity_type, $field_name) = explode(':', $subtype, 2);
  196. $field = field_info_field($field_name);
  197. ctools_form_include($form_state, 'field_ui.admin', 'field_ui', '');
  198. ctools_form_include($form_state, 'fields');
  199. $form['ctools_field_list'] = array(
  200. '#type' => 'value',
  201. '#value' => array(),
  202. );
  203. ctools_fields_get_field_formatter_settings_form($field, $conf['formatter'], $form, $form_state);
  204. return $form;
  205. }
  206. function ctools_entity_field_content_type_formatter_styles_submit($form, &$form_state) {
  207. $fields = $form_state['values']['ctools_field_list'];
  208. $formatter_info = ctools_fields_get_field_formatter_info($fields);
  209. foreach ($formatter_info as $info) {
  210. if (!empty($info['settings'])) {
  211. foreach ($info['settings'] as $field_name => $value) {
  212. if (isset($form_state['values'][$field_name])) {
  213. $form_state['conf']['formatter_settings'][$field_name] = $form_state['values'][$field_name];
  214. }
  215. }
  216. }
  217. }
  218. if (isset($form_state['values']['delta_limit'])) {
  219. $form_state['conf']['delta_limit'] = $form_state['values']['delta_limit'];
  220. $form_state['conf']['delta_offset'] = $form_state['values']['delta_offset'];
  221. $form_state['conf']['delta_reversed'] = $form_state['values']['delta_reversed'];
  222. }
  223. }
  224. /**
  225. * Returns the administrative title for a type.
  226. */
  227. function ctools_entity_field_content_type_admin_title($subtype, $conf, $context) {
  228. list($bundle, $field_name) = explode(':', $subtype);
  229. ctools_include('fields');
  230. if (is_object($context) && isset($context->identifier)) {
  231. $identifier = $context->identifier;
  232. }
  233. else {
  234. $type = 'ctools_entity_field_content_type_admin_title';
  235. $message = t('Context is missing for field: @name', array('@name' => $subtype));
  236. $variables = array($subtype, $conf, $context);
  237. watchdog($type, $message, $variables, $severity = WATCHDOG_NOTICE);
  238. $identifier = t('Unknown');
  239. }
  240. return t('"@s" @field', array('@s' => $identifier, '@field' => ctools_field_label($field_name)));
  241. }