Allow caption tokens from parent (paragraph entity)

Created on 14 November 2024, 5 months ago

Problem/Motivation

Having lean media for reusability is a common practice I've been seeing lately, you create a Media bundle with the minimum so it can be reused and then the configurable bit lives on paragraphs (or other), but mainly paragraphs.

This module has already clever implementations for parent paragraphs on the gallery side, but it would be nice to have paragraph token replacement too.

Proposed resolution

Add paragraph as a token entity on the formatter form, then load it if exists when the replace happens.

📌 Task
Status

Active

Version

1.0

Component

Code

Created by

🇪🇸Spain pcambra Asturies

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @pcambra
  • 🇪🇸Spain pcambra Asturies

    Working on a patch

  • 🇪🇸Spain pcambra Asturies

    If this is an acceptable solution, I'll add an equivalent MR to https://www.drupal.org/project/glightbox_media_video

  • 🇪🇸Spain pcambra Asturies
  • 🇪🇸Spain pcambra Asturies
    • pcambra committed 9c2df031 on 1.0.x
      Issue #3487478: Allow caption tokens from parent (paragraph entity)
      
  • 🇷🇸Serbia levmyshkin Novi Sad, Serbia

    Hi pcambra, it will be tricky to define 'mainly' paragraph automatically. Glightbox image formatter which is being applied to Image field can be referenced to Media entity first and Media field attached in Paragraph directly or in nested paragraph with Gallery and Gallery Item paragraph types. Image field can be included in 'mainly' Gallery paragraph type directly. Paragraph module is not limited by any structure or nested levels.
    It's possible to prepare tokens for specific (common, most popular) cases:
    1. Multiple image field inside 'mainly' Paragraph (without Media).
    2. Single image field in paragraph Gallery Item and Gallery 'mainly' parent paragraph type (without Media).
    3. Multiple media field inside 'mainly' Paragraph.
    4. Single media field in paragraph Gallery Item and Gallery 'mainly' parent paragraph type.

    It can be one token 'Parent title', but here is another problem, which field must be selected for this token. Gallery id is being generated automatically without additional complexity with Text field at the end.

    It can be token by reference:
    referenece_field_name:parent_entity:reference_field_name:parent_entity:text_field_name

    From first glance, it should be straightforward, but I'm afraid that Drupal doesn't support parent entity, because of circullar tokens (parent entities support children fields tokens, children fields tokens support parent fields tokens) and it must be tricky too:
    https://www.drupal.org/project/paragraphs/issues/3067265 Paragraph templates: unable to access parent-node tokens from child-paragraphs in the default installed setup Needs review

    Here is a custom solution to create own site specific token:
    https://drupal.stackexchange.com/questions/254406/how-to-get-token-of-pa...

    I can create only 4 tokens for cases above, then it will be possible to select field for token, but site editor must be aware which token relates for his own site depends on its structure.

  • 🇷🇸Serbia levmyshkin Novi Sad, Serbia

    I merged your request in 1.0.x-dev. I will try to add tokens on the next week.

  • 🇷🇸Serbia levmyshkin Novi Sad, Serbia
  • 🇷🇸Serbia levmyshkin Novi Sad, Serbia
  • Status changed to Fixed about 2 months ago
  • 🇩🇪Germany zany

    I have e.g. a "gallery" paragraph with "field_gallery_items" and "field_label", and a (nested) "gallery_items" paragraph with "field_image" and "field_description".

    I want to use the "field_description" (which is alongside the "field_image") as custom caption description.

    I notice the following: I can use "[paragraph:id]" (shows the same value on all glightbox images) and "[paragraph:field_label:value]" for custom caption and custom caption description (from the parent paragraph).
    But I can not use "[paragraph:field_description:value]" (from the direct paragraph containing this "field_image").

    Is the paragraph token supposed to be the direct paragraph entity containing the glightbox formatted field? Or is this intentionally the parent paragraph?

  • 🇩🇪Germany zany

    On closer inspection I see the issue with my use-case: glightbox.theme.inc properly sets
    $data = [$entity_type => $entity, 'file' => $item]; in line 303 -- which in my case sets a "paragraph" entity.
    But line 307 overrides this with $data['paragraph'] = $parent; -- should this maybe set parent or such instead of paragraph (which hides the current entity)?

  • 🇷🇸Serbia levmyshkin Novi Sad, Serbia

    Hi pcambra, I updated tokens for GLightbox paragraphs, please, use [paragraph_container:field_name] token for your Image/Media field:
    https://www.drupal.org/project/glightbox/issues/3511270#comment-16018183 🐛 Allow caption tokens paragraph access if there is a parent paragraph Active

Production build 0.71.5 2024