3

I have a node type A with a entity_reference field named "field_subnode" pointing to a node type B:

A -> B

I'm using pathauto, with the following patterns setted up:

URL path for nodes of type A: books/[node:title]

Now I need something similar to: books/[node-type-A:title]/[node:title] as path for my nodes of type B, to obtain urls like the following:

books/my-car-book/chapter-1

Which token I have to use to obtain what I want?

I've tried different tokens like for example:

books/[node:field-subnode:1:title]/[node:title]

but without success.

Thank you for helping me.

3

You need the Entity Tokens module, part of the Entity project: http://drupal.org/project/entity. Once it's enabled you'll see the available recursive tokens in the tree.

  • Entity Tokens was already active in my site. I've tried any kind of token in the "Show Replacement patterns" area in the "content paths" section of pathauto but without success. Can you please provide a token example based on the info I gave above? Thank you very much – MXT May 30 '12 at 21:24
  • Ah - I think I may have overlooked something, I am using the References module, not Entity Reference. My experience has been that Entity Reference, while supposedly "the future" of reference fields, is still missing a lot of features and community support (although it's been a while since I really gave it a shot). So that's probably the issue. With References, I get tokens like [node:field-program:title]. That would give me the title of a node referenced in a field called field_program. – Daniel Feder Jun 5 '12 at 0:24
0

This is a workaround I've used recently. It requires you to define your own token for a single entityreferene field. Assuming you have a module called mymodule, the entityreference field on the node is called field_subnode this will allow you to use the token chains like [node:subnode:title]. Rename mymodule with your own module name and clear your caches after implementing.

/**
 * Implements hook_token_info().
 */
function mymodule_token_info() {
  return array(
    'tokens' => array(
      'node' => array(
        'subnode' => array(
          'name' => t('Node reference'),
          'description' => t('Node referenced via field_subnode'),
          'type' => 'node',
        ),
      ),
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function mymodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);

  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'subnode' :
          $default = '';
          if (isset($node->field_subnode)) {
             $subnode = entity_metadata_wrapper('node', $node)->field_subnode->value();
            $default = $subnode->title;
          }
          $replacements[$original] = $sanitize ? filter_xss($default) : $default;
          break;
      }
    }

    if (isset($node->field_subnode) && $subnode_tokens = token_find_with_prefix($tokens, 'subnode')) {
      $subnode = entity_metadata_wrapper('node', $node)->field_subnode->value();
      $replacements += token_generate('node', $subnode_tokens, array('node' => $subnode), $options);
    }
  }
  return $replacements;
}
  • Thank you very much for your answer: I made my custom module following your instructions: the custom token appear in the token tree, but it doesn't have any effect when saving the node. Only to be sure: is your code valid for token creation ON node type B, that one that is referenced from A, and that it doesn't contain the field_subnode? (field_subnode is a field belonging to node type A) Thak you very much – MXT Jun 4 '12 at 17:12
0

I have a similar setup with services and sub-services and the following pattern works beautifully. I'm setting this pattern on the sub-service content type where I have a term reference field called field_service linking to its parent service.

services/[node:field-service:title]/[node:title]

In your case, you're setting the reference field on the parent node instead of on the child node. If it's possible, flip this. If not, I'm not aware of a token that you can put in B that will reference the field that is set in A (whereas in my case it's a token in B referencing a field in B) and I guess most probably this won't be available even in future releases for technical difficulties.

I'm using the following:

  • Drupal 7.15
  • entity 7.x-1.0-rc3 (with Entity tokens module enabled)
  • entityreference 7.x-1.0-rc5
  • pathauto 7.x-1.2

Hope this helps.

Cheers.

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.