1

I have a paragraph, which is referenced by a node. Now I am trying to pass field values of node to the paragraph using tokens. I'm only aware of [paragraph:parent_id] and [paragraph:parent_type] but don't know how to get specific field values.

What I've tried so far without success:
[node:field_foo:value]
[paragraph:field_foo:value]
[paragraph:node:field_foo:value]
[paragraph:parent:field_foo:value]
[paragraph:parent:entity:field_foo:value]

The list of available tokens does not contain parent tokens. Is it even possible to get token of the parent entity? And if so, how?

4

There's nothing out of the box (see this issue for the details). Implementing your own token isn't too tricky.

I haven't had a chance to test this but I'm pretty sure it'll work:

function MODULE_token_info_alter(&$info) {
  $info['tokens']['paragraph']['parent_entity'] = [
    'name' => 'Parent entity',
    'description' => 'Parent entity of the paragraph',
  ];
}

function MODULE_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];

  if ($type == 'paragraph' && $paragraph = $data['paragraph']) {
    $parent = $paragraph->getParentEntity();
    $bubbleable_metadata->addCacheableDependency($parent);

    foreach ($tokens as $name => $original) {
      if ($name == 'parent_entity') {
        $replacements[$original] = $parent->label();
      }
    }

    $token_service = \Drupal::token();
    if ($parent_entity_tokens = $token_service->findWithPrefix($tokens, 'parent_entity')) {
      $replacements += $token_service->generate($parent->getEntityTypeId(), $parent_entity_tokens, [$parent->getEntityTypeId() => $parent], $options, $bubbleable_metadata);
    }
  }

  return $replacements;
}
  • I did not found an issue regarding version 8.x, so I was hoping that parent tokens might be available out of the box. But your code does work. Thanks for introducing me in custom tokens. – Philipp Michael Jan 25 '18 at 8:58
2

So with the newest version of Drupal 8 and Paragraphs, this is possible.

Looking at the tokens on /admin/help/token, there is a [paragraph:parent_entity] under the paragraphs section which is exactly what you need.

Example use case: to get the title of the parent node, I used [paragraph:parent_entity:title].

  • Could you please provide a link to the corresponding Drupal issue? – Philipp Michael Jun 3 '19 at 12:21
  • @PhilippMichael I'm not sure what you mean. There was no drupal issue that I know about; I just went to /admin/help/token/ and the token was listed. Do you mean the issue mentioned in Clive's answer? That was actually a Drupal 7 issue (I couldn't find any issues about this for Drupal 8) – Avery Green Jun 3 '19 at 16:57
  • No bugfix or feature without a Drupal issue. Just wanted to know if it was already available when I asked this question or solved in the meantime. Nevermind. – Philipp Michael Jun 4 '19 at 6:10
  • Can't find [paragraph:parent_entity:title] but [paragraph:parent_field_name:value] using Paragraphs 8.x-1.8 and Token 8.x-1.1 – Philipp Michael Jun 4 '19 at 6:39
  • none of the paragraph:parent entities work in Drupal 8.6.10 – Laurence Fass Dec 12 '19 at 10:46

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.