You are here

fc_field_groups.module in Field Complete 7

Field Complete Field Groups - Provides field complete support for field groups.

File

fc_field_groups/fc_field_groups.module
View source
<?php

/**
 * @file
 * Field Complete Field Groups - Provides field complete support for field groups.
 */

/**
 * Implements hook_fc_form_pre_render_alter().
 */
function fc_field_groups_fc_form_pre_render_alter(&$form, $ids) {
  if (!empty($form['#entity_type']) && !fc_entity_is_enabled($form['#entity_type']) || empty($form['#field_complete']) || empty($form['#groups'])) {

    // If this form contains no field complete
    // fields, or any groups, do nothing.
    return;
  }
  $settings = array();
  if (!property_exists($form['#entity'], 'fc')) {
    $form['#entity']->fc = fcComplete::load($form['#entity_type'], $form['#entity']);
  }
  $completeness = $form['#entity']->fc->completeness;
  foreach ($form['#groups'] as $group_name => &$group) {
    if (empty($group->children)) {

      // No children in this group at all
      continue;
    }

    // This skips groups we've already seen because we process
    // them recursively, just in case we have nested groups.
    $complete = _fc_field_groups_group_has_completeness($group_name, $form['#groups'], $completeness);
    if ($complete === NULL) {

      // We've alread processed this group (it's nested)
      // or it contains no completeness fields.
      continue;
    }

    // Split up the classes
    $classes = explode(' ', $group->format_settings['instance_settings']['classes']);

    // Add my classes
    $classes[] = 'fc-field-group';
    $classes[] = 'fc-field-group-' . ($complete ? 'complete' : 'incomplete');

    // Set them back into the settings
    $group->format_settings['instance_settings']['classes'] = implode(' ', $classes);

    // Record the details about this group for JS settings
    $settings[] = array(
      'text' => $group->label,
      'state' => $complete,
    );
  }

  // Fixed for Profile2 (see https://drupal.org/node/2095277)
  $bits = explode('-', $form['#id']);
  $form_id = $form['#entity_type'] != 'profile2' ? $form['#id'] : 'profile2-edit-' . array_pop($bits) . '-form';
  $path = drupal_get_path('module', 'fc_field_groups');
  $form['#attached']['js'][] = $path . '/fc_field_groups.js';
  $form['#attached']['css'][] = $path . '/fc_field_groups.css';
  $form['#attached']['js'][] = array(
    'data' => array(
      'fc_field_groups' => array(
        $form_id => $settings,
      ),
    ),
    'type' => 'setting',
  );
}

/**
 * Recursively processes groups to see if they contain completeness fields
 * and if they do whether it is complete or not.
 *
 * @param string $group_name
 * @param array $group
 * @param array $all_groups
 *
 * @return boolean or NULL
 *   NULL means we've processed it, or it has no completeness at all.
 */
function _fc_field_groups_group_has_completeness($group_name, $all_groups, $completeness, $depth = 0) {
  static $processed_groups = array();

  // Have we already processed this group?
  if (!empty($processed_groups[$group_name])) {
    return NULL;
  }

  // Mark this group as processed
  $processed_groups[$group_name] = TRUE;
  if (in_array($all_groups[$group_name]->format_type, array(
    'htabs',
    'vtabs',
  ))) {

    // This group is a wrapper and has no part in the display
    return NULL;
  }
  $complete = NULL;
  foreach ($all_groups[$group_name]->children as $child_name) {
    if (!empty($all_groups[$child_name])) {

      // This is a group name. so check the
      // child group to see if it's complete
      $child_complete = _fc_field_groups_group_has_completeness($child_name, $all_groups, $completeness, $depth + 1);
      if ($child_complete !== NULL) {

        // If the result was NULL it has no effect on the result
        $complete = $child_complete;
      }
    }
    else {

      // It's a field, if it's not in the completeness list
      // we just ignore it, otherwise we set the $complete
      // variable and continue processing.
      if (array_key_exists($child_name, $completeness)) {
        $complete = $completeness[$child_name];
      }
    }

    // Speed check, if complete has become false it can never be true
    // so we can exit the loop
    if ($complete === FALSE) {
      break;
    }
  }
  return $complete;
}

Functions

Namesort descending Description
fc_field_groups_fc_form_pre_render_alter Implements hook_fc_form_pre_render_alter().
_fc_field_groups_group_has_completeness Recursively processes groups to see if they contain completeness fields and if they do whether it is complete or not.