Skip to content

Commit

Permalink
[FEATURE] Multiple items in mainEntity of a WebPage
Browse files Browse the repository at this point in the history
Resolves: #25
  • Loading branch information
brotkrueml committed Nov 22, 2019
1 parent be1a8ef commit 4d948bf
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- Multiple items in mainEntity of a WebPage (#25)

## [1.3.1] - 2019-11-04

### Changed
Expand Down
19 changes: 9 additions & 10 deletions Classes/Manager/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ final class SchemaManager implements SingletonInterface
/** @var BreadcrumbList[] */
private $breadcrumbList = [];

/** @var AbstractType|null */
private $mainEntityOfWebPage;
/** @var AbstractType[] */
private $mainEntityOfWebPage = [];

/**
* Add a type
Expand Down Expand Up @@ -121,11 +121,7 @@ public function hasWebPage(): bool
*/
public function setMainEntityOfWebPage(AbstractType $mainEntity): self
{
if ($this->mainEntityOfWebPage) {
$this->addType($this->mainEntityOfWebPage);
}

$this->mainEntityOfWebPage = $mainEntity;
$this->mainEntityOfWebPage[] = $mainEntity;

return $this;
}
Expand All @@ -147,8 +143,8 @@ public function renderJsonLd(): string
$result[] = $breadcrumb->toArray();
}

if ($this->mainEntityOfWebPage) {
$result[] = $this->mainEntityOfWebPage->toArray();
foreach ($this->mainEntityOfWebPage as $mainEntity) {
$result[] = $mainEntity->toArray();
}
}

Expand Down Expand Up @@ -184,7 +180,10 @@ private function preparePropertiesForWebPage(): void
}
}

if ($this->mainEntityOfWebPage) {
$numberOfMainEntities = \count($this->mainEntityOfWebPage);
if ($numberOfMainEntities === 1) {
$this->webPage->addProperty(static::WEBPAGE_PROPERTY_MAIN_ENTITY, $this->mainEntityOfWebPage[0]);
} elseif ($numberOfMainEntities > 1) {
$this->webPage->addProperty(static::WEBPAGE_PROPERTY_MAIN_ENTITY, $this->mainEntityOfWebPage);
}
}
Expand Down
17 changes: 9 additions & 8 deletions Documentation/Developer/MainEntity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ sense (e.g. a breadcrumb cannot be the primary content).

.. NOTE::

There can be only one main entity at a time. If more than one main entity is
set, the last one added has priority. The ones set before are rendered as
root types.
Technically, there can be more than one main entity at a time. For example,
if you have a ``FAQPage`` you will usually assign
`more than one question <https://developers.google.com/search/docs/data-types/faqpage>`__
as ``mainEntity``.


Using the API
-------------

The main entity of a web page can be defined with the API. Let's start with an
example, which sets a product as the primary content:
example that specifies a product as the primary content:

.. code-block:: php
Expand Down Expand Up @@ -77,10 +78,10 @@ helper.
.. NOTE::

If the ``WebPage`` type is not defined, because in the extension
configuration the
:ref:`according setting <configuration-automaticWebPageSchemaGeneration>` is
disabled, then the main entity is rendered as a root type.
If the ``WebPage`` type is not defined because the
:ref:`appropriate setting<configuration-automaticWebPageSchemaGeneration>`
is disabled in the extension configuration, the main entity is rendered as
a root type.


Using the view helpers
Expand Down
6 changes: 3 additions & 3 deletions Documentation/Developer/WebPage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ expiration date set, the according property ``expires`` will be set in the
markup.

But you have various options to set the web page type on your own. This can be
the case, if you want to define the ``mainEntity`` property for a blog article
or a product.
the case, if you want to define the :ref:`mainEntity <main-entity-of-web-page>`
property for a blog article or a product.

But now let's get our hands on the code.
But now let's get ahold of the code.


.. index:: WebPage via API
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[general]
project = schema
release = 1.3.1
release = 1.4.0-dev
copyright = 2019 by Chris Müller

[html_theme_options]
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Manager/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function setMainEntityOfWebPageTwiceWithWebPageAvailable(): void

$actual = $this->subject->renderJsonLd();

self::assertSame('<script type="application/ld+json">{"@context":"http://schema.org","@graph":[{"@type":"WebPage","mainEntity":{"@type":"Thing","name":"A thing, set as main entity #2"}},{"@type":"Thing","name":"A thing, set as main entity #1"}]}</script>', $actual);
self::assertSame('<script type="application/ld+json">{"@context":"http://schema.org","@type":"WebPage","mainEntity":[{"@type":"Thing","name":"A thing, set as main entity #1"},{"@type":"Thing","name":"A thing, set as main entity #2"}]}</script>', $actual);
}

