fields.inc

  1. cis7 sites/all/modules/ulmus/ctools/includes/fields.inc
  2. cle7 sites/all/modules/ulmus/ctools/includes/fields.inc
  3. ecd7 sites/all/modules/ulmus/ctools/includes/fields.inc
  4. elmsmedia7 sites/all/modules/ulmus/ctools/includes/fields.inc
  5. harmony7 sites/all/modules/ulmus/ctools/includes/fields.inc
  6. icor7 sites/all/modules/ulmus/ctools/includes/fields.inc
  7. meedjum_blog7 sites/all/modules/ulmus/ctools/includes/fields.inc
  8. mooc7 sites/all/modules/ulmus/ctools/includes/fields.inc

Extend core fields with some helper functions to reduce code complexity within views and ctools plugins.

Functions

Namesort descending Description
ctools_fields_fake_field_instance Fake an instance of a field.
ctools_fields_get_fields_by_type Returns a list of field definitions of a specified type.
ctools_fields_get_field_formatter_info Helper function for generating all the formatter information associated with any fields. Especially useful for determining the fields that will be added to form that executes hook_field_formatter_settings_form().
ctools_fields_get_field_formatter_settings_form Helper function for calling hook_field_formatter_settings_form() without needing to load an instance of the field.
ctools_field_foreign_keys Derive the foreign keys that a field provides.
ctools_field_invoke_field Replacement for core _field_invoke() to invoke on a single field.
ctools_field_invoke_field_default Replacement for core _field_invoke_default() to invoke on a single field.
ctools_field_label Returns the label of a certain field.

File

