Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add improvements in json ld definition for webpage #56

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions templates/_schemas/definitions/creativeWork.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% macro creativeWork(props, schema = null) %}
{% from '_schemas/definitions/thing' import thing %}
{% from '_schemas/definitions/organization' import organization %}
{% from '_schemas/definitions/person' import person %}

{% set authors = [] %}

{% for author in (props.authors ?? []) %}
{% if author.type is defined and author.type == 'person' %}
{% set authors = authors|merge([person(author)]) %}
{% else %}
{% set authorSchema = {
title: author.title|default(siteName),
url: author.url|default(siteUrl),
mainEntityOfPage: false
} %}
{% set authors = authors|merge([organization(authorSchema)]) %}
{% endif %}
{% endfor %}

{% set publishers = [] %}

{% for publisher in (props.publishers ?? []) %}
{% if publisher.type is defined and publisher.type == 'person' %}
{% set publishers = publishers|merge([person(publisher)]) %}
{% else %}
{% set publisherSchema = {
title: publisher.title|default(null),
url: publisher.url|default(null),
mainEntityOfPage: false,
offices: publisher.offices|default(null),
} %}
{% set publishers = publishers|merge([organization(publisherSchema)]) %}
{% endif %}
{% endfor %}

{% set schema = schema ?? craft.schema.CreativeWork %}
{% set schema = thing(props, schema) %}
{% set schema = schema
.author(authors|default(null))
.publisher(publishers|default(null))
.dateCreated(props.dateCreated|default(null))
.datePublished(props.datePublished|default(null))
.dateModified(props.dateUpdated|default(null))
%}

{% return schema %}
{% endmacro %}
11 changes: 11 additions & 0 deletions templates/_schemas/definitions/person.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% macro person(props, schema = null) %}
{% from '_schemas/definitions/thing' import thing %}

{% set schema = schema ?? craft.schema.Person %}
{% set schema = thing(props, schema) %}
{% set schema = schema
.name(props.name)
%}

{% return schema %}
{% endmacro %}
20 changes: 6 additions & 14 deletions templates/_schemas/definitions/webpage.twig
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
{% macro webpage(props, schema = null) %}
{% from '_schemas/definitions/thing' import thing %}
{% from '_schemas/definitions/creativeWork' import creativeWork %}
{% from '_schemas/definitions/imageObject' import imageObject %}
{% from '_schemas/definitions/organization' import organization %}

{% set schema = schema ?? craft.schema.Webpage %}
{% set schema = thing(props, schema) %}
{% set schema = schema ?? craft.schema.WebPage %}
{% set schema = creativeWork(props, schema) %}
{% set schema = schema
.id(props.url ~ '#webpage')
.dateCreated(props.dateCreated)
.datePublished(props.dateCreated)
.dateModified(props.dateUpdated)
.headline(create('craft\\helpers\\StringHelper').safeTruncate(props.title, 110))
.primaryImageOfPage((props.images ?? []) is not empty ? imageObject(props.images|first) : null)
.author((props.authors.organizations ?? [])|map(org => organization(org))|default(null))
.creator((props.creators ?? [])|map(org => organization(org))|default(null))
.copyrightHolder((props.authors.organizations ?? [])|map(org => organization(org))|default(null))
.copyrightYear(props.dateCreated|date('Y'))
.id(props.url ~ '#webpage'|default(null))
.mainContentOfPage(props.mainContentOfPage|default('main'))
.primaryImageOfPage(props.image ?? false ? imageObject(props.image) : null)
%}

{% return schema %}
Expand Down