From 651786ba101fbd2eeb6109697d3b2f396a686584 Mon Sep 17 00:00:00 2001 From: Anita Date: Mon, 9 Jan 2023 13:22:31 +0530 Subject: [PATCH 1/6] Add improvements in json ld definition for webpage --- .../_schemas/definitions/creativeWork.twig | 50 +++++++++++++++++++ templates/_schemas/definitions/webpage.twig | 20 +++----- 2 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 templates/_schemas/definitions/creativeWork.twig diff --git a/templates/_schemas/definitions/creativeWork.twig b/templates/_schemas/definitions/creativeWork.twig new file mode 100644 index 0000000..0abe55f --- /dev/null +++ b/templates/_schemas/definitions/creativeWork.twig @@ -0,0 +1,50 @@ +{% macro creativeWork(props, schema = null) %} + {% from '_schemas/definitions/thing' import thing %} + {% from '_schemas/definitions/organization' import organization %} + + {% set authors = [] %} + + {% for author in (props.authors ?? []) %} + {% if author.type is defined and author.type == 'person' %} + {% set authors = authors|merge([craft.schema.Person + .name(author.name) + .url(author.url) + ]) %} + {% 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([craft.schema.Person.name(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 %} diff --git a/templates/_schemas/definitions/webpage.twig b/templates/_schemas/definitions/webpage.twig index f401df2..386bc99 100644 --- a/templates/_schemas/definitions/webpage.twig +++ b/templates/_schemas/definitions/webpage.twig @@ -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(siteUrl|default(null)) + .mainContentOfPage(props.mainContentOfPage|default('main')) + .primaryImageOfPage(props.image ?? false ? imageObject(props.image) : null) %} {% return schema %} From 731df04ebaae72799b6d368320a3bc6191ee243b Mon Sep 17 00:00:00 2001 From: Anita Date: Wed, 11 Jan 2023 18:35:18 +0530 Subject: [PATCH 2/6] Add macro for person --- templates/_schemas/definitions/person.twig | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 templates/_schemas/definitions/person.twig diff --git a/templates/_schemas/definitions/person.twig b/templates/_schemas/definitions/person.twig new file mode 100644 index 0000000..3182022 --- /dev/null +++ b/templates/_schemas/definitions/person.twig @@ -0,0 +1,10 @@ +{% macro person(props, schema = null) %} + + {% set schema = schema ?? craft.schema.Person %} + {% set schema = schema + .name(props.name) + .url(props.url) + %} + + {% return schema %} +{% endmacro %} \ No newline at end of file From 76aa5fca4b2b2b602b1bba8a6c67fadbd94af82a Mon Sep 17 00:00:00 2001 From: Anita Date: Wed, 11 Jan 2023 18:36:16 +0530 Subject: [PATCH 3/6] Use person macro for authors and publishers --- templates/_schemas/definitions/creativeWork.twig | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/templates/_schemas/definitions/creativeWork.twig b/templates/_schemas/definitions/creativeWork.twig index 0abe55f..af6c949 100644 --- a/templates/_schemas/definitions/creativeWork.twig +++ b/templates/_schemas/definitions/creativeWork.twig @@ -1,15 +1,13 @@ {% 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([craft.schema.Person - .name(author.name) - .url(author.url) - ]) %} + {% set authors = authors|merge([person(author)]) %} {% else %} {% set authorSchema = { title: author.title|default(siteName), @@ -24,7 +22,7 @@ {% for publisher in (props.publishers ?? []) %} {% if publisher.type is defined and publisher.type == 'person' %} - {% set publishers = publishers|merge([craft.schema.Person.name(publisher)]) %} + {% set publishers = publishers|merge([person(publisher)]) %} {% else %} {% set publisherSchema = { title: publisher.title|default(null), From 03fc4b47884f7683663261853095d4e010619cdb Mon Sep 17 00:00:00 2001 From: Anita Date: Wed, 11 Jan 2023 18:36:37 +0530 Subject: [PATCH 4/6] Update site id property --- templates/_schemas/definitions/webpage.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/_schemas/definitions/webpage.twig b/templates/_schemas/definitions/webpage.twig index 386bc99..d9edbd8 100644 --- a/templates/_schemas/definitions/webpage.twig +++ b/templates/_schemas/definitions/webpage.twig @@ -5,7 +5,7 @@ {% set schema = schema ?? craft.schema.WebPage %} {% set schema = creativeWork(props, schema) %} {% set schema = schema - .id(siteUrl|default(null)) + .id(props.url ~ '#webpage'|default(null)) .mainContentOfPage(props.mainContentOfPage|default('main')) .primaryImageOfPage(props.image ?? false ? imageObject(props.image) : null) %} From 8e6ab1e511f23e2b742de05c0567c7f8e2ab4737 Mon Sep 17 00:00:00 2001 From: Anita Date: Thu, 12 Jan 2023 18:56:48 +0530 Subject: [PATCH 5/6] Inherit from thing definition --- templates/_schemas/definitions/person.twig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/_schemas/definitions/person.twig b/templates/_schemas/definitions/person.twig index 3182022..5599538 100644 --- a/templates/_schemas/definitions/person.twig +++ b/templates/_schemas/definitions/person.twig @@ -1,9 +1,10 @@ {% 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) - .url(props.url) %} {% return schema %} From 8d072632427a5f844b99befb4df9685fd3850bbb Mon Sep 17 00:00:00 2001 From: Anita Date: Fri, 13 Jan 2023 16:23:34 +0530 Subject: [PATCH 6/6] Remove unused property --- templates/_schemas/definitions/person.twig | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/templates/_schemas/definitions/person.twig b/templates/_schemas/definitions/person.twig index 5599538..cfa2d6d 100644 --- a/templates/_schemas/definitions/person.twig +++ b/templates/_schemas/definitions/person.twig @@ -3,9 +3,6 @@ {% set schema = schema ?? craft.schema.Person %} {% set schema = thing(props, schema) %} - {% set schema = schema - .name(props.name) - %} {% return schema %} -{% endmacro %} \ No newline at end of file +{% endmacro %}