Call to undefined function Drupal\govdelivery_bulletins\Service\twig_render_template()

Created on 26 March 2025, 12 days ago

Problem/Motivation

If addToQueueAndReset() is called as part of a Views Bulk Operation (VBO) action, the following error occurs:

Call to undefined function Drupal\govdelivery_bulletins\Service\twig_render_template() in Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->buildXml() (line 350 of modules/contrib/govdelivery_bulletins/src/Service/AddBulletinToQueue.php).

Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->buildBulletinData() (Line: 259)
Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->addToQueue(NULL) (Line: 275)
Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->addToQueueAndReset() (Line: 409)
...

Steps to reproduce

1. We've written a custom VBO action plugin that allows us to bulk publish nodes.
2. When we use this action to publish something, it changes the status to 'published' and saves it, which triggers a hook_node_update() function we've written to send an email via GovDelivery.
3. Our hook_node_update calls \Drupal::service('govdelivery_bulletins.add_bulletin_to_queue')->addToQueueAndRest() which triggers the error.

Proposed resolution

It seems that VBO executes code in a slightly different context that doesn't have Drupal's hook_node_update() defined as it is during normal execution. Perhaps there's a way to force that function to be defined or to more fully reference the function so Drupal knows where it's at.

Alternatively, you could probably replace twig_render_template() with \Drupal::service('twig')->render($template, $variables)

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.11

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States RichardDavies Portland, Oregon

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

Merge Requests

Comments & Activities

  • Issue created by @RichardDavies
  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    Thank you for reporting this.

    It looks like the same issue that is described here https://www.jeffgeerling.com/blog/2019/rendering-twig-templates-programm...

    The twig engine is not loaded during VBO.

    Seems like adding the twig service as avia dependency injection is the right way to go and might solve this edge case.

  • πŸ‡ΊπŸ‡ΈUnited States RichardDavies Portland, Oregon

    FYI I was able to create a temporary workaround by modifying my code as follows:

    // Load the Twig theme engine so govdelivery_bulletins can use twig_render_template() when called from a VBO action.
    include_once \Drupal::root() . '/core/themes/engines/twig/twig.engine';
    $twig = \Drupal::service('twig');
    $twig_debug = $twig->isDebug();
    if ($twig_debug) $twig->disableDebug();
    
    \Drupal::service('govdelivery_bulletins.add_bulletin_to_queue')->addToQueueAndReset();
    
    if ($twig_debug) $twig->enableDebug();
    
  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    Interesting. I will push up a MR in a little while. But I will need you to tell me if solves the problem.

  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    @richarddavies can you test the patch from this MR with your setup and let me know if it works?
    https://git.drupalcode.org/project/govdelivery_bulletins/-/merge_request...

  • πŸ‡ΊπŸ‡ΈUnited States RichardDavies Portland, Oregon

    I removed my workaround and applied the patch, but it's not working. Now I get this error during normal execution:

    TypeError: Drupal\govdelivery_bulletins\Service\AddBulletinToQueue::__construct(): Argument #1 ($queue_factory) must be of type Drupal\Core\Queue\QueueFactoryInterface, Drupal\Core\Queue\QueueFactory given, called in /app/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 261 in Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->__construct() (line 191 of modules/contrib/govdelivery_bulletins/src/Service/AddBulletinToQueue.php).
    
    Drupal\Component\DependencyInjection\Container->createService(Array, 'govdelivery_bulletins.add_bulletin_to_queue') (Line: 179)
    Drupal\Component\DependencyInjection\Container->get('govdelivery_bulletins.add_bulletin_to_queue') (Line: 197)
    Drupal::service('govdelivery_bulletins.add_bulletin_to_queue') (Line: 401)
    ...
    

    And I get this error when using my VBO action:

    ArgumentCountError: Too few arguments to function Drupal\govdelivery_bulletins\Service\AddBulletinToQueue::__construct(), 0 passed in /app/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 261 and exactly 2 expected in Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->__construct() (line 191 of modules/contrib/govdelivery_bulletins/src/Service/AddBulletinToQueue.php). 
    
    Drupal\Component\DependencyInjection\Container->createService(Array, 'govdelivery_bulletins.add_bulletin_to_queue') (Line: 179)
    Drupal\Component\DependencyInjection\Container->get('govdelivery_bulletins.add_bulletin_to_queue') (Line: 197)
    Drupal::service('govdelivery_bulletins.add_bulletin_to_queue') (Line: 401)
    ...
    
  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    Thank you. I see the problem. working on a fix.

  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    If you force refresh that patch I think that should solve those errors.

    And again thank you for testing this out because I do not have a current site using this module.

  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida
  • πŸ‡ΊπŸ‡ΈUnited States RichardDavies Portland, Oregon

    Unfortunately, it's still not working. I get this error now during normal execution and VBO action:

    Fatal error: Could not check compatibility between Drupal\govdelivery_bulletins\Service\AddBulletinToQueue::create(Drupal\govdelivery_bulletins\Service\ContainerInterface $container) and Drupal\Core\DependencyInjection\ContainerInjectionInterface::create(Symfony\Component\DependencyInjection\ContainerInterface $container), because class Drupal\govdelivery_bulletins\Service\ContainerInterface is not available in /app/web/modules/contrib/govdelivery_bulletins/src/Service/AddBulletinToQueue.php on line 200 
    
  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    Woah... never seen that one before. I'll dig into it this evening.

  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    Ok this should do it.
    One more time please.

  • πŸ‡ΊπŸ‡ΈUnited States RichardDavies Portland, Oregon

    It worked from my VBO action, but I got this error during normal execution:

    Warning: Undefined variable $queue in /app/web/modules/contrib/govdelivery_bulletins/src/Service/AddBulletinToQueue.php on line 407 The website encountered an unexpected error. Try again later.
    
    Error: Call to a member function deleteItem() on null in Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->dedupeQueue() (line 407 of modules/contrib/govdelivery_bulletins/src/Service/AddBulletinToQueue.php).
    
    Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->buildXml() (Line: 334)
    Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->buildBulletinData() (Line: 285)
    Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->addToQueue(NULL) (Line: 301)
    Drupal\govdelivery_bulletins\Service\AddBulletinToQueue->addToQueueAndReset() (Line: 417)
    

    It looks like you forgot to replace $queue with $this->queue on lines 407 and 427.

  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    Sorry. I missed the two instances of the $queue.
    I just pushed a fix.

  • πŸ‡ΊπŸ‡ΈUnited States RichardDavies Portland, Oregon

    Ok, we've got a winner. It now works without error when executed normally and when executed in my VBO action. Looks good!

  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida

    Awesome. Thank you for playing "programming blind" with me. :)
    I 'll get this merged and cut a release in a few minutes.

  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida
  • πŸ‡ΊπŸ‡ΈUnited States swirt Florida
  • πŸ‡ΊπŸ‡ΈUnited States RichardDavies Portland, Oregon

    Perfect, thank you so much for the quick response and fix!

Production build 0.71.5 2024