field.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * Field module integration.
  5. */
  6. /**
  7. * Implements hook_field_formatter_info().
  8. */
  9. function custom_formatters_field_formatter_info() {
  10. $formatters = array();
  11. $settings = variable_get('custom_formatters_settings', array('label_prefix' => TRUE, 'label_prefix_value' => t('Custom')));
  12. foreach (custom_formatters_crud_load_all(TRUE) as $key => $formatter) {
  13. $label = $settings['label_prefix'] ? "{$settings['label_prefix_value']}: {$formatter->label}" : $formatter->label;
  14. $formatters["custom_formatters_{$key}"] = array(
  15. 'label' => $label,
  16. 'field types' => drupal_explode_tags($formatter->field_types),
  17. );
  18. if (isset($formatter->fapi)) {
  19. ob_start();
  20. eval($formatter->fapi);
  21. ob_get_clean();
  22. if (isset($form)) {
  23. $formatters["custom_formatters_{$key}"]['settings'] = array();
  24. foreach ($form as $form_key => $element) {
  25. $formatters["custom_formatters_{$key}"]['settings'][$form_key] = isset($element['#default_value']) ? $element['#default_value'] : '';
  26. }
  27. }
  28. }
  29. }
  30. return $formatters;
  31. }
  32. /**
  33. * Implements hook_field_formatter_settings_summary().
  34. */
  35. function custom_formatters_field_formatter_settings_summary($field, $instance, $view_mode) {
  36. $display = $instance['display'][$view_mode];
  37. $settings = $display['settings'];
  38. $formatter = custom_formatters_crud_load(drupal_substr($display['type'], 18));
  39. $summary = '';
  40. if (isset($formatter->fapi)) {
  41. ob_start();
  42. eval($formatter->fapi);
  43. ob_get_clean();
  44. if (isset($form)) {
  45. foreach ($form as $key => $element) {
  46. if (isset($element['#type']) && !in_array($element['#type'], array('fieldset'))) {
  47. $value = empty($settings[$key]) ? '<em>' . t('Empty') . '</em>' : $settings[$key];
  48. $value = is_array($value) ? implode(', ', array_filter($value)) : $value;
  49. $summary .= "{$element['#title']}: {$value}<br />";
  50. }
  51. }
  52. $summary = !empty($summary) ? $summary : ' ';
  53. }
  54. }
  55. return $summary;
  56. }
  57. /**
  58. * Implements hook_field_formatter_settings_form().
  59. */
  60. function custom_formatters_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  61. $display = $instance['display'][$view_mode];
  62. $settings = $display['settings'];
  63. $formatter = custom_formatters_crud_load(drupal_substr($display['type'], 18));
  64. $element = array();
  65. if (isset($formatter->fapi)) {
  66. ob_start();
  67. eval($formatter->fapi);
  68. ob_get_clean();
  69. if (isset($form)) {
  70. $element = $form;
  71. foreach (array_keys($element) as $key) {
  72. $element[$key]['#default_value'] = $settings[$key];
  73. }
  74. }
  75. }
  76. return $element;
  77. }
  78. /**
  79. * Implements hook_field_formatter_view().
  80. */
  81. function custom_formatters_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display, $formatter = NULL) {
  82. $element = array();
  83. $formatter = empty($formatter) ? custom_formatters_crud_load(drupal_substr($display['type'], 18)) : $formatter;
  84. if (isset($formatter) && !empty($items)) {
  85. $engines = module_invoke_all('custom_formatters_engine_info');
  86. $engine = $formatter->mode;
  87. if (isset($engines[$engine]['file']) && file_exists($engines[$engine]['file'])) {
  88. require_once $engines[$engine]['file'];
  89. }
  90. if (function_exists($function = $engines[$engine]['callbacks']['render'])) {
  91. $element = $function($formatter, $obj_type, $object, $field, $instance, $langcode, $items, $display);
  92. if (is_string($element)) {
  93. $element = array(
  94. array(
  95. '#markup' => $element,
  96. ),
  97. );
  98. }
  99. foreach (element_children($element) as $delta) {
  100. $element[$delta]['#cf_options'] = isset($display['#cf_options']) ? $display['#cf_options'] : array();
  101. }
  102. }
  103. // Allow other modules to modify the element.
  104. drupal_alter('custom_formatters_field_formatter_view_element', $element, $formatter);
  105. }
  106. return $element;
  107. }