You are here

function field_weight_multiple_entity_view_alter in Field display weights (per node) 7.2

Implements hook_entity_view_alter().

1 call to field_weight_multiple_entity_view_alter()
field_weight_inherit_entity_view_alter in modules/field_weight_inherit.module

File

modules/field_weight_multiple.module, line 399

Code

function field_weight_multiple_entity_view_alter(&$build, $type) {

  // So, we are engaging in some righteous evil surgery here. We're going to
  // pull out the individual field items and put them in $build,
  // along with #weight. Then we will remove the main field wrappers from $build.
  // Similar logic to field_weight here
  if ($type == 'node') {

    // May be replaced with option variables, if this expands to all entities.
    // Check if the bundle type is enabled.
    $enabled_node_types = variable_get('field_weight_node_types', array());
    if (in_array($build['#bundle'], $enabled_node_types, TRUE)) {

      // See if any field delta weights have been set.
      $multi_weights = field_weight_multiple_get_weight($build['#node']->vid);
      if ($multi_weights) {

        // Unset all field wrappers from $build.
        $fields = _field_weight_multiple_parse(field_info_instances('node', $build['#node']->type));
        foreach ($fields as $field) {
          if (isset($build[$field])) {

            // Get a copy of the field
            $field_structure = $build[$field];

            // Add the deltas to $build
            $node_multi_items = $build[$field]['#items'];
            foreach ($node_multi_items as $delta => $node_multi) {

              // Use the render array that's already in the $build array,
              // for maximum predictability or something
              // Put it underneath the existing field wrapper structure
              // and fix up the IDs/contents
              $build["{$field}_{$delta}"] = $field_structure;
              $new_item =& $build["{$field}_{$delta}"];

              // Unset deltas that aren't this one
              foreach (element_children($new_item) as $element_delta) {
                if ($element_delta !== $delta) {
                  unset($new_item[$element_delta]);
                  unset($new_item['#items'][$element_delta]);
                }
              }
            }
            unset($build[$field]);
          }
        }

        // Apply weight/hidden settings.
        foreach ($multi_weights as $key => $values) {
          $build[$key]['#weight'] = $values['weight'];
          if ($values['hidden'] == TRUE) {

            // If field has been hidden set this to FALSE, therefore
            // won't be displayed.
            $build[$key]['#access'] = FALSE;
          }
        }
      }
    }
  }
}