diff --git a/templates/_schemas/definitions/creativeWork.twig b/templates/_schemas/definitions/creativeWork.twig new file mode 100644 index 0000000..af6c949 --- /dev/null +++ b/templates/_schemas/definitions/creativeWork.twig @@ -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 %} diff --git a/templates/_schemas/definitions/person.twig b/templates/_schemas/definitions/person.twig new file mode 100644 index 0000000..cfa2d6d --- /dev/null +++ b/templates/_schemas/definitions/person.twig @@ -0,0 +1,8 @@ +{% macro person(props, schema = null) %} + {% from '_schemas/definitions/thing' import thing %} + + {% set schema = schema ?? craft.schema.Person %} + {% set schema = thing(props, schema) %} + + {% return schema %} +{% endmacro %} diff --git a/templates/_schemas/definitions/webpage.twig b/templates/_schemas/definitions/webpage.twig index f401df2..d9edbd8 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(props.url ~ '#webpage'|default(null)) + .mainContentOfPage(props.mainContentOfPage|default('main')) + .primaryImageOfPage(props.image ?? false ? imageObject(props.image) : null) %} {% return schema %}