Hey, thanks for this great module!

I've run into a issue where I have a Paragraph bundle that contains image fields. I'm using the File Field paths module along with ImageField Tokens and would like to use tokens from the node on the File name and Alt tag. When setting up the image field inside the bundles, I am unable to specify any tokens from the node that the bundle will be displayed on.

Is this something that can currently be achieved I may have overlooked? Or is this somethign you would consider adding?

There is a similar issue over in the Field Collection issue queue.

Thanks

Comments

Volker23’s picture

I just found out that tokens are not really supported. Please count me in, i'd love to see that feature in paragraphs, which is my favorite content structure module right now. Thanks for your work!

Alexandre360’s picture

+1 to this.

Example of usefulness of this :

  • Create a "Slider" paragraph bundle
  • Inside that bundle, create a field collection that contain the following fields : image, caption, url...
  • Install and enable the filefield path, token, entity token module

Now you probably want to have logic file path for your images in your slider, for example : /images/sliders/[node:title]. But it's not possible because you can't get access to the node title that handle the slider paragraph you just created.

aitala’s picture

I would find it useful as well..

Thanks,

Eric

onehandsomedog’s picture

+1 Would lke this available

Nchase’s picture

also intersting in conjunction with filefield_paths. At the moment you can't use node tokens to rename your images / files.

Etroid’s picture

+1 This would be a very useful feature.

arefen’s picture

this is very useful. please add this feature

Murz’s picture

I have found workaround for insert node tokens in paragraph_item:

  1. Install and enable modules: Entity tokens, Token Filter, Entity Reference, Entity Reference Prepopulate Token.
  2. Add entity_reference field to your Paragraph bundle with machine name field_node_reference.
  3. Create new Paragraphs item with this bundle, and select current host node in field_node_reference field.
  4. Insert in Paragraph item body field tokens with format: [paragraphs_item:field-node-reference:<node field>], here is some examples:
    • [paragraphs_item:field-node-reference:title]
    • [paragraphs_item:field-node-reference:nid]
    • [paragraphs_item:field-node-reference:field_image]
  5. As result, those tokens will be replaced to host node values.
delacosta456’s picture

hi nice @Murz

But i am not sure this is going to help when in a case like the one mentioned by @Nchase where , you need to configure the url with token

Murz’s picture

@delacosta456, token like [paragraphs_item:field-node-reference:url] must work well - can you try this?

delacosta456’s picture

@ Murz

if i remember Prepopulating with ER Prepopulate Token ik ok for filling field in form..

The filefield_paths case is when configuring a file field on file settings page.

Murz’s picture

@delacosta456, I test this case and all works well - I setup [paragraphs_item:field-node-reference:nid].[file:ffp-extension-original] in filefield file name settings, and try to upload image into paragraph item image field, and it successfully renamed to host node nid.

But my example is not the solution for this issue, this is only workaround for this problem.

Good solution will be populate host node tokens in attached paragraphs items automatically by Paragraphs module.

delacosta456’s picture

ok.. i need your workaround but please help me understand some point of you scenario

2. Add entity_reference field to your Paragraph bundle with machine name field_node_reference.
the mchine name should it exactly be field_node_reference ?

3 . Create new Paragraphs item with this bundle, and select current host node in field_node_reference field.
Created the paragraph item mean that previously the paragraphs bundle created has been added as field to a node , right ?

Murz’s picture

2. Add entity_reference field to your Paragraph bundle with machine name field_node_reference.
the mchine name should it exactly be field_node_reference ?

You can use any field machine name, but after this you must use same name in token, so if you create feld_hostnode - you must use [paragraphs_item:field-hostnode:url] token.

3 . Create new Paragraphs item with this bundle, and select current host node in field_node_reference field.
Created the paragraph item mean that previously the paragraphs bundle created has been added as field to a node , right ?

You must create new node with paragraphs field, and save it (because for node reference it must have nid). After this you add paragraph item to the node with you bundle type, and in this paragraph item you must find (via autocomplete) your current node.

After this tokens like [paragraphs_item:field-node-reference:nid] must work in paragraph body field like other tokens.

delacosta456’s picture

