entity_field.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. $clone = clone $entity;
  123. $all_values = field_get_items($entity_type, $entity, $field_name, $language);
  124. if (is_array($all_values)) {
  125. // Reverse values.
  126. if (isset($conf['delta_reversed']) && $conf['delta_reversed']) {
  127. $all_values = array_reverse($all_values, TRUE);
  128. }
  129. if (isset($conf['delta_limit'])) {
  130. $offset = intval($conf['delta_offset']);
  131. $limit = !empty($conf['delta_limit']) ? $conf['delta_limit'] : NULL;
  132. $all_values = array_slice($all_values, $offset, $limit, TRUE);
  133. }
  134. $clone->{$field_name}[$language] = $all_values;
  135. }
  136. $field_output = field_view_field($entity_type, $clone, $field_name, $field_settings, $language);
  137. if (!empty($field_output) && !empty($conf['override_title'])) {
  138. $field_output['#title'] = filter_xss_admin($conf['override_title_text']);
  139. }
  140. // Build the content type block.
  141. $block = new stdClass();
  142. $block->module = 'entity_field';
  143. if ($conf['label'] == 'title' && isset($field_output['#title'])) {
  144. $block->title = $field_output['#title'];
  145. }
  146. $block->content = $field_output;
  147. $block->delta = $ids[0];
  148. return $block;
  149. }
  150. /**
  151. * Returns an edit form for custom type settings.
  152. */
  153. function ctools_entity_field_content_type_formatter_options($form, &$form_state) {
  154. if (empty($form_state['conf']['formatter_settings'])) {
  155. $form_state['conf']['formatter_settings'] = array();
  156. }
  157. $conf = $form_state['conf'];
  158. $subtype = $form_state['subtype_name'];
  159. list($entity_type, $field_name) = explode(':', $subtype, 2);
  160. $field = field_info_field($field_name);
  161. module_load_include('inc', 'field_ui', 'field_ui.admin');
  162. $formatter_options = field_ui_formatter_options($field['type']);
  163. $field_label_options = array(
  164. 'title' => t('Pane title'),
  165. 'above' => t('Above'),
  166. 'inline' => t('Inline'),
  167. 'hidden' => t('Hidden'),
  168. );
  169. $form['label'] = array(
  170. '#type' => 'select',
  171. '#title' => t('Label'),
  172. '#options' => $field_label_options,
  173. '#default_value' => $conf['label'],
  174. );
  175. $form['formatter'] = array(
  176. '#type' => 'select',
  177. '#title' => t('Select a formatter'),
  178. '#options' => $formatter_options,
  179. '#default_value' => $conf['formatter'],
  180. );
  181. return $form;
  182. }
  183. function ctools_entity_field_content_type_formatter_options_submit($form, &$form_state) {
  184. $form_state['conf']['formatter'] = $form_state['values']['formatter'];
  185. $form_state['conf']['label'] = $form_state['values']['label'];
  186. }
  187. function ctools_entity_field_content_type_formatter_styles($form, &$form_state) {
  188. if (!$form_state['conf']['formatter_settings']) {
  189. $form_state['conf']['formatter_settings'] = array();
  190. }
  191. $conf = $form_state['conf'];
  192. $subtype = $form_state['subtype_name'];
  193. list($entity_type, $field_name) = explode(':', $subtype, 2);
  194. $field = field_info_field($field_name);
  195. ctools_form_include($form_state, 'field_ui.admin', 'field_ui', '');
  196. ctools_form_include($form_state, 'fields');
  197. $form['ctools_field_list'] = array(
  198. '#type' => 'value',
  199. '#value' => array(),
  200. );
  201. ctools_fields_get_field_formatter_settings_form($field, $conf['formatter'], $form, $form_state);
  202. return $form;
  203. }
  204. function ctools_entity_field_content_type_formatter_styles_submit($form, &$form_state) {
  205. $fields = $form_state['values']['ctools_field_list'];
  206. $formatter_info = ctools_fields_get_field_formatter_info($fields);
  207. foreach ($formatter_info as $info) {
  208. if (!empty($info['settings'])) {
  209. foreach ($info['settings'] as $field_name => $value) {
  210. if (isset($form_state['values'][$field_name])) {
  211. $form_state['conf']['formatter_settings'][$field_name] = $form_state['values'][$field_name];
  212. }
  213. }
  214. }
  215. }
  216. if (isset($form_state['values']['delta_limit'])) {
  217. $form_state['conf']['delta_limit'] = $form_state['values']['delta_limit'];
  218. $form_state['conf']['delta_offset'] = $form_state['values']['delta_offset'];
  219. $form_state['conf']['delta_reversed'] = $form_state['values']['delta_reversed'];
  220. }
  221. }
  222. /**
  223. * Returns the administrative title for a type.
  224. */
  225. function ctools_entity_field_content_type_admin_title($subtype, $conf, $context) {
  226. list($bundle, $field_name) = explode(':', $subtype);
  227. ctools_include('fields');
  228. if (is_object($context) && isset($context->identifier)) {
  229. $identifier = $context->identifier;
  230. }
  231. else {
  232. watchdog('ctools_entity_field_content_type_admin_title', 'Context is missing for field: @name', array('@name' => $subtype), WATCHDOG_NOTICE);
  233. $identifier = t('Unknown');
  234. }
  235. return t('"@s" @field', array('@s' => $identifier, '@field' => ctools_field_label($field_name)));
  236. }