inforca / ibexa-json-ld-bundle
Bundle pour intégration de jsonLD
Requires
- php: >=8.1
- ext-curl: *
- ext-dom: *
- ext-pdo: *
- ext-soap: *
- ibexa/core: ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.3
- phpstan/phpstan-doctrine: ^0.12.4
- phpstan/phpstan-symfony: ^0.12.1
This package is not auto-updated.
Last update: 2025-06-04 13:15:55 UTC
README
Description:
A bundle for Ibexa that lets you add a JSONLD schema in content full views for a dedicated content type (By configuring the schema)
Requirements:
- Ibexa v4.x
- PHP 8.x
Install:
Add this line to the composer.json dependencies:
"inforca/ibexa-json-ld-bundle": "dev-main",
And into "repositories" p of composer.json:
"repositories": {
...,
"inforca-ibexa-json-ld-bundle": {
"type": "vcs",
"url": "https://git.inforca.mc/webforca/bundles/ibexa-json-ld-bundle"
}
}
.. then run
composer update inforca/ibexa-json-ld-bundle
Look after changes:
Check your file config/bundles.php
Add the next line, if it is not already present, into your array of charged bundles
Inforca\IbexaJsonLdBundle\InforcaJsonLdBundle::class => ['all' => true],
Use
Create schema.json file
In public folder, create "jsonLd" folder, and inside create a file called "schema.json"
Specifics schemas (optionnal)
In order to be able to customise its schemas for different siteaccess, you can configure and create your own files, wherever you want, as long as the file is in ‘json’ format and has the file extension ‘.json’.
In addition, you can configure a schema for a type of content (‘article’ for example), in your file dedicated to a siteaccess and also configure it in the default ‘schema.json’. The purpose of this is to have a fallback configuration in the event that you are no longer on this siteaccess and other sites need an ‘article’ configuration for another use.
To do this, you need to add a configuration under the ‘parameters’ configuration key in your configuration file, such as:
### Find your corresponding schema
Go to https://schema.org/
Find the corresponding schema to the content type you want a schema implemented and loaded in <head> section of your website
Example for an **article** or **newspost** (https://schema.org/NewsArticle):
```json
{
"@context": "https://schema.org",
"@type": "NewsArticle",
"url": "http://www.bbc.com/news/world-us-canada-39324587",
"publisher":{
"@type":"Organization",
"name":"BBC News",
"logo":"http://www.bbc.co.uk/news/special/2015/newsspec_10857/bbc_news_logo.png?cb=1"
},
"headline": "Trump Russia claims: FBI's Comey confirms investigation of election 'interference'",
"mainEntityOfPage": "http://www.bbc.com/news/world-us-canada-39324587",
"articleBody": "Director Comey says the probe into last year's US election would assess if crimes were committed.",
"image":[
"http://ichef-1.bbci.co.uk/news/560/media/images/75306000/jpg/_75306515_line976.jpg",
],
"datePublished":"2017-03-20T20:30:54+00:00"
}
```
Add the schema to the **schema.json** file created juste before by adding a new empty json table.
Create an other json table inside the empty one, with a table key which is the **content type identifier** that you configured
in back office, for the schema corresponding.
For example, with a content type called "article" in back office, the schema.json file looks like:
```json
{
"article": {
"@context": "https://schema.org",
"@type": "NewsArticle",
"url": "http://www.bbc.com/news/world-us-canada-39324587",
"publisher": {
"@type": "Organization",
"name": "BBC News",
"logo": "http://www.bbc.co.uk/news/special/2015/newsspec_10857/bbc_news_logo.png?cb=1"
},
"headline": "Trump Russia claims: FBI's Comey confirms investigation of election 'interference'",
"mainEntityOfPage": "http://www.bbc.com/news/world-us-canada-39324587",
"articleBody": "Director Comey says the probe into last year's US election would assess if crimes were committed.",
"image": [
"http://ichef-1.bbci.co.uk/news/560/media/images/75306000/jpg/_75306515_line976.jpg"
],
"datePublished": "2017-03-20T20:30:54+00:00"
},
}
```
If you want to add an other schema for an other content type, add it below the previous one like:
```json
{
"article": {
...
},
"event": {
"@context": "https://schema.org",
"@type": "MusicGroup",
"event": [
{
"@type": "Event",
"location": "Memphis, TN, US",
"offers": "ticketmaster.com/foofighters/may20-2011",
"startDate": "2011-05-20",
"url": "foo-fighters-may20-fedexforum"
},
{
"@type": "Event",
"location": "Council Bluffs, IA, US",
"offers": "ticketmaster.com/foofighters/may23-2011",
"startDate": "2011-05-23",
"url": "foo-fighters-may23-midamericacenter"
}
],
"image": [
"foofighters-1.jpg",
"foofighters-2.jpg",
"foofighters-3.jpg"
],
"name": "Foo Fighters",
},
}
```
### Call the JSONLD controller
In your pagelayout.html.twig, in the **<head>** section, call the controller which will inject the schema corresponding to your content
in your page.
```
{% if content %}
{{ render_esi(controller('Inforca\\IbexaJsonLdBundle\\Controller\\JsonLdController::generateJsonLdSchema', {
contentId: content.id,
})) }}
{% endif %}
```
### Fields mapping instructions
We want now programmatically get informations from your back office (title, description, image, relation field information...),
to schematize your content following the content type schema configured in schema.json file.
We can get informations from a field of the current content, metadescription informations, from a related content field
(related content title for example), a boolean etc.
#### **FIELD VALUE**
We use the **"#"** instruction followed by the **field identifier** you want to get the value.
Example:
- **#title** will return the title content field value.
```json
"article": {
...
"publisher": {
...
"name": "#title",
...
}
},
```
#### **BOOLEAN VALUE**
When you get boolean field value, you will follow the instructions above (to get a field value) for example.
```json
"article": {
...
"publisher": {
...
"isPublished": "#isPublished" // will return true or false,
...
}
},
```
You can change the return value of the boolean, example:
```json
"article": {
...
"publisher": {
...
"isPublished": "#isPublished(yes|no)" // will return yes or no,
...
}
},
```
You can of course combine instructions, for example if your boolean field is in a related content
of your current content:
```json
"article": {
...
"publisher": {
...
"isPublished": "*related_field_identifier#isPublished(of course|not at all)" // will return yes or no,
...
}
},
```
#### **METADATA FIELD VALUE**
We use the **"@"** instruction followed by the **metadata field identifier** you want to get the value.
Example:
- **@creationDate** will return the creation date metadata field value of the current content.
- **@modificationDate** will return the modification date metadata field value of the current content.
- **@url** will return the current content URL.
```json
"article": {
...
"publisher": {
...
"createdAt": "@creationDate",
"modifiedAt": "@modificationDate",
"url": "@url",
...
}
},
```
Some others metadata fields:
- @identifier
- @contentID
- @modificationDate
#### **RELATED CONTENT FIELD VALUE**
We use the **"*"** instruction followed by the identifier of the relation field.
Then, we add the instruction **"#"** seen above to get the field value followed by the field identifier of the related content field you want to get.
Example: I want to get the **name** of the **author** which is in **relation** in our **current content.**
```json
"article": {
...
"author": [{
"@type": "Person",
"name": "*related_author#name"
}]
},
```
#### **SUBFIELD VALUE (of the current content OR related content)**
We use the **">"** instruction followed by the **subfield identifier** you want to get the value.
Example with the Ibexa "address" field which is composed by subfields.
If my "address" field is in the current content type definition, you can get the value like this:
```json
"address": {
"@type": "PostalAddress",
"streetAddress": "#adresse>street",
"addressRegion": "#adresse>region",
"postalCode": "#adresse>postal_code",
"addressLocality": "#adresse>locality"
}
```
You can also combine instructions like if my "address" field is in a content which is related to the current content for example,
you can get the value like this:
```json
"address": {
"@type": "PostalAddress",
"streetAddress": "*related_place#adresse>street",
"addressRegion": "*related_place#adresse>region",
"postalCode": "*related_place#adresse>postal_code",
"addressLocality": "*related_place#adresse>locality"
}
```
### Enable or Disable bundle as you want, per siteaccess (optionnal)
By default, the bundle is activated at configuration level for all your sites.
If you wish to deactivate it for some or activate it only for certain siteaccess, you can add these configurations under the ‘parameters’ key.
Example:
The bundle is enabled by default for all siteaccess, so i override the default configuration to disable for all.
```inforca_jsonld_bundle.default.enable_jsonld_schemas: false```
Then, i enable the bundle only on a particular siteaccess in dedicated config file:
```inforca_jsonld_bundle.<your_siteaccess>.enable_jsonld_schemas: true```