You are here

function inline_entity_form_pre_render_add_fieldset_markup in Inline Entity Form 7

Move form elements into fieldsets for presentation purposes.

Inline forms use #tree = TRUE to keep their values in a hierarchy for easier storage. Moving the form elements into fieldsets during form building would break up that hierarchy, so it's not an option for Field API fields. Therefore, we wait until the pre_render stage, where any changes we make affect presentation only and aren't reflected in $form_state['values'].

1 string reference to 'inline_entity_form_pre_render_add_fieldset_markup'
inline_entity_form_entity_form in ./inline_entity_form.module
Wraps and returns the entity form provided by the passed-in controller.

File

./inline_entity_form.module, line 1797
Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…

Code

function inline_entity_form_pre_render_add_fieldset_markup($form) {
  $sort = array();
  foreach (element_children($form) as $key) {
    $element = $form[$key];

    // In our form builder functions, we added an arbitrary #fieldset property
    // to any element that belongs in a fieldset. If this form element has that
    // property, move it into its fieldset.
    if (isset($element['#fieldset']) && isset($form[$element['#fieldset']])) {
      $form[$element['#fieldset']][$key] = $element;

      // Remove the original element this duplicates.
      unset($form[$key]);

      // Mark the fieldset for sorting.
      if (!in_array($key, $sort)) {
        $sort[] = $element['#fieldset'];
      }
    }
  }

  // Sort all fieldsets, so that element #weight stays respected.
  foreach ($sort as $key) {
    uasort($form[$key], 'element_sort');
  }
  return $form;
}