You are here

function fc_field_multiple_value_form in Field Complete 7

Returns HTML for an individual form element.

Combine multiple values into a table with drag-n-drop reordering. TODO : convert to a template.

Parameters

$variables: An associative array containing:

  • element: A render element representing the form element.
1 string reference to 'fc_field_multiple_value_form'
fc_theme_registry_alter in ./fc.module
Implements hook_theme_registry_alter().

File

./fc.module, line 245
Field Complete - Provides field-based completeness for any entity.

Code

function fc_field_multiple_value_form($variables) {
  $element = $variables['element'];
  $output = '';
  if ($element['#cardinality'] > 1 || $element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
    $table_id = drupal_html_id($element['#field_name'] . '_values');
    $order_class = $element['#field_name'] . '-delta-order';
    $required = !empty($element['#required']) ? theme('form_required_marker', $variables) : '';
    $complete = !empty($element['#field_complete']) ? theme('form_complete_marker', array(
      'element' => $element,
    )) : '';
    $header = array(
      array(
        'data' => '<label>' . t('!title !required', array(
          '!title' => $element['#title'],
          '!required' => $required . $complete,
        )) . "</label>",
        'colspan' => 2,
        'class' => array(
          'field-label',
        ),
      ),
      t('Order'),
    );
    $rows = array();

    // Sort items according to '_weight' (needed when the form comes back after
    // preview or failed validation)
    $items = array();
    foreach (element_children($element) as $key) {
      if ($key === 'add_more') {
        $add_more_button =& $element[$key];
      }
      else {
        $items[] =& $element[$key];
      }
    }
    usort($items, '_field_sort_items_value_helper');

    // Add the items as table rows.
    foreach ($items as $key => $item) {
      $item['_weight']['#attributes']['class'] = array(
        $order_class,
      );
      $delta_element = drupal_render($item['_weight']);
      $cells = array(
        array(
          'data' => '',
          'class' => array(
            'field-multiple-drag',
          ),
        ),
        drupal_render($item),
        array(
          'data' => $delta_element,
          'class' => array(
            'delta-order',
          ),
        ),
      );
      $rows[] = array(
        'data' => $cells,
        'class' => array(
          'draggable',
        ),
      );
    }
    $output = '<div class="form-item">';
    $output .= theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => $table_id,
        'class' => array(
          'field-multiple-table',
        ),
      ),
    ));
    $output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
    $output .= '<div class="clearfix">' . drupal_render($add_more_button) . '</div>';
    $output .= '</div>';
    drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
  }
  else {
    foreach (element_children($element) as $key) {

      // The #field_complete flag is inherited so we have to re-check
      // whether this specific field should have a 'complete' marker.
      // There's probably a better way of doing this.
      $subelement =& $element[$key];
      if (!empty($subelement['#field_complete']) && !empty($subelement['#entity'])) {
        list(, , $bundle) = entity_extract_ids($subelement['#entity_type'], $subelement['#entity']);
        $instance = field_info_instance($subelement['#entity_type'], $element['#field_name'], $bundle);
        $subelement['#field_complete'] = !empty($instance['settings']['fc']['fc_include']);
        foreach ($subelement['#columns'] as $column) {
          if (!empty($subelement[$column])) {
            $subelement[$column]['#field_complete'] = $subelement['#field_complete'];
          }
        }
      }
      $output .= drupal_render($subelement);
    }
  }
  return $output;
}