Twigtweak breaks preview, also TypeError: Drupal\twig_tweak\TwigTweakExtension::drupalField(): Argument #3 ($id) must be of type string, null given

Created on 8 June 2023, over 1 year ago

We were surprised to find one and only one content type could not be previewed. When changes to this content type were made and the editor clicked on "preview", instead of seeing a preview of their changes they would see the existing content β†’ . No errors were reported.

We finally discovered if we created a new node and clicked on preview a typeerror would be returned, "TypeError: Drupal\twig_tweak\TwigTweakExtension::drupalField(): Argument #3 ($id) must be of type string, null given". Curiously the author can still save this node and the node displays properly, the error only occurs when attempting to preview pointing to a bug in twigtweak.

Commenting out these lines of twig bypasses both the error and the issue with not being able to preview.

{{ drupal_field('body', 'node', node.id) }}

{{ drupal_field('field_announcement', 'node', node.id) }}

There does not appear to be anything the slightest bit unusual about these fields. Body is text, formatted long with summary, announcement is text, formatted long.

I've not been able to figure out alternative twig for these fields. For example if for body I use {{ node.body.value|raw }} instead then it strips out embedded media entities.

Is there a way I can tweak the bolded lines above to prevent this error? Thanks!!

πŸ’¬ Support request
Status

Closed: duplicate

Version

3.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States plato1123

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

Comments & Activities

  • Issue created by @plato1123
  • πŸ‡·πŸ‡ΊRussia Chi

    Note the third parameter of drupal_field(). It must be ID of the node. When previewing a new node the ID is NULL because the node is not saved to database yet. If you have access to node object in your template, it's always better to use view filter instead.

    Example:

    {{ node.body|view}}
    {{ node.field_announcement|view }}
    
  • Status changed to Closed: duplicate over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States plato1123

    Just to confirm, there is a problem even when this node has already been saved. This breaks the ability to preview and instead of seeing a preview the editor sees the last saved copy. What's more there is no error message but the preview is broken. Because there was no error it took quite some time to track this problem down to twigtweak.

    PS it seems like in this circumstance I can get away with using
    {{ content.body }}
    and
    {{ content.field_announcement }}

    Thanks so much for your help!!!

  • πŸ‡·πŸ‡ΊRussia Chi

    a preview the editor sees the last saved copy.

    That is expected behavior. drupal_field always loads node from database, it does not care of what you are previewing.
    This function is typically used when the node variable is not available. Otherwise {{ node.field_NAME|view }} is preferable way to render a node.

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

    Awesome, I appreciate your fast reply and for upping my twig game!

Production build 0.71.5 2024