Skip to content
Snippets Groups Projects
Commit 5371c87c authored by Jacob Rockowitz's avatar Jacob Rockowitz
Browse files

Issue #3382329: Add additonalType support to Schema.org Blueprints Subtype module

parent e390b38f
No related branches found
No related tags found
No related merge requests found
......@@ -756,7 +756,6 @@ schema_properties:
- acquireLicensePage
- actionableFeedbackPolicy
- additionalProperty
- additionalType
- answerCount
- assesses
- branchCode
......@@ -867,6 +866,8 @@ schema_properties:
actor:
label: Actors
unlimited: true
additionalType:
type: string
affectedBy:
unlimited: true
affiliation:
......
......@@ -4,6 +4,7 @@ Table of contents
* Introduction
* Features
* Configuration
* References
Introduction
......@@ -25,6 +26,8 @@ Features
and allowed values.
- Adds custom 'subtype' mapping to Schema.org mapping properties.
- Alters the Schema.org mapping list builder and adds a 'Subtype' column.
- Replaces @type in JSON-LD for valid subtypes or uses the subtype value
as the https://schema.org/additionalType.
Configuration
......@@ -37,3 +40,9 @@ Configuration
- Enter default label used for subtype fields.
- Enter the default description used for subtype fields.
- Enter Schema.org types that support subtyping by default.
References
----------
- [How to use additionalType and sameAs to link to Wikipedia](https://support.schemaapp.com/support/solutions/articles/33000277321-how-to-use-additionaltype-and-sameas-to-link-to-wikipedia)
......@@ -114,11 +114,28 @@ function schemadotorg_subtype_schemadotorg_mapping_defaults_alter(array &$defaul
* The entity.
*/
function schemadotorg_subtype_schemadotorg_jsonld_schema_type_entity_alter(array &$data, EntityInterface $entity): void {
// Allow subtype property to override the mapping Schema.org type.
if (!empty($data['subtype'])) {
// Check that the subtype property is set, if not exit.
if (empty($data['subtype'])) {
return;
}
// Use the subtype property to override the mapping Schema.org type or
// set the additionalType property.
/** @var \Drupal\schemadotorg\SchemaDotOrgSchemaTypeManagerInterface $schema_type_manager */
$schema_type_manager = \Drupal::service('schemadotorg.schema_type_manager');
if ($schema_type_manager->isSubTypeOf($data['subtype'], $data['@type'])) {
$data['@type'] = $data['subtype'];
unset($data['subtype']);
}
elseif (isset($data['additionalType'])) {
$data['additionalType'] = array_merge(
(array) $data['additionalType'],
(array) $data['subtype']
);
}
else {
$data['additionalType'] = $data['subtype'];
}
unset($data['subtype']);
}
/* ************************************************************************** */
......@@ -297,7 +314,9 @@ function schemadotorg_subtype_form_schemadotorg_types_settings_form_alter(array
'#settings_type' => SchemaDotOrgSettings::ASSOCIATIVE_GROUPED,
'#settings_format' => 'SchemaType|prSchemaSubtype01:Subtype 01,SchemaSubtype02:Subtype 02,SchemaSubtype03:Subtype 03',
'#title' => t('Schema.org default subtypes allowed values'),
'#description' => t('Enter default subtype allowed values for Schema.org types.'),
'#description' => t('Enter default subtype allowed values for Schema.org types.')
. ' '
. t('Subtype allowed values that are not valid Schema.org types will be rendered as an <a href=":href">additionalType</a> property via JSON-LD', [':href' => 'https://schema.org/additionalType']),
'#description_link' => 'types',
];
}
<?php
declare(strict_types = 1);
namespace Drupal\Tests\schemadotorg_subtype\Kernel;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\Tests\schemadotorg\Kernel\SchemaDotOrgKernelEntityTestBase;
/**
* Tests the functionality of the Schema.org Subtype JSON-LD.
*
* @covers schemadotorg_subtype_schemadotorg_jsonld_schema_type_entity_alter()
* @group schemadotorg
*/
class SchemaDotOrgSubtypeJsonLdTest extends SchemaDotOrgKernelEntityTestBase {
/**
* Modules to install.
*
* @var string[]
*/
protected static $modules = [
'schemadotorg_jsonld',
'schemadotorg_subtype',
];
/**
* Schema.org JSON-LD builder.
*
* @var \Drupal\schemadotorg_jsonld\SchemaDotOrgJsonLdBuilderInterface
*/
protected $builder;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['schemadotorg_jsonld', 'schemadotorg_subtype']);
$this->builder = $this->container->get('schemadotorg_jsonld.builder');
}
/**
* Test Schema.org Subtype JSON-LD.
*/
public function testSubtypeJsonLd(): void {
\Drupal::currentUser()->setAccount($this->createUser(['access content']));
/** @var \Drupal\schemadotorg\SchemaDotOrgConfigManagerInterface $schema_config_manager */
$schema_config_manager = \Drupal::service('schemadotorg.config_manager');
$schema_config_manager->setSchemaTypeDefaultProperties('Person', 'additionalType');
// Add Caregiver to Person allowed type.
$this->config('schemadotorg_subtype.settings')
->set('default_subtypes', ['Person'])
->set('default_allowed_values.Person', ['Patient' => 'Patient', 'Caregiver' => 'Caregiver'])
->save();
$this->createSchemaEntity('node', 'Person');
$patient_node = Node::create([
'type' => 'person',
'title' => 'Patient',
'schema_person_subtype' => 'Patient',
]);
$patient_node->save();
$caregiver_node = Node::create([
'type' => 'person',
'title' => 'Caregiver',
'schema_person_subtype' => 'Caregiver',
]);
$caregiver_node->save();
// Check that Patient subtype sets the @type to Patient.
$expected_result = [
'@type' => 'Patient',
'@url' => $patient_node->toUrl()->setAbsolute()->toString(),
'name' => 'Patient'
];
$this->assertEquals($expected_result, $this->builder->buildEntity($patient_node));
// Check that Caregiver subtype sets the 'additionalType' property
// to Caregiver.
$expected_result = [
'@type' => 'Person',
'@url' => $caregiver_node->toUrl()->setAbsolute()->toString(),
'name' => 'Caregiver',
'additionalType' => 'Caregiver'
];
$this->assertEquals($expected_result, $this->builder->buildEntity($caregiver_node));
// Add 'Parent' as the additional type.
$caregiver_node->schema_additional_type->value = 'Parent';
$caregiver_node->save();
// Check that Caregiver subtype is merged with the 'Parent'
// additionalType property.
$expected_result = [
'@type' => 'Person',
'@url' => $caregiver_node->toUrl()->setAbsolute()->toString(),
'name' => 'Caregiver',
'additionalType' => [
'Parent',
'Caregiver',
],
];
$this->assertEquals($expected_result, $this->builder->buildEntity($caregiver_node));
}
}
excluded_schema_types: { }
excluded_schema_properties: { }
excluded_schema_properties:
- additionalType
excluded_field_names: { }
included_field_names:
- changed
......
......@@ -73,6 +73,9 @@ class SchemaDotOrgTranslationManagerTest extends SchemaDotOrgKernelEntityTestBas
// @see schemadotorg_translation_schemadotorg_mapping_insert()
/* ********************************************************************** */
// Add additionalTyp property to Place.
$this->appendSchemaTypeDefaultProperties('Place', 'additionalType');
// Create a Schema.org mapping.
$this->createSchemaEntity('node', 'Place');
......@@ -104,6 +107,7 @@ class SchemaDotOrgTranslationManagerTest extends SchemaDotOrgKernelEntityTestBas
$this->assertTrue($field_definitions['schema_address']->isTranslatable());
$this->assertFalse($field_definitions['schema_image']->isTranslatable());
$this->assertFalse($field_definitions['schema_telephone']->isTranslatable());
$this->assertFalse($field_definitions['schema_additional_type']->isTranslatable());
// Check property field added to a Schema.org has translation enabled.
$this->createSchemaDotOrgField('node', 'Place');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment