function tripal_form_alter

3.x tripal.module tripal_form_alter(&$form, $form_state, $form_id)

Implements hook_form_alter().

File

tripal/tripal.module, line 1034
The core Tripal module

Code

function tripal_form_alter(&$form, $form_state, $form_id) {

  // Remove fields that have no form. It's just a bit too confusing to have
  // widgets appear in the form but without any form elements inside them.
  if ($form_id == 'tripal_entity_form') {
    $children = element_children($form);
    foreach ($children as $child) {
      // Count the number of form elements.
      if (array_key_exists('und', $form[$child])) {
        $total_widgets = 0;
        // Some fields with cardinality of one that aren't TripalFields
        // may not have an array, so we need to catch those.
        if (array_key_exists('#type', $form[$child]['und'])) {
          $total_widgets++;
        }
        foreach ($form[$child]['und'] as $delta => $element) {
          if (is_numeric($delta)) {
            $total_widgets += count(element_children($element));
            // Ignore the weight column
            if (array_key_exists('_weight', $element)) {
              $total_widgets--;
            }
            // Ignore a hidden value column
            if (array_key_exists('value', $element) and $element['value']['#type'] == 'value') {
              $total_widgets--;
            }
            // Some form elements don't have a 'value' and they don't have any
            // widgets (i.e image field and description field. We don't
            // want to loose those, so add one to the widget count.
            if (!array_key_exists('value', $element)) {
              $total_widgets++;
            }
          }
        }
        // If we have no widgets then here's not a form for this field so just
        // remove it.
        if ($total_widgets == 0) {
          unset($form[$child]);
        }
      }
    }
  }
}