You are here

function field_group_fields_nest in Field Group 7.2

Same name and namespace in other branches
  1. 8.3 field_group.module \field_group_fields_nest()
  2. 8 field_group.module \field_group_fields_nest()
  3. 7 field_group.module \field_group_fields_nest()

Recursive function to nest fields in the field groups.

This function will take out all the elements in the form and place them in the correct container element, a fieldgroup. The current group element in the loop is passed recursively so we can stash fields and groups in it while we go deeper in the array.

Parameters

Array $element: The current element to analyse for grouping.

Array $vars: Rendering vars from the entity beïng viewed.

1 call to field_group_fields_nest()
field_group_build_entity_groups in ./field_group.module
Preprocess/ Pre-render callback.

File

./field_group.module, line 1697
Fieldgroup module.

Code

function field_group_fields_nest(&$element, &$vars = NULL) {

  // Create all groups and keep a flat list of references to these groups.
  $group_references = array();
  foreach ($element['#groups'] as $group_name => $group) {
    $element[$group_name] = array();
    $group_references[$group_name] =& $element[$group_name];
  }

  // Move all children to their parents. Use the flat list of references for
  // direct access as we don't know where in the root_element hierarchy the
  // parent currently is situated.
  foreach ($element['#group_children'] as $child_name => $parent_name) {

    // Entity beïng viewed
    if ($vars) {

      // If not a group, check vars['content'] for empty field.
      if (!isset($element['#groups'][$child_name]) && isset($vars['content'][$child_name])) {
        $group_references[$parent_name][$child_name] = $vars['content'][$child_name];
        unset($vars['content'][$child_name]);
      }
      else {
        $group_references[$parent_name][$child_name] =& $element[$child_name];
        unset($element[$child_name]);
      }
    }
    else {

      // Block denied fields (#access) before they are put in groups.
      // Fields (not groups) that don't have children (like field_permissions) are removed
      // in field_group_field_group_build_pre_render_alter.
      if (isset($element[$child_name]) && (!isset($element[$child_name]['#access']) || $element[$child_name]['#access'])) {

        // If this is a group, we have to use a reference to keep the reference
        // list intact (but if it is a field we don't mind).
        $group_references[$parent_name][$child_name] =& $element[$child_name];
      }

      // The child has been copied to its parent: remove it from the root element.
      unset($element[$child_name]);
    }
  }

  // Bring extra element wrappers to achieve a grouping of fields.
  // This will mainly be prefix and suffix altering.
  foreach ($element['#groups'] as $group_name => $group) {
    field_group_pre_render($group_references[$group_name], $group, $element);
  }
}