You are here

function field_group_fields_nest in Field Group 7

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.2 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 being 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 2017
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['#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];

      // Get group parents
      $parents = array();
      $current_group = $group;
      while (!empty($current_group)) {
        array_unshift($parents, $current_group->group_name);
        $current_group = isset($element['#fieldgroups'][$current_group->parent_name]) ? $element['#fieldgroups'][$current_group->parent_name] : NULL;
      }
      $group_references[$group_name]['#array_parents'] = $parents;
      $element['#fieldgroups'][$group_name]->array_parents = $parents;

      // Remove self from parents and set #field_parents
      array_pop($parents);
      $group_references[$group_name]['#field_parents'] = $parents;
    }
  }

  // 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]);
      }
      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];
        $group_references[$parent_name]['#weight'] = $element['#fieldgroups'][$parent_name]->weight;

        // Prepend #array_parents & #field_parents of group child element & its element_children
        // if those keys are set, and don't already include the group parents
        $group_child =& $group_references[$parent_name][$child_name];
        $group_parents = $group_references[$parent_name]['#array_parents'];
        $process_elements = array_merge(array(
          &$group_child,
        ), _field_group_element_children_recursive_ref($group_child));
        foreach ($process_elements as $key => $current_element) {
          if (isset($current_element['#array_parents']) && !in_array($group_parents[0], $current_element['#array_parents'])) {
            $process_elements[$key]['#array_parents'] = array_merge($group_parents, $current_element['#array_parents']);
          }
          if (isset($current_element['#field_parents']) && !in_array($group_parents[0], $current_element['#field_parents'])) {
            $process_elements[$key]['#field_parents'] = array_merge($group_parents, $current_element['#field_parents']);
          }
        }
      }

      // 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);
  }
}