0

Is it possible to show a numbered output to an unlimited value field (in the input form rather than the display).

Basically I have a field collection and I'd like to show by the side of each one entered a count, so if you write one it looks like

1 | field_one
  | field_two
  | field_three

Then if you added another it would look like

1 | field_one
  | field_two
  | field_three
2 | field_one
  | field_two
  | field_three

and so on...

I'm trying to think where you'd go about doing this, I can't see anything that counts out how many values it has when looking at the $form array in the page itself, and I wouldn't have thought it'd be something that you can do via a template file.

Can this be done?

0

Wow that was much harder to do than I really thought it would be, for anyone interested in how to do this, you can use the information below, in case anyone is interested I've taken most of the information from this site about how to remove the drag handles, but the principle is the same, added to which, I wanted to remove the drag handles anyway

First you have to create your own module, and use hook_theme_registry_alter to change where the default theming is defined for multiple value fields

/**
 * Implements hook_theme_registry_alter().
 */
function mymodule_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['field_multiple_value_form'])) {
    $theme_registry['field_multiple_value_form']['type'] = 'module';
    $theme_registry['field_multiple_value_form']['theme path'] = drupal_get_path('module', 'mymodule');
    $theme_registry['field_multiple_value_form']['function'] = 'mymodule_theme_field_multiple_value_form';
  }
}

Then you can re-define how it's themed by copying the existing theme_field_multiple_value_form code and writing it into your new function. BUT, be aware that this is going to redefine how ALL unlimited field values work, so really you just want to add in an 'if' clause somewhere, I just put mine at the start

function mymodule_theme_field_multiple_value_form($variables) {
  $element = $variables['element'];
  $output = '';

  if (($element['#cardinality'] > 1 || $element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) && $element['#field_name'] != 'field_myfield') {

  // Input normal code, this is for any unlimited input field you DON'T want altering

  } else if (($element['#cardinality'] > 1 || $element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) && $element['#field_name'] == 'field_myfield') {

$table_id = drupal_html_id($element['#field_name'] . '_values');
$required = !empty($element['#required']) ? theme('form_required_marker', $variables) : '';

$header = array(
  array(
    'data' => '<label>' . t('!title !required', array('!title' => $element['#title'], '!required' => $required)) . "</label>",
    'colspan' => 2,
    'class' => array('field-label'),
  ),
);
$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');

$count = 1;
// Add the items as table rows.
foreach ($items as $key => $item) {
  unset($item['_weight']);
  $cells = array(
    array(
      'data' => $count,
      'class' => array('count'),
    ),
    drupal_render($item),
  );
  $rows[] = array(
    'data' => $cells,
  );

  $count++;
}

$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>';


  } else {
      foreach (element_children($element) as $key) {
        $output .= drupal_render($element[$key]);
      }
    }

    return $output;
  }

Hope this helps someone in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.