field_object.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Implements hook_theme().
  4. */
  5. function field_object_theme() {
  6. return array(
  7. 'field_object_label' => array(
  8. 'variables' => array(
  9. 'field' => array(),
  10. 'instance' => array(),
  11. ),
  12. ),
  13. );
  14. }
  15. /**
  16. * Implements hook_field_info().
  17. */
  18. function field_object_field_info() {
  19. return array(
  20. 'field_object' => array(
  21. 'label' => t('Field Reference'),
  22. 'description' => t('Refers to a field instance.'),
  23. 'settings' => array(),
  24. 'instance_settings' => array(
  25. 'function' => NULL,
  26. ),
  27. 'default_widget' => 'options_select',
  28. 'default_formatter' => 'field_object_label',
  29. 'no_ui' => TRUE,
  30. 'property_type' => 'text',
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements hook_field_widget_info_alter().
  36. */
  37. function field_object_field_widget_info_alter(array &$info) {
  38. $info['options_select']['field types'][] = 'field_config_reference';
  39. }
  40. /**
  41. * Implements hook_field_formatter_info().
  42. */
  43. function field_object_field_formatter_info() {
  44. return array(
  45. 'field_object_label' => array(
  46. 'label' => t('Label'),
  47. 'description' => t("The field instance's label."),
  48. 'field types' => array('field_object'),
  49. 'settings' => array(),
  50. ),
  51. );
  52. }
  53. /**
  54. * Implements hook_field_is_empty().
  55. */
  56. function field_object_field_is_empty(array $item, array $field) {
  57. return empty($item['path']);
  58. }
  59. /**
  60. * Implements hook_field_formatter_view().
  61. */
  62. function field_object_field_formatter_view($entity_type, $entity, array $field, array $instance, $language, array $items, array $display) {
  63. $element = array();
  64. foreach ($items as $delta => $item) {
  65. $label = array();
  66. foreach (_field_object_expand_path($item['path']) as $field) {
  67. $label[] = theme('field_object_label', $field);
  68. }
  69. $element[$delta]['#markup'] = implode(' » ', $label);
  70. }
  71. return $element;
  72. }
  73. /**
  74. * Implements hook_options_list().
  75. */
  76. function field_object_options_list(array $field, array $instance, $entity_type, $entity) {
  77. return _field_object_build_hierarchy($field, $instance, $entity_type, $entity)->options();
  78. }
  79. /**
  80. * Renders a human-readable label for a field instance, including the entity
  81. * type and (if applicable) bundle that hosts it.
  82. */
  83. function theme_field_object_label(array $variables) {
  84. $instance = $variables['instance'];
  85. $output = '';
  86. if ($instance['entity_type'] != 'field_collection_item') {
  87. $entity_type = entity_get_info($instance['entity_type']);
  88. $output = $entity_type['label'] . ' » ';
  89. if ($entity_type['entity keys']['bundle']) {
  90. $output .= $entity_type['bundles'][ $instance['bundle'] ]['label'] . ' » ';
  91. }
  92. }
  93. return $output . $instance['label'];
  94. }
  95. /**
  96. * Helper function. Builds a FieldHierarchy object for the widget builder.
  97. */
  98. function _field_object_build_hierarchy(array $field, array $instance, $entity_type, $entity) {
  99. $hierarchy = new FieldHierarchy();
  100. // The instance should define a function which returns an array of FieldChain
  101. // objects to be added to the hierarchy.
  102. $function = $instance['settings']['function'];
  103. if ($function && is_callable($function)) {
  104. $arguments = func_get_args();
  105. $chains = (array) call_user_func_array($function, $arguments);
  106. array_walk($chains, array($hierarchy, 'addChain'));
  107. }
  108. return $hierarchy;
  109. }
  110. /**
  111. * Helper function. Expands a field reference's path value into an array
  112. * of field and instance definitions.
  113. */
  114. function _field_object_expand_path($path) {
  115. $output = array();
  116. foreach (explode('::', $path) as $instance) {
  117. list ($entity_type, $bundle, $field) = explode(':', $instance);
  118. $output[] = array(
  119. 'field' =>
  120. field_info_field($field),
  121. 'instance' =>
  122. field_info_instance($entity_type, $field, $bundle),
  123. );
  124. }
  125. return $output;
  126. }