/**
Expand All @@ -296,6 +296,6 @@ public function setWebPageAndMainEntityOfWebPageAfterThatPreservesFirstType(): v

$actual = $this->subject->renderJsonLd();

self::assertSame('<script type="application/ld+json">{"@context":"http://schema.org","@graph":[{"@type":"WebPage","mainEntity":{"@type":"Thing","name":"A thing, set as new main entity"}},{"@type":"Thing","name":"A thing, set as main entity directly in WebPage"}]}</script>', $actual);
self::assertSame('<script type="application/ld+json">{"@context":"http://schema.org","@type":"WebPage","mainEntity":[{"@type":"Thing","name":"A thing, set as main entity directly in WebPage"},{"@type":"Thing","name":"A thing, set as new main entity"}]}</script>', $actual);
}
}
57 changes: 36 additions & 21 deletions Tests/Unit/ViewHelpers/Type/ThingViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,50 @@ public function fluidTemplatesProvider(): iterable

yield 'Type with -isMainEntityOfWebPage set to true without a WebPage' => [
'<schema:type.thing
-id="parentThing"
-isMainEntityOfWebPage="1"
name="parent name"
url="http://example.org/">
<schema:type.person
-as="subjectOf"
-id="childThing"
name="child name"
url="https://example.org/child"
/>
-id="parentThing"
-isMainEntityOfWebPage="1"
name="parent name"
url="http://example.org/">
<schema:type.person
-as="subjectOf"
-id="childThing"
name="child name"
url="https://example.org/child"
/>
</schema:type.thing>',
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"Thing","@id":"parentThing","name":"parent name","subjectOf":{"@type":"Person","@id":"childThing","name":"child name","url":"https://example.org/child"},"url":"http://example.org/"}</script>',
];

yield 'Type with -isMainEntityOfWebPage set to true with a WebPage' => [
'<schema:type.webPage/>
<schema:type.thing
-id="parentThing"
-isMainEntityOfWebPage="1"
name="parent name"
url="http://example.org/">
<schema:type.person
-as="subjectOf"
-id="childThing"
name="child name"
url="https://example.org/child"
/>
</schema:type.thing>',
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"WebPage","mainEntity":{"@type":"Thing","@id":"parentThing","name":"parent name","subjectOf":{"@type":"Person","@id":"childThing","name":"child name","url":"https://example.org/child"},"url":"http://example.org/"}}</script>',
];

yield 'More than on type with -isMainEntityOfWebPage set to true with a WebPage' => [
'<schema:type.webPage/>
<schema:type.thing
-id="parentThing"
-id="parentThing#1"
-isMainEntityOfWebPage="1"
name="parent name"
url="http://example.org/">
<schema:type.person
-as="subjectOf"
-id="childThing"
name="child name"
url="https://example.org/child"
/>
</schema:type.thing>',
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"WebPage","mainEntity":{"@type":"Thing","@id":"parentThing","name":"parent name","subjectOf":{"@type":"Person","@id":"childThing","name":"child name","url":"https://example.org/child"},"url":"http://example.org/"}}</script>',
name="parent name #1"
/>
<schema:type.thing
-id="parentThing#2"
-isMainEntityOfWebPage="1"
name="parent name #2"
/>',
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"WebPage","mainEntity":[{"@type":"Thing","@id":"parentThing#1","name":"parent name #1"},{"@type":"Thing","@id":"parentThing#2","name":"parent name #2"}]}</script>',
];

yield 'Property value of 0.00 is rendered' => [
Expand Down
7 changes: 0 additions & 7 deletions Tests/UnitTestsBootstrap.php

This file was deleted.

1 change: 0 additions & 1 deletion Tests/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
bootstrap="UnitTestsBootstrap.php"
>
<testsuites>
<testsuite name="Unit tests">
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.3.x-dev"
"dev-master": "1.4.x-dev"
},
"typo3/cms": {
"app-dir": ".Build",
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'clearCacheOnLoad' => true,
'author' => 'Chris Müller',
'author_email' => 'typo3@krue.ml',
'version' => '1.3.1',
'version' => '1.4.0-dev',
'constraints' => [
'depends' => [
'typo3' => '9.5.0-10.4.99',
Expand Down

0 comments on commit 4d948bf

Please sign in to comment.