5.7.5.6 - Custom package on_start method called by conversation block
Permalink
I'm building a custom package to easely add rich snippet and structured markup data to a Concrete5 website.
In the package controller I add this code (for example):
Everything works except when I add the Conversation block to the page.
Inside the Conversation block view.php my on_start method is called again and put on the website.
This means the json-ld snippet is added to the page twice.
This is the html output from the conversation block:
I've solved this by adding an if-condition to my on_start method, however this might not be the best solution...
I've also tried with addHeaderItem instead of the addFooterItem, and both gave the same result.
Anyone else experienced the conversation block adding data through the on_start of a custom package controller ? (my package handle is abc_snippet in case someone wants to try and reproduce)
In the package controller I add this code (for example):
$jsonLDSite = '<script type="application/ld+json">{'; $jsonLDSite .= '"@context" : "http://schema.org",'; $jsonLDSite .= '"@type" : "WebSite",'; $jsonLDSite .= '"name" : "'.$name.'",'; if(!empty($alternateName)){ $jsonLDSite .= '"alternateName" : "'.$alternateName.'",'; } $jsonLDSite .= '"url" : "'.$url.'"'; $jsonLDSite .= '}</script>'; $view->addFooterItem($jsonLDSite);
Everything works except when I add the Conversation block to the page.
Inside the Conversation block view.php my on_start method is called again and put on the website.
This means the json-ld snippet is added to the page twice.
This is the html output from the conversation block:
<div class="ccm-conversation-wrapper" data-conversation-id="5"> <script type="application/ld+json"> {"@context" : "http://schema.org","@type" : "WebSite","name" : Mywebsite Name","url" : "http://www.mywebsite.com"} </script> <h4>Add Message</h4> // code of the conversation block continues
I've solved this by adding an if-condition to my on_start method, however this might not be the best solution...
// Making sure this is only called once at on_start in the index.php. if($_SERVER['CONTEXT_PREFIX'] == DIR_REL.'/index.php'){ //here the code to add the json-ld to the footer }
I've also tried with addHeaderItem instead of the addFooterItem, and both gave the same result.
Anyone else experienced the conversation block adding data through the on_start of a custom package controller ? (my package handle is abc_snippet in case someone wants to try and reproduce)