You are here

function field_group_fields_nest in Field Group 8

Same name and namespace in other branches
  1. 8.3 field_group.module \field_group_fields_nest()
  2. 7.2 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 being viewed.

Array $context: The display context (entity type, form or view).

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 445
Allows administrators to attach custom fields to fieldable types.

Code

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

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

    // 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 the content variable for empty field.
      $key = field_group_get_content_element_key($context);
      if (!isset($element['#fieldgroups'][$child_name]) && isset($vars[$key][$child_name])) {
        $group_references[$parent_name][$child_name] = $vars[$key][$child_name];
        unset($vars[$key][$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;
      }

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