hi @Murz
Nice .. i reproduce your instruction and it is more clear...

But .. i just doest not why if i enable the prepopulate checkbox on the reference field added to paragraph bundle..,
and coming on the node that have this paragraph bundle, with argument present in url , the paragraph item reference fiedl is not prepopulated as expected

Murz’s picture

Yes, in all token list this field subitems is not pre-populated, but it works when I directly type those tokens.

delacosta456’s picture

hi @Murz
Solved..

By doing this :

-Install https://www.drupal.org/project/paragraphs_defaults
- On the settings of Paragraph Default , for my paragraph bundle, i Edit Default add an empty paragraph and save so that the form should stay opened to be populated.

- Important, did a Batch Update (Link near to Edit Default)

and after that the field was correctly prepoulated

dedicated_hobby_coder’s picture

In case anyone needs the host node nid or its title "current-page" tokens are available.

Had to place a views field inside a paragraph sitting on a field collection inside a node... ... already dreaming up elaborate ideas of how to get that nid for my views argument when I remember to keep it simple stupid^^

[current-page:url:unaliased:args:value:1] and my view is just perfect^^
https://www.drupal.org/node/390482#token-current-page

maybe... expanding current-page tokens chain to something like [current-page:node:xxx:xxx] is the easier approach... field collections, paragraphs, bricks (?) ..., ..., same issue everywhere, how to get those fields and values by jumping up into the host...

it wouldn't eradicate the need to tokenize values on the paragraphs collections etc... but it would solve many issues in a single blow... and it allows unlimited nesting of entities inside entities

just an idea that the real programmers and developers can pick up for drupal 7.58 or 9 or so^^ ;)

NoComments’s picture

To solve this problem, I created my own tokens in which I received the data I needed and used them.
like this

 <?php
 if ($type == 'global') {

    /** @var ParagraphsItemEntity $par_item */
    $par_item = $data['paragraphs_item'];
    $hostEntity = $par_item->hostEntity();

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'paragraph-node-title':
          if(isset($hostEntity->title)){
            $replacements[$original] = $hostEntity->title;
          }
          break;
        case 'paragraph-node-nid':
          if(isset($hostEntity->nid)) {
            $replacements[$original] = $hostEntity->nid;
          }
          break;
        case 'paragraph-node-type':
          if(isset($hostEntity->type)) {
            $replacements[$original] = $hostEntity->type;
          }
          break;
      }
    }
  }

  return $replacements;
?>
NoComments’s picture

To solve this problem, I created my own tokens in which I received the data I needed and used them.
like this

 <?php
 if ($type == 'global') {

    /** @var ParagraphsItemEntity $par_item */
    $par_item = $data['paragraphs_item'];
    $hostEntity = $par_item->hostEntity();

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'paragraph-node-title':
          if(isset($hostEntity->title)){
            $replacements[$original] = $hostEntity->title;
          }
          break;
        case 'paragraph-node-nid':
          if(isset($hostEntity->nid)) {
            $replacements[$original] = $hostEntity->nid;
          }
          break;
        case 'paragraph-node-type':
          if(isset($hostEntity->type)) {
            $replacements[$original] = $hostEntity->type;
          }
          break;
      }
    }
  }

  return $replacements;
?>
mlncn’s picture

Should there be a separate Drupal 8 issue? Should this issue be set to version 8.x (first)?

meyerrob’s picture

The complete code for a custom token module which replaces the host entity ID (like in [field_collection_item:host]) looks like this:

/**
 * Implements hook_token_info(). This hook will register a new token.
 */
function custom_token_token_info() {
  $info['tokens']['paragraphs_item']['host'] = array(
    'name' => t('Paragraph host ID'),
    'description' => t('Node ID of entity which contains this paragraph'),
  );
  return $info;
}

/**
 * Implements hook_tokens(). This hook will operate the token and replace it with it's value.
 */
function custom_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if ($type == 'paragraphs_item') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'host':
          $par_item = $data['paragraphs_item'];
          $hostEntity = $par_item->hostEntity();
          $replacements[$original] = $hostEntity->nid;
          break;

        default:
          break;
      }
    }
  }
  return $replacements;
}