Cannot use Twig and Token

Created on 28 July 2024, 9 months ago
Updated 29 July 2024, 9 months ago

Hello,

When enabling io ajax, if the block involves Twig and Token, they will become invalid, such as [current-page:title], {{ drupal_title() }}. Is there a way to solve this problem, because the URL of the page has changed to /io/block? Can the Twig template of the current page be introduced into /io/block, so that the Twig syntax and Token of the current page can be used in /io/block.

Tip: The URL of the current page has become a referer URL of /io/block.

💬 Support request
Status

Postponed: needs info

Version

2.0

Component

Miscellaneous

Created by

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

Comments & Activities

  • Issue created by @msn5158
  • Also, using io ajax in blocks of ctools and fieldblock , they cannot load the block content.

  • Status changed to Postponed: needs info 9 months ago
  • 🇮🇩Indonesia gausarts

    Thank you.

    > Twig and Token, they will become invalid...
    Not sure, but worth a try. If using Views UI to create the block, please disable Cache options at the right hand side of Views UI. Most AJAX requests are required on demand as you can see by pressing F12, and inspect element, and scroll to the bottom near closing BODY tag. Any assets, JS or CSS, are requested directly, and never printed on the page due to the nature of dynamic request. It relies mostly in browsers cache mechanism instead.

    > blocks of ctools and fieldblock...
    As noted at project requirements, IO block requires a block.html.twig which preserves container attributes and expects {{ content }} variables printed in a Twig template. It will fail if they are not there. You may want to check and verify those particular block templates yourself.

    Both are the current limitations we might consider to improve if any are doable.

    Let me know your findings?

  • I use Twig and token in custom blocks (/admin/content/block), It get the path is /io/block by token [current-page:url:path] and twig {{ path('<current>') }}, It treats /io/block as the current page.

  • 🇮🇩Indonesia gausarts

    > I use Twig and token in custom blocks
    Makes sense. If using Views UI, Views normally parses/replaces token for you. However for custom codes, you may want to use twig_tweak module, either using drupal_token function, or token_replace filer. Check out its docs for correct usages.

  • Yes, I am using this module and inline_formatter_field .

    What I mean is, it cannot retrieve data from the current page context if enable io ajax.

    AJAX not enabled, available Twig in inline_formatter_field of custom block: {{ drupal_title() }} and {{ path('<current>') }}, They work normally and meet expectations.

    But, if ajax enabled, {{ drupal_title() }} unable to obtain the current page title, and {{ path('<current>') }} get the incorrect path (/io/block), not the current page path (/mypath/123).

  • After testing, it wasn't can't use tokens and Twig, It only can't obtain contextual data for the current page.

    When enabling AJAX:

    The global token is working properly: such as [site:name], but not include [current-page:title] of current page.

    Twig is working normally: such as {{ drupal_breadcrumb() }}, but not include {{ drupal_title() }} to get current page title.

  • In addition, there's a question,

    Edit in Node:

    <h2>This is the Twig template from the node</h2>
    <h2>I can use {{ '{{ node.nid.value }} = ' ~ node.nid.value }}</h2>
    {{ dump() }}

    Edit in custom block library:

    <h2>This is the Twig template from custom block library</h2>
    <h2>I can use {{ '{{ block_content.id.value }} = ' ~ block_content.id.value }}</h2>
    <h2>But, I can't use {{ '{{ node.nid.value }} in custom block library (it will nothing print): ' ~ node.nid.value }}</h2>
    <h2>What should I do to achieve it like this</h2>
    {{ dump() }}

    See the picture:
    How do I use the node Twig template in a custom block library, How to import for use?

    Thank you.

  • 🇮🇩Indonesia gausarts

    > only can't obtain contextual data for the current page.
    You need to put the context, hence node, into the token_replace arguments, as seen here:
    https://git.drupalcode.org/project/twig_tweak/-/blob/3.x/src/TwigTweakEx...

    Not sure if relevant, specific for title, there is a specific filter drupal_title like here:
    https://git.drupalcode.org/project/twig_tweak/-/blob/3.x/src/TwigTweakEx...
    But I guess, using the first should be more contextual.

Production build 0.71.5 2024