As the title says: I've got a custom input filter in Drupal 7 which replaces a token with a custom drupal form. Without showing the complete code, which might just confuse things, The main essence is the following:

function _my_filter($text, $filter = NULL, $format = NULL, $langcode = NULL, $cache = NULL, $cache_id = NULL) {
  $rendered_text = $text;
  preg_match_all(MY_CUSTOM_REGEX, $text, $matches);
  if (!empty($matches[0])) {
    foreach ($matches[0] as $match) {
      $custom_form = drupal_get_form('my_custom_form'); 
      $replacement = drupal_render($custom_form);
      $rendered_text = str_replace($match, $replacement, $rendered_text);        
  return $rendered_text;

For some reason when the node is being saved this times out. I've narrowed it down to being something in my drupal_get_form (but this form renders fine without any problems outside of an input filter).

Can I use drupal_get_form and drupal_render within an input filter like this?

1 Answer 1


I believe you can do this, but not by default. You'll need to disable caching. This is because the filtered data is cached, so your form will only work the first time. To disable caching, you need to set cache to FALSE in your hook_filter_info() definition.

If it's timing out, then it may be a problem in your form definition itself - does the for work in non-filter context (ie embedded directly into a page)?

  • I kept thinking I had this solved...but I still don't. It absolutely appears cache related. Putting cache => false into my _info definition didn't fix it, but that may be due to multiple layers of caching happening on the server. You seem to be right that it shows the form the first time...then after that, every time, it times out. So, while your fix didn't 'solve' this, I think it's the correct answer and the key to the puzzle here.
    – Bobby
    Jan 5, 2017 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.