Drupal PHP Fatal error Call to a member function getElementsByTagName()

I suddenly started getting the following error:

PHP Fatal error:  Call to a member function getElementsByTagName() on a non-object in public_html/drupal/modules/filter/filter.module on line 1122

I checked what there is on line 1122. I could clearly see that the error needs to happen because $body_node was null. For a quick fix I just implemented a null check as follows:

function filter_dom_serialize($dom_document) {
  ...

if ($body_node != null) {
  foreach ($body_node->getElementsByTagName('script') as $node) {
    filter_dom_serialize_escape_cdata_element($dom_document, $node);
  }

  foreach ($body_node->getElementsByTagName('style') as $node) {
    filter_dom_serialize_escape_cdata_element($dom_document, $node, '/*', '*/');
  }

  foreach ($body_node->childNodes as $child_node) {
    $body_content .= $dom_document->saveXML($child_node);
  }
}

  ...
}

This seems to resolve the problem for now.

Leave a comment

Your email address will not be published. Required fields are marked *