Discovered as part of the ads feature pull request: https://github.com/BurdaMagazinOrg/module-fb_instant_articles/pull/36#is..., if you are set either a ads embed code or a analytics embed code and are using entitycache module, you will receive errors like the following on a primed cache:

Warning: DOMDocument::importNode(): Couldn't fetch DOMDocumentFragment in Facebook\InstantArticles\Elements\Element->dangerouslyAppendUnescapedHTML() (line 47 of /var/www/docroot/sites/all/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Elements/Element.php).
Recoverable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /var/www/docroot/sites/all/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Elements/Ad.php on line 215 and defined in Facebook\InstantArticles\Elements\Element->dangerouslyAppendUnescapedHTML() (line 48 of /var/www/docroot/sites/all/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Elements/Element.php).

DOMDocumentFragment and DOMNode classes don't support being serialized, so when entitycache is installed, what happens is, the $node get's loaded up, the ads/analytics from the settings get added to the display object including the reference to a DOMDocumentFragment for when you have a ad/analytics using embed code. entitycache module serializes and caches the loaded $node and when it fetches from cache on the subsequent request, the DOMDocumentFragment is empty, and it throws errors. Again this is b/c DOMDocumentFragment and DOMNode classes don't support being serialized.

Admittedly an edge case, b/c not everyone uses entitycache and further, you could avoid using the settings form and instead use the alter hook, hook_fb_instant_articles_display_instant_article_alter, to add in an ad you many need. Still would be nice to fix.

Comments

m4olivei created an issue. See original summary.

sadiefp’s picture

@m4olivei, were you able to add Google Analytics with hook_fb_instant_articles_display_instant_article_alter or hook_fb_instant_articles_article_alter?

I don't think I understand the issue. I tried to add Google Analytics in hook_fb_instant_articles_article_alter like this:

    $instant_article
      ->addChild(
        Analytics::create()
          ->withHTML("
          <script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
            ga('create', 'UA-XXXXXXXX-1', 'auto');
            ga('send', 'pageview');
          </script>
        ")
      );

But I still get the same error:

Error
Warning: DOMDocument::importNode(): Couldn't fetch DOMCdataSection in Facebook\InstantArticles\Elements\ElementWithHTML->dangerouslyAppendUnescapedHTML() (line 62 of /sites/all/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Elements/ElementWithHTML.php).
Recoverable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /sites/all/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Elements/Analytics.php on line 103 and defined in Facebook\InstantArticles\Elements\ElementWithHTML->dangerouslyAppendUnescapedHTML() (line 63 of /sites/all/vendor/facebook/facebook-instant-articles-sdk-php/src/Facebook/InstantArticles/Elements/ElementWithHTML.php).
The website encountered an unexpected error. Please try again later.
markdorison’s picture

Issue summary: View changes
brocakun’s picture

@sadie @m4olivei I'm getting the same error using the hook_fb_instant_articles_article_alter while trying to add a Google Analitycs code, have you guys found any workaroud?

sadiefp’s picture

@brocakun, I haven't found a code work around. Right now we have folks manually adding the google analytics code in the facebook instant articles admin on business.facebook.com.

brocakun’s picture

@sadiefp It seems that I found a workaround, we use the hook_node_inset and hook_node_update to publish the Instant Article via API, the only way I make them work with Embed Codes and with the Google Analitycs code, was clearing the entity cache of the node before using the api, I know the performance impact and that this will not work with the RSS submodule, but seems to work with our custom code.

/**
 * Implements hook_node_inset().
 */
function module_node_insert($node) {
  module_fb_instant_articles_publish_article($node);
}

/**
 * Implements hook_node_update().
 */
function module_node_update($node) {
  module_fb_instant_articles_publish_article($node);
}
/**
 * Implements custom function to publish a Facebook Instant Article via API.
 */
function module_fb_instant_articles_publish_article($node) {
  if ($node->type == "article") {
    // workaround for html codes with entitycache.
    if(module_exists('entitycache')) {
      entity_get_controller('node')->resetCache(array($node->nid));
    }
    $node = node_load($node->nid, NULL, TRUE);
    node_build_content($node, $view_mode = 'fb_instant_article');
    $client = \Drupal\fb_instant_articles\DrupalClient::get();
    $client->importArticle($node->fb_instant_article, TRUE);
  }
}
pribeh’s picture

I'm getting this as well.