Is there any way to pass the token from the current paragraph to the view?

Created on 12 July 2018, over 6 years ago
Updated 26 August 2024, 3 months ago

Hello. I created a few fields in the paragraph and also attached viewsreference field.
Since the node can have a few similar paragraphs, I want to display only own content related to the current paragraph.
However when I am trying to use "[paragraph:id]", nothing happen.

I have checked alterView() and seams as the viewsreference module are looking for tokens from the node and maybe will work in some cases, but I don't know how to pass a current delta value in the token, example [node:field_content_bottom:0:target_id].

Anyway or idea how to fix it?
Thanks in advance.

πŸ’¬ Support request
Status

Needs work

Version

2.0

Component

User interface

Created by

πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡±Netherlands seanB Netherlands

    In πŸ’¬ Parent field entity in plugin interface Fixed we have a patch to pass the parent entity data to the view using $view->element['#viewsreference'] so other (custom) views alter hooks can do with the data whatever they want. This would at least allow a view alter to set arguments based on the parent entity.
    This does not add token support though. Maybe we need to add support here for a custom "parent_entity" token?

  • Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Waiting for branch to pass
  • @ruslan-piskarov opened merge request.
  • Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Waiting for branch to pass
  • Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Waiting for branch to pass
  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    Rerolled for dev version

  • Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Waiting for branch to pass
  • πŸ‡ΊπŸ‡ΈUnited States aaronpinero

    Patch #34 does not cleanly apply to the latest beta (6). I didn't check #40. I've been depending on patch #34 for quite a while now, it would be great if we could get an updated patch (or, better yet, something permanent merged).

    Thanks.

  • Status changed to Needs work about 1 year ago
  • πŸ‡³πŸ‡±Netherlands seanB Netherlands

    I would accept a patch that adds a custom token for the parent entity data passed in via $view->element['#viewsreference'].
    See πŸ’¬ Parent field entity in plugin interface Fixed :

         $view->element['#viewsreference'] = [
           'data' => $unserialized_data,
           'enabled_settings' => $unserialized_enabled_settings,
    +      'parent_entity_type' => $parent_entity_type,
    +      'parent_entity_id' => $parent_entity_id,
    +      'parent_field_name' => $parent_field_name,
         ];
    

    The latest patch and the MR uses the node from the route argument, which is not necessarily the parent entity (eg. when using paragraphs or layout builder). This also doesn't work for non-node entities.

  • πŸ‡ΊπŸ‡ΈUnited States ndewhurst USA

    It seems that #40 applies to the latest release but causes an error with $items->getEntity() because $items does not exist in the lazyBuilder method.
    Here is an updated patch that loads the entity based on the (newly?) provided parent_entity_* vars.

    Note: I am not familiar with this module and I'm sure this is not the best solution. You probably don't need to pass the entity everywhere if the parent entity type and ID are available. I'm just trying to maintain compatibility in a project that has some dependencies declaring that they need this patch.

  • πŸ‡ΊπŸ‡ΈUnited States aaronpinero

    I tried applying patch #43 to the beta6 release via composer. The patch applied cleanly, but my pages using the views reference field displayed the WSOD. The log recorded the following error:

    AssertionError: When a #lazy_builder callback is specified, no properties can exist; all properties must be generated by the #lazy_builder callback. You specified the following properties: #attached. in assert() (line 334 of /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php)
    #0 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(334): assert(false, 'When a #lazy_bu...')
    #1 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender(Array, false)
    #2 /var/www/html/web/core/modules/views/src/Plugin/views/field/EntityField.php(934): Drupal\Core\Render\Renderer->render(Array)
    #3 /var/www/html/web/core/modules/views/src/Plugin/views/field/FieldPluginBase.php(1170): Drupal\views\Plugin\views\field\EntityField->render_item(0, Array)
    #4 /var/www/html/web/core/modules/views/views.theme.inc(238): Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object(Drupal\views\ResultRow))
    #5 [internal function]: template_preprocess_views_view_field(Array, 'views_view_fiel...', Array)
    #6 /var/www/html/web/core/lib/Drupal/Core/Theme/ThemeManager.php(287): call_user_func_array('template_prepro...', Array)
    #7 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\Core\Theme\ThemeManager->render('views_view_fiel...', Array)
    #8 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender(Array, false)
    #9 /var/www/html/web/core/modules/views/src/Plugin/views/field/FieldPluginBase.php(1746): Drupal\Core\Render\Renderer->render(Array)
    #10 /var/www/html/web/core/modules/views/src/Plugin/views/style/StylePluginBase.php(777): Drupal\views\Plugin\views\field\FieldPluginBase->theme(Object(Drupal\views\ResultRow))
    #11 [internal function]: Drupal\views\Plugin\views\style\StylePluginBase->elementPreRenderRow(Array)
    #12 /var/www/html/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(101): call_user_func_array(Array, Array)
    #13 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(788): Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_ren...', 'exception', 'Drupal\\Core\\Ren...')
    #14 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(374): Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array)
    #15 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender(Array, false)
    #16 /var/www/html/web/core/modules/views/src/Plugin/views/style/StylePluginBase.php(716): Drupal\Core\Render\Renderer->render(Array)
    #17 /var/www/html/web/core/modules/views/src/Plugin/views/style/StylePluginBase.php(582): Drupal\views\Plugin\views\style\StylePluginBase->renderFields(Array)
    #18 /var/www/html/web/core/modules/views/src/Plugin/views/style/StylePluginBase.php(473): Drupal\views\Plugin\views\style\StylePluginBase->renderGrouping(Array, Array, true)
    #19 /var/www/html/web/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php(2169): Drupal\views\Plugin\views\style\StylePluginBase->render(Array)
    #20 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1538): Drupal\views\Plugin\views\display\DisplayPluginBase->render()
    #21 /var/www/html/web/core/modules/views/src/Plugin/views/display/Block.php(131): Drupal\views\ViewExecutable->render()
    #22 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1635): Drupal\views\Plugin\views\display\Block->execute()
    #23 /var/www/html/web/core/modules/views/src/Element/View.php(81): Drupal\views\ViewExecutable->executeDisplay('block_3', Array)
    #24 /var/www/html/web/core/modules/views/src/Plugin/Block/ViewsBlock.php(59): Drupal\views\Element\View::preRenderViewElement(Array)
    #25 /var/www/html/web/core/modules/block/src/BlockViewBuilder.php(171): Drupal\views\Plugin\Block\ViewsBlock->build()
    #26 [internal function]: Drupal\block\BlockViewBuilder::preRender(Array)
    #27 /var/www/html/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(101): call_user_func_array(Array, Array)
    #28 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(788): Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_ren...', 'exception', 'Drupal\\Core\\Ren...')
    #29 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(374): Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array)
    #30 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(446): Drupal\Core\Render\Renderer->doRender(Array)
    #31 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender(Array, false)
    #32 /var/www/html/web/core/lib/Drupal/Core/Template/TwigExtension.php(479): Drupal\Core\Render\Renderer->render(Array)
    #33 /var/www/html/web/sites/default/files/php/twig/64f8e0a2e7225_page.html.twig_e7hh4y0wifva4a_ScRFDQ36Sd/zfFVEjIYHqdRX1anIHqxRCGpcFxi4utbCkf16f__ry0.php(147): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
    #34 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_4c484e285e34c1c517dfce0f56403aae->doDisplay(Array, Array)
    #35 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
    #36 /var/www/html/vendor/twig/twig/src/Template.php(390): Twig\Template->display(Array)
    #37 /var/www/html/web/core/themes/engines/twig/twig.engine(55): Twig\Template->render(Array)
    #38 /var/www/html/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/h...', Array)
    #39 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\Core\Theme\ThemeManager->render('page', Array)
    #40 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender(Array, false)
    #41 /var/www/html/web/core/lib/Drupal/Core/Template/TwigExtension.php(479): Drupal\Core\Render\Renderer->render(Array)
    #42 /var/www/html/web/sites/default/files/php/twig/64f8e0a2e7225_html.html.twig_K7e0YUqNYNYFXZjaSejrhWCNJ/F9GmlgEUaQj5BFA8BJiV1mhKLHdT20aW1lYKJfOlhzk.php(87): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
    #43 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_e64b960fb8704841d8453d0d3f187c3c->doDisplay(Array, Array)
    #44 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
    #45 /var/www/html/vendor/twig/twig/src/Template.php(390): Twig\Template->display(Array)
    #46 /var/www/html/web/core/themes/engines/twig/twig.engine(55): Twig\Template->render(Array)
    #47 /var/www/html/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/h...', Array)
    #48 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\Core\Theme\ThemeManager->render('html', Array)
    #49 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender(Array, false)
    #50 /var/www/html/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(162): Drupal\Core\Render\Renderer->render(Array)
    #51 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
    #52 /var/www/html/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(163): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
    #53 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
    #54 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
    #55 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
    #56 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(174): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view')
    #57 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
    #58 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #59 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #60 /var/www/html/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #61 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #62 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #63 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #64 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #65 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
    #66 {main}

  • First commit to issue fork.
  • πŸ‡«πŸ‡·France yohansenso

    Patch #43 works well for me, I only added support for translated parent entity (it seems like the default translation was the one always used).

    NB: This is my first contribution, any advice is appreciated!

  • πŸ‡«πŸ‡·France yohansenso

    Patch #43 works well for me, I only added support for translated parent entity (it seems like the default translation was the one always used).

    NB: This is my first contribution, any advice is appreciated!

  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

    Ruslan Piskarov β†’ changed the visibility of the branch 2985364- to hidden.

  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

    I suggest working on a different approach as mentioned @seanB at #42.
    Since parent_entity_type and parent_entity_id already are available, we can fix it more easily.
    Not sure I covered all cases.
    Need review.
    Works with 2.0-beta7.

  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

    Added support for a translated parent entity too.

  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine
  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

    Improvered. Covered more cases, like [paragraph:field_taxonomy_ref:target_id].

  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

    Fixed the issue when a view has ajax enabled and pagination doesn't work.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    1 pass
  • πŸ‡ΊπŸ‡ΈUnited States jacobbell84

    Patches 51 and 52 aren't working for me. I'm confused on some of the changes there. Patch 51 mentioned that it covers more bases, but the only change I see was this:

    +      elseif (isset($view->element['#viewsreference']['entity'])
    +        && $view->element['#viewsreference']['entity'] instanceof EntityInterface) {
    +        $replacements[$view->element['#viewsreference']['entity']
    +          ->getEntityTypeId()] = $view->element['#viewsreference']['entity'];
    +      }
    

    $view->element['#viewsreference']['entity'] is a holdover from an older patch though, it's not being set anywhere in this patch. Then 52 adds an additional check to fix ajax pagination:

    -  $viewsreference_plugin_manager = \Drupal::service('plugin.manager.viewsreference.setting');
    -  $plugin_definitions = $viewsreference_plugin_manager->getDefinitions();
    -  if (isset($view->element['#viewsreference']['enabled_settings'])) {
    +  if (isset($view->element['#viewsreference']['entity'])) {
    +    $viewsreference_plugin_manager = \Drupal::service('plugin.manager.viewsreference.setting');
    +    $plugin_definitions = $viewsreference_plugin_manager->getDefinitions();
    

    Because of that check, and ['entity'] never being set, these plugins never execute for me anymore.

  • πŸ‡ΊπŸ‡ΈUnited States generalredneck

    echoing @jacobbell84.
    I fixed it by changing the check in hhok_views_pre_view like so:

        // Let all settings plugins alter the view.
    -   if (isset($view->element['#viewsreference']['entity'])) {
    +   if (isset($view->element['#viewsreference'])) {
    

    I don't see where the ['entity'] key is being set anywhere either. I don't know if this is 100% correct. thoughts?

  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

    If I remember correctly the ['entity'] will appear if embed a view with enabled ajax in pagination.
    First time it mentioned at #10.
    I can't test today. Need improve a patch.

  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    No longer works with the dev version

  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Thanks everyone for the work on this! To help review it would be good to have a single branch, and other branches hidden as its unclear at the moment (+ mix of paches).

    Looking at 2985364-pass-token-from-paragraph branch, it would be good for someone to explain better to me the aim here. From the issue summary, it seems it should be isolated to the ViewsReferenceArgument setting, but all settings are changed? So maybe issue summary needs an update, or scope needs to be more limited.

    Now that we have test coverage everywhere, any merge requests coming in would also need to have test coverage please.

    Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    Reroll of 52 to apply to latest version of module

  • πŸ‡ΊπŸ‡ΈUnited States jv24

    #58 looks like it was just missing the $token_service variable. Updated patch to include it

  • First commit to issue fork.
  • πŸ‡¨πŸ‡­Switzerland das-peter

    das-peter β†’ changed the visibility of the branch 2985364-updates-for-beta6 to hidden.

  • πŸ‡¨πŸ‡­Switzerland das-peter

    Review of: #59

    1. +++ b/src/Plugin/ViewsReferenceSetting/ViewsReferenceArgument.php
      @@ -77,11 +78,32 @@
      +      $token_service = \Drupal::token();
             if (is_array($arguments)) {
               foreach ($arguments as $index => $argument) {
                 if (!empty($this->tokenService->scan($argument))) {
      -            $arguments[$index] = $this->tokenService->replace($argument, ['node' => $node]);
      +            $arguments[$index] = $token_service->replace($argument, $replacements);
      

      Why are we getting the token service while we already have the service via DI? It is even used in the condition before the replacement.

    2. +++ b/viewsreference.module
      @@ -95,9 +95,9 @@ function viewsreference_views_pre_view(ViewExecutable $view, $display_id, array
      -  $viewsreference_plugin_manager = \Drupal::service('plugin.manager.viewsreference.setting');
      -  $plugin_definitions = $viewsreference_plugin_manager->getDefinitions();
      -  if (isset($view->element['#viewsreference']['enabled_settings'])) {
      +  if (isset($view->element['#viewsreference']['entity'])) {
      +    $viewsreference_plugin_manager = \Drupal::service('plugin.manager.viewsreference.setting');
      +    $plugin_definitions = $viewsreference_plugin_manager->getDefinitions();
      

      Seems unnecessary - the ['entity'] addition came from the other approach it seems (pre #48).

    If I get the general consensus the new approach started in #48 is the preferred one.
    I'd agree with that since that approach needs less changes and seems to work in a generic approach while maintaining backward compatibility.
    That said - I've created new branches in the fork:

    • 2985364-pass-token-from-paragraph-light-approach: Patch from #59 with the changes I mentioned above
    • 2985364-pass-token-from-paragraph-legacy-approach: Pre-#48 code

    The MR branch 2985364-pass-token-from-paragraph contains now the light approach

    @scott_euser There should be now only one Branch / MR - I've also added an update to the issue summary. Haven't gotten around to add tests yet - hence keeping on needs work.

  • πŸ‡¨πŸ‡­Switzerland das-peter

    das-peter β†’ changed the visibility of the branch 2985364-pass-token-from-paragraph-light-approach to hidden.

  • πŸ‡¨πŸ‡­Switzerland das-peter

    das-peter β†’ changed the visibility of the branch 2985364-pass-token-from-paragraph to hidden.

  • πŸ‡¨πŸ‡­Switzerland das-peter

    das-peter β†’ changed the visibility of the branch 2985364-pass-token-from-paragraph to active.

  • πŸ‡¨πŸ‡­Switzerland das-peter

    das-peter β†’ changed the visibility of the branch 2985364-pass-token-from-paragraph-legacy-approach to hidden.

Production build 0.71.5 2024