entity_form_field.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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' => '', 'formatter' => ''),
  9. 'content type' => 'ctools_entity_form_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_form_field_content_type_content_type($subtype) {
  19. $types = ctools_entity_form_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_form_field_content_type_content_types() {
  28. // This will hold all the individual field content types.
  29. $types = &drupal_static(__FUNCTION__);
  30. if (isset($types)) {
  31. return $types;
  32. }
  33. $types = array();
  34. $content_types = array();
  35. $entities = entity_get_info();
  36. foreach ($entities as $entity_type => $entity) {
  37. foreach ($entity['bundles'] as $type => $bundle) {
  38. foreach (field_info_instances($entity_type, $type) as $field_name => $field) {
  39. if (!isset($types[$entity_type . ':' . $field_name])) {
  40. $types[$entity_type . ':' . $field_name] = array(
  41. 'category' => t('Form'),
  42. 'icon' => 'icon_field.png',
  43. 'title' => t('Field form: @widget_label', array(
  44. '@widget_label' => t($field['label']),
  45. )),
  46. 'description' => t('Field on the referenced entity.'),
  47. );
  48. }
  49. $content_types[$entity_type . ':' . $field_name]['types'][$type] = $bundle['label'];
  50. }
  51. }
  52. }
  53. if (module_exists('field_group')) {
  54. foreach ($entities as $entity_type => $entity) {
  55. foreach ($entity['bundles'] as $type => $bundle) {
  56. if ($group_info = field_group_info_groups($entity_type, $type, "form")) {
  57. foreach ($group_info as $group_name => $group) {
  58. if (!isset($types[$entity_type . ':' . $group_name])) {
  59. $types[$entity_type . ':' . $group_name] = array(
  60. 'category' => t('Form'),
  61. 'icon' => 'icon_field.png',
  62. 'title' => t('Group form: @widget_label', array('@widget_label' => $group->label)),
  63. 'description' => t('Field group on the referenced entity.'),
  64. );
  65. }
  66. $content_types[$entity_type . ':' . $group_name]['types'][$type] = $bundle['label'];
  67. }
  68. }
  69. }
  70. }
  71. }
  72. // Create the required context for each field related to the bundle types.
  73. foreach ($types as $key => $field_content_type) {
  74. list($entity_type, $field_name) = explode(':', $key, 2);
  75. $types[$key]['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
  76. 'form' => array('form'),
  77. 'type' => array_keys($content_types[$key]['types']),
  78. ));
  79. unset($content_types[$key]['types']);
  80. }
  81. return $types;
  82. }
  83. /**
  84. * Render the custom content type.
  85. */
  86. function ctools_entity_form_field_content_type_render($subtype, $conf, $panel_args, $context) {
  87. if (empty($context) || empty($context->data)) {
  88. return;
  89. }
  90. // Get a shortcut to the entity.
  91. $entity = $context->data;
  92. list($entity_type, $field_name) = explode(':', $subtype, 2);
  93. // Load the entity type's information for this field.
  94. $ids = entity_extract_ids($entity_type, $entity);
  95. $field = field_info_instance($entity_type, $field_name, $ids[2]);
  96. // Check for field groups.
  97. if (empty($field) && module_exists('field_group')) {
  98. $groups = field_group_info_groups($entity_type, $entity->type, "form");
  99. $group = !empty($groups[$field_name]) ? $groups[$field_name] : NULL;
  100. }
  101. // Do not render if the entity type does not have this field or group.
  102. if (empty($field) && empty($group)) {
  103. return;
  104. }
  105. $block = new stdClass();
  106. if (isset($context->form)) {
  107. $block->content = array();
  108. if (!empty($field)) {
  109. $block->content[$field_name] = $context->form[$field_name];
  110. unset($context->form[$field_name]);
  111. }
  112. else {
  113. // Pre-render the form to populate field groups.
  114. if (isset($context->form['#pre_render'])) {
  115. foreach ($context->form['#pre_render'] as $function) {
  116. if (function_exists($function)) {
  117. $context->form = $function($context->form);
  118. }
  119. }
  120. unset($context->form['#pre_render']);
  121. }
  122. $block->content[$field_name] = $context->form[$field_name];
  123. unset($context->form[$field_name]);
  124. }
  125. }
  126. else {
  127. $block->content = t('Entity info.');
  128. }
  129. return $block;
  130. }
  131. /**
  132. * Returns the administrative title for a type.
  133. */
  134. function ctools_entity_form_field_content_type_admin_title($subtype, $conf, $context) {
  135. list($entity_type, $field_name) = explode(':', $subtype, 2);
  136. if (!empty($context->restrictions)) {
  137. $field = field_info_instance($entity_type, $field_name, $context->restrictions['type'][0]);
  138. }
  139. else {
  140. $field = array('label' => $subtype);
  141. }
  142. return t('"@s" @field form', array('@s' => $context->identifier, '@field' => $field['label']));
  143. }
  144. function ctools_entity_form_field_content_type_edit_form($form, &$form_state) {
  145. // provide a blank form so we have a place to have context setting.
  146. return $form;
  147. }