1 field_group.module field_group_fields_nest(array &$element, ? array &$vars = NULL)

Recursive function to nest fields in the field groups.

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|null $vars: Rendering vars from the entity being viewed.

File

core/modules/field_group/field_group.module, line 1031
Field Group module.

Code

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

  // Create all groups and keep a flat list of references to these groups.
  $group_references = array();
  foreach ($element['#fieldgroups'] as $group_name => $group) {
    // Check for any erroneous groups from other modules.
    if (is_string($group_name)) {
      // Construct own weight, as some fields (for example preprocess fields)
      // don't have weight set.
      $element[$group_name] = array();
      $group_references[$group_name] = &$element[$group_name];
    }
  }

  // Loop through all form children looking for those that are supposed to be
  // in groups, and insert placeholder element for the new group field in the
  // correct location within the form structure.
  $element_clone = array();
  foreach (element_children($element) as $child_name) {
    $element_clone[$child_name] = $element[$child_name];
    // If this element is in a group, create the placeholder element.
    if (isset($element['#group_children'][$child_name])) {
      $element_clone[$element['#group_children'][$child_name]] = array();
    }
  }
  $element = array_merge($element_clone, $element);

  // 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 being viewed.
    if ($vars) {
      // If not a group, check vars['content'] for empty field.
      if (!isset($element['#fieldgroups'][$child_name]) && isset($vars['content'][$child_name])) {
        $group_references[$parent_name][$child_name] = $vars['content'][$child_name];
        unset($vars['content'][$child_name]);
      }
      elseif (!isset($element['#fieldgroups'][$child_name]) && isset($vars['user_profile'][$child_name])) {
        $group_references[$parent_name][$child_name] = $vars['user_profile'][$child_name];
        unset($vars['user_profile'][$child_name]);
      }
      // 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).
      else {
        $group_references[$parent_name][$child_name] = &$element[$child_name];
        unset($element[$child_name]);
      }
    }
    // Form being viewed.
    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];
        $group_references[$parent_name]['#weight'] = $element['#fieldgroups'][$parent_name]->weight;
      }

      // 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['#fieldgroups'] as $group_name => $group) {
    field_group_pre_render($group_references[$group_name], $group, $element);
  }

}