entity_field.inc 8.7 KB

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