Anybody interested to have the field-collection field delta as token?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

osopolar created an issue. See original summary.

osopolar’s picture

Status: Active » Needs review
FileSize
1 KB

I modified the field-collection module. Later I realized that it also could be done in a custom module, and as I need delta + 1, I finally implemented it there. If anybody is interested in that, here comes the patch and also the code for the custom module.

/**
 * Implements hook_token_info_alter().
 */
function custom_token_info_alter(&$info) {
  // Add delta + 1 token.
  $info['tokens']['host']['delta_plus_1'] = array(
      'name' => t('Host entity item delta + 1'),
      'description' => t('The delta of the reference pointing to this field collection item + 1 to count naturally.'),
    );
  return $info;
}

/**
 * Implements hook_tokens().
 */
function custom_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  // Replacement for token [host:delta_plus_1].
  if (($type == 'host') and !empty($data['field_collection_item'])) {
    $collection = $data['field_collection_item'];
    if (!empty($tokens['delta_plus_1'])) {
      // delta + 1 and leading zeros, if delta has only one digit.
      $replacements[$tokens['delta_plus_1']] = sprintf('%02d', 1 + $collection->delta());
    }
  }

  return $replacements;
}
Chris Matthews’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #2 is 3 years old, but still applied cleanly to the latest 7.x-1.x-dev and is valuable to have delta value available as a token so after reviewing/testing I'm changing the status to RTBC.

Checking patch field_collection.tokens.inc...
Hunk #2 succeeded at 100 (offset -4 lines).
Applied patch field_collection.tokens.inc cleanly.

  • ram4nd committed 00506d1 on 7.x-1.x authored by osopolar
    Issue #2681291 by osopolar: Token for fieldcollection delta
    
ram4nd’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.