sites/all/modules/ulmus/ctools/includes/fields.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Extend core fields with some helper functions to reduce code complexity within views and ctools plugins.
  5. */
  6. /**
  7. * Fake an instance of a field.
  8. *
  9. * @param $field_name
  10. * The unique name for this field no matter what entity/bundle it may be used on.
  11. * @param $view_mode
  12. * We're building a new view mode for this function. Defaults to ctools, but we expect developers to actually name this something meaningful.
  13. * @param $formatter
  14. * The formatter key selected from the options provided by field_ui_formatter_options().
  15. * @param $formatter_settings
  16. * An array of key value pairs. These will be used as #default_value for the form elements generated by a call to hook_field_formatter_settings_form() for this field type.
  17. * Typically we'll pass an empty array to begin with and then pass this information back to ourselves on form submit so that we can set the values for later edit sessions.
  18. */
  19. function ctools_fields_fake_field_instance($field_name, $view_mode = 'ctools', $formatter, $formatter_settings) {
  20. $field = field_read_field($field_name);
  21. $field_type = field_info_field_types($field['type']);
  22. return array(
  23. // Build a fake entity type and bundle.
  24. 'field_name' => $field_name,
  25. 'entity_type' => 'ctools',
  26. 'bundle' => 'ctools',
  27. // Use the default field settings for settings and widget.
  28. 'settings' => field_info_instance_settings($field['type']),
  29. 'widget' => array(
  30. 'type' => $field_type['default_widget'],
  31. 'settings' => array(),
  32. ),
  33. // Build a dummy display mode.
  34. 'display' => array(
  35. $view_mode => array(
  36. 'type' => $formatter,
  37. 'settings' => $formatter_settings,
  38. ),
  39. ),
  40. // Set the other fields to their default values.
  41. // @see _field_write_instance().
  42. 'required' => FALSE,
  43. 'label' => $field_name,
  44. 'description' => '',
  45. 'deleted' => 0,
  46. );
  47. }
  48. /**
  49. * Helper function for calling hook_field_formatter_settings_form() without needing to load an instance of the field.
  50. *
  51. * @param $field
  52. * A fully loaded field.
  53. * @param $formatter_type
  54. * The formatter key selected from the options provided by field_ui_formatter_options().
  55. * @param $form
  56. * The full form from the function that's calling this function.
  57. * @param $form_state
  58. * The full form_state from the function that's calling this function.
  59. * @param $view_mode
  60. * We're passing a view mode from this function to the fake instance we're creating. Defaults to ctools, but we expect developers to actually name this something meaningful.
  61. */
  62. function ctools_fields_get_field_formatter_settings_form($field, $formatter_type, &$form, $form_state, $view_mode = 'ctools') {
  63. $conf = $form_state['conf'];
  64. $formatter = field_info_formatter_types($formatter_type);
  65. if (isset($formatter['settings'])) {
  66. $conf['formatter_settings'] += $formatter['settings'];
  67. }
  68. $function = $formatter['module'] . '_field_formatter_settings_form';
  69. if (function_exists($function)) {
  70. $instance = ctools_fields_fake_field_instance($field['field_name'], $view_mode, $formatter_type, $conf['formatter_settings']);
  71. $settings_form = $function($field, $instance, $view_mode, $form, $form_state);
  72. if ($settings_form) {
  73. $form['ctools_field_list']['#value'][] = $field;
  74. $form += $settings_form;
  75. }
  76. }
  77. if (isset($field['cardinality']) && $field['cardinality'] != 1) {
  78. list($prefix, $suffix) = explode('@count', t('Skip the first @count item(s)'));
  79. $form['delta_offset'] = array(
  80. '#type' => 'textfield',
  81. '#size' => 5,
  82. '#field_prefix' => $prefix,
  83. '#field_suffix' => $suffix,
  84. '#default_value' => isset($conf['delta_offset']) ? $conf['delta_offset'] : 0,
  85. );
  86. list($prefix, $suffix) = explode('@count', t('Then display at most @count item(s)'));
  87. $form['delta_limit'] = array(
  88. '#type' => 'textfield',
  89. '#size' => 5,
  90. '#field_prefix' => $prefix,
  91. '#field_suffix' => $suffix,
  92. '#description' => t('Enter 0 to display all items.'),
  93. '#default_value' => isset($conf['delta_limit']) ? $conf['delta_limit'] : 0,
  94. );
  95. $form['delta_reversed'] = array(
  96. '#title' => t('Display in reverse order'),
  97. '#type' => 'checkbox',
  98. '#default_value' => !empty($conf['delta_reversed']),
  99. '#description' => t('(start from last values)'),
  100. );
  101. }
  102. }
  103. /**
  104. * Helper function for generating all the formatter information associated with
  105. * any fields.
  106. * Especially useful for determining the fields that will be added to form that
  107. * executes hook_field_formatter_settings_form().
  108. *
  109. * @param $fields
  110. * An array of fully loaded fields.
  111. */
  112. function ctools_fields_get_field_formatter_info($fields) {
  113. $info = array();
  114. $field_info = module_invoke_all('field_formatter_info');
  115. foreach ($fields as $field) {
  116. foreach ($field_info as $format_name => $formatter_info) {
  117. if (in_array($field['type'], $formatter_info['field types'])) {
  118. $info += array($format_name => $formatter_info);
  119. }
  120. }
  121. }
  122. drupal_alter('field_formatter_info', $info);
  123. return $info;
  124. }
  125. /**
  126. * Returns the label of a certain field.
  127. *
  128. * Cribbed from Views.
  129. */
  130. function ctools_field_label($field_name) {
  131. $label_counter = array();
  132. // Count the amount of instances per label per field.
  133. $instances = field_info_instances();
  134. foreach ($instances as $entity_type) {
  135. foreach ($entity_type as $bundle) {
  136. if (isset($bundle[$field_name])) {
  137. $label_counter[$bundle[$field_name]['label']] = isset($label_counter[$bundle[$field_name]['label']]) ? ++$label_counter[$bundle[$field_name]['label']] : 1;
  138. }
  139. }
  140. }
  141. if (empty($label_counter)) {
  142. return $field_name;
  143. }
  144. // Sort the field lables by it most used label and return the most used one.
  145. arsort($label_counter);
  146. $label_counter = array_keys($label_counter);
  147. return $label_counter[0];
  148. }
  149. /**
  150. * Replacement for core _field_invoke() to invoke on a single field.
  151. *
  152. * Core only allows invoking field hooks via a private function for all fields
  153. * on an entire entity. However, we very often need to invoke our hooks on
  154. * a single field as we take things apart and only use little bits.
  155. *
  156. * @param $field_name
  157. * Either a field instance object or the name of the field.
  158. * If the 'field' key is populated it will be used as the field
  159. * settings.
  160. * @param $op
  161. * Possible operations include:
  162. * - form
  163. * - validate
  164. * - presave
  165. * - insert
  166. * - update
  167. * - delete
  168. * - delete revision
  169. * - view
  170. * - prepare translation
  171. * @param $entity_type
  172. * The type of $entity; e.g. 'node' or 'user'.
  173. * @param $entity
  174. * The fully formed $entity_type entity.
  175. * @param $a
  176. * - The $form in the 'form' operation.
  177. * - The value of $view_mode in the 'view' operation.
  178. * - Otherwise NULL.
  179. * @param $b
  180. * - The $form_state in the 'submit' operation.
  181. * - Otherwise NULL.
  182. * @param $options
  183. * An associative array of additional options, with the following keys:
  184. * - 'field_name': The name of the field whose operation should be
  185. * invoked. By default, the operation is invoked on all the fields
  186. * in the entity's bundle. NOTE: This option is not compatible with
  187. * the 'deleted' option; the 'field_id' option should be used
  188. * instead.
  189. * - 'field_id': The id of the field whose operation should be
  190. * invoked. By default, the operation is invoked on all the fields
  191. * in the entity's' bundles.
  192. * - 'default': A boolean value, specifying which implementation of
  193. * the operation should be invoked.
  194. * - if FALSE (default), the field types implementation of the operation
  195. * will be invoked (hook_field_[op])
  196. * - If TRUE, the default field implementation of the field operation
  197. * will be invoked (field_default_[op])
  198. * Internal use only. Do not explicitely set to TRUE, but use
  199. * _field_invoke_default() instead.
  200. * - 'deleted': If TRUE, the function will operate on deleted fields
  201. * as well as non-deleted fields. If unset or FALSE, only
  202. * non-deleted fields are operated on.
  203. * - 'language': A language code or an array of language codes keyed by field
  204. * name. It will be used to narrow down to a single value the available
  205. * languages to act on.
  206. *
  207. * @see _field_invoke()
  208. */
  209. function ctools_field_invoke_field($field_name, $op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) {
  210. if (is_array($field_name)) {
  211. $instance = $field_name;
  212. $field = empty($field_name['field']) ? field_info_field($instance['field_name']) : $field_name['field'];
  213. $field_name = $instance['field_name'];
  214. }
  215. else {
  216. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  217. $instance = field_info_instance($entity_type, $field_name, $bundle);
  218. }
  219. if (empty($instance)) {
  220. return;
  221. }
  222. // Merge default options.
  223. $default_options = array(
  224. 'default' => FALSE,
  225. 'deleted' => FALSE,
  226. 'language' => NULL,
  227. );
  228. $options += $default_options;
  229. $return = array();
  230. // Everything from here is unmodified code from _field_invoke() formerly
  231. // inside a foreach loop over the instances.
  232. $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op;
  233. if (function_exists($function)) {
  234. // Determine the list of languages to iterate on.
  235. $available_languages = field_available_languages($entity_type, $field);
  236. $languages = _field_language_suggestion($available_languages, $options['language'], $field_name);
  237. foreach ($languages as $langcode) {
  238. $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
  239. $result = $function($entity_type, $entity, $field, $instance, $langcode, $items, $a, $b);
  240. if (isset($result)) {
  241. // For hooks with array results, we merge results together.
  242. // For hooks with scalar results, we collect results in an array.
  243. if (is_array($result)) {
  244. $return = array_merge($return, $result);
  245. }
  246. else {
  247. $return[] = $result;
  248. }
  249. }
  250. // Populate $items back in the field values, but avoid replacing missing
  251. // fields with an empty array (those are not equivalent on update).
  252. if ($items !== array() || isset($entity->{$field_name}[$langcode])) {
  253. $entity->{$field_name}[$langcode] = $items;
  254. }
  255. }
  256. }
  257. return $return;
  258. }
  259. /**
  260. * Replacement for core _field_invoke_default() to invoke on a single field.
  261. *
  262. * @see ctools_field_invoke_field()
  263. * @see _field_invoke_default()
  264. */
  265. function ctools_field_invoke_field_default($field_name, $op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) {
  266. $options['default'] = TRUE;
  267. return ctools_field_invoke_field($field_name, $op, $entity_type, $entity, $a, $b, $options);
  268. }
  269. /**
  270. * Returns a list of field definitions of a specified type.
  271. *
  272. * @param string $field_type
  273. * A field type name; e.g. 'text' or 'date'.
  274. *
  275. * @return array
  276. * An array of field definitions of the specified type, keyed by field name.
  277. */
  278. function ctools_fields_get_fields_by_type($field_type) {
  279. $fields = array();
  280. foreach (field_info_fields() as $field_name => $field_info) {
  281. if ($field_info['type'] == $field_type) {
  282. $fields[$field_name] = $field_info;
  283. }
  284. }
  285. return $fields;
  286. }
  287. /**
  288. * Derive the foreign keys that a field provides.
  289. *
  290. * @param $field_name
  291. * The name of the field.
  292. *
  293. * @return
  294. * An array of foreign keys according to Schema API.
  295. */
  296. function ctools_field_foreign_keys($field_name) {
  297. $foreign_keys = &drupal_static(__FUNCTION__, array());
  298. if (!isset($foreign_keys[$field_name])) {
  299. $foreign_keys[$field_name] = array();
  300. $field = field_info_field($field_name);
  301. if (!empty($field['foreign keys'])) {
  302. $foreign_keys[$field_name] = $field['foreign keys'];
  303. }
  304. else {
  305. // try to fetch foreign keys from schema, as not everything
  306. // stores foreign keys properly in the field info.
  307. $module = $field['module'];
  308. module_load_install($module);
  309. $schema = module_invoke($module, 'field_schema', $field);
  310. if (!empty($schema['foreign keys'])) {
  311. $foreign_keys[$field_name] = $schema['foreign keys'];
  312. }
  313. }
  314. }
  315. return $foreign_keys[$field_name];
  316. }