J3 Issue ?
avatar hans2103
hans2103
29 Aug 2018

Is your feature request related to a problem? Please describe.

Issue with adding structured data as json format to Joomla using PHP.
When I add a script declaration of type = json it will be combined in the header.

    $jsonscript                 = array();
    $jsonscript['website']      = '
{
    "@context": "http://schema.org/",
    "@type": "WebSite",
    "name": "ECMA"
    "url": "https://example.com/"
}';
    $jsonscript['organization'] = '
{
    "@context": "http://schema.org",
    "@type": "Organization",
    "url": "https://example.com/",
    "logo": "https://example.com/images/logo-example.png",
    "contactPoint": [
    {
        "@type": "ContactPoint",
        "telephone": "+31-6-11",
        "contactType": "customer service",
        "availableLanguage": ["Dutch", "English"]
    }
    ],
    "name": "ECMA"
}
';

    foreach ($jsonscript as $content) :
        $document->addScriptDeclaration($content, $type = 'application/ld+json');
    endforeach;

This will be rendered:

    <script type="application/ld+json">
{
    "@context": "http://schema.org/",
    "@type": "WebSite",
    "name": "ECMA",
    "url": "https://www.example.com/"
}
{
    "@context": "http://schema.org",
    "@type": "Organization",
    "url": "https://www.example.com",
    "logo": "https://example.com/images/logo-example.png",
    "contactPoint": [
    {
        "@type": "ContactPoint",
        "telephone": "+31-6-11",
        "contactType": "customer service",
        "availableLanguage": ["Dutch", "English"]
    }
    ],
    "name": "ECMA"
}
    </script>

Th rich snippets testing tool does not recognize the second json part. It will only recognize type = website.

Describe the solution you'd like

It would be great if some kind of parameter can be added which will prevent merging the tags.

The output I would like to see is:

    <script type="application/ld+json">
{
    "@context": "http://schema.org/",
    "@type": "WebSite",
    "name": "ECMA",
    "url": "https://www.example.com/"
}
    </script>
    <script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "Organization",
    "url": "https://www.example.com",
    "logo": "https://example.com/images/logo-example.png",
    "contactPoint": [
    {
        "@type": "ContactPoint",
        "telephone": "+31-6-11",
        "contactType": "customer service",
        "availableLanguage": ["Dutch", "English"]
    }
    ],
    "name": "ECMA"
}
    </script>

Comma separated entries seems not to work for the Rich Snippets Testing Tool

Additional context

When this feature request is granted you are able to add json to the head of your website using the function addScriptDeclaration.

avatar hans2103 hans2103 - open - 29 Aug 2018
avatar joomla-cms-bot joomla-cms-bot - change - 29 Aug 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 29 Aug 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 29 Aug 2018
Category JavaScript
avatar ReLater
ReLater - comment - 29 Aug 2018

Hm, I think you're doing something wrong by building your JSON strings manually instead of using json_encode($some_data_collection).

Why don't you collect your rich snippets datas in a (maybe multidimensional) $array first and in the end run a single json_encode($array); to get the correct JSON string.

$document->addScriptDeclaration(json_encode($array), $type = 'application/ld+json');

avatar ReLater
ReLater - comment - 29 Aug 2018

Ah, sorry, now I understand your feature request.

I'm using ->addCustomTag(...) in foreach instead of ->addScriptDeclaration(...)

foreach (severalSnippetsCollections as $collected_data_array)
{
$output = array('<script type="application/ld+json">');
$output[] = json_encode($collected_data_array);
$output[] = '</script>';
JFactory::getDocument()->addCustomTag($output);
}
avatar franz-wohlkoenig franz-wohlkoenig - change - 30 Aug 2018
Status New Discussion
avatar hans2103
hans2103 - comment - 30 Aug 2018

@ReLater using the function addCustomTag can be used too, but the function addScriptDeclaration seems to be made for the job.

/**
* Adds a script to the page
*
* @param string $content Script
* @param string $type Scripting mime (defaults to 'text/javascript')
*
* @return Document instance of $this to allow chaining
*
* @since 11.1
*/
public function addScriptDeclaration($content, $type = 'text/javascript')
{
if (!isset($this->_script[strtolower($type)]))
{
$this->_script[strtolower($type)] = $content;
}
else
{
$this->_script[strtolower($type)] .= chr(13) . $content;
}
return $this;
}

I'm trying to add a script with $type="application/ld+json" to the head of my Joomla site.
The only missing part of this function is the ability to add the script separated from other scripts with the same type.

avatar brianteeman brianteeman - change - 30 Oct 2018
Labels Added: J3 Issue
avatar brianteeman brianteeman - labeled - 30 Oct 2018
avatar SharkyKZ
SharkyKZ - comment - 5 Aug 2019

This is fixed in 4.0 with #25357. As the fix contains a B/C break, it won't make it to 3.x.

avatar franz-wohlkoenig franz-wohlkoenig - change - 5 Aug 2019
Status Discussion Closed
Closed_Date 0000-00-00 00:00:00 2019-08-05 08:40:56
Closed_By franz-wohlkoenig
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 5 Aug 2019

closed as stated above.

avatar franz-wohlkoenig franz-wohlkoenig - close - 5 Aug 2019

Add a Comment

Login with GitHub to post a comment