3

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?

0

2 Answers 2

9

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;
}
4
  • 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. Jan 25, 2018 at 8:58
  • Could you provide some more detail about the what the token_service section does? I assume it is trying to get sub-tokens for the parent entity EG. [paragraph:parent_entity:field_body]? If so though it is not working for me. $parent_entity_tokens is an empty array.
    – lolcode
    May 26, 2020 at 16:26
  • The above code does work great to provide all the entity sub tokens but unfortunately it only shows the new parent token in the token browser.
    – liquidcms
    Apr 26, 2022 at 6:47
  • Kudos for @Clive, I've added this as MR on this issue drupal.org/project/paragraphs/issues/3067265
    – d70rr3s
    May 1, 2022 at 14:40
4

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].

6
  • Could you please provide a link to the corresponding Drupal issue? Jun 3, 2019 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) Jun 3, 2019 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. Jun 4, 2019 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 Jun 4, 2019 at 6:39
  • 1
    On 9.2.11 I don't see a parent_entity token.
    – liquidcms
    Mar 28, 2022 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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