Skip to content
Snippets Groups Projects
Commit c9782d72 authored by Nicolas Morand's avatar Nicolas Morand Committed by Jacob Rockowitz
Browse files

Issue #3479145 by nicolasgraph, jrockowitz: Adds Physical support

parent b33ad276
No related branches found
Tags 1.0.0-alpha27
1 merge request!202Resolve #3479145 "Adds physical support"
Pipeline #330394 failed
Showing
with 393 additions and 5 deletions
......@@ -50,6 +50,7 @@
"drupal/office_hours": "^1.0",
"drupal/paragraphs": "^1.0",
"drupal/pathauto": "^1.0",
"drupal/physical": "^1.4",
"drupal/range": "^1.0",
"drupal/rdf": "^2.0",
"drupal/scheduler": "^2.0",
......
......@@ -46,6 +46,7 @@
"drupal/office_hours": "^1.0",
"drupal/paragraphs": "^1.0",
"drupal/pathauto": "^1.0",
"drupal/physical": "^1.0",
"drupal/scheduler": "^2.0",
"drupal/simple_sitemap": "^4.0",
"drupal/smart_date": "^4.1",
......
......@@ -272,6 +272,7 @@ for managing optional dependencies and patches.
- [Geolocation Field](https://www.drupal.org/project/geolocation) ⭐ for https://schema.org/GeoCoordinates.
- [Geofield](https://www.drupal.org/project/geofield) an alternative for https://schema.org/GeoCoordinates. No integration is being provided.
- [Office Hours](https://www.drupal.org/project/office_hours) ⭐ for https://schema.org/OpeningHoursSpecification
- [Physical Fields](https://www.drupal.org/project/physical) ⭐ for https://schema.org/QuantitativeValue
- [Range](https://www.drupal.org/project/range) ⭐ for https://schema.org/MonetaryAmount
- [SmartDate](https://www.drupal.org/project/smart_date) ⭐ for https://schema.org/Date and https://schema.org/Schedule
- [Time Field](https://www.drupal.org/project/time_field) for https://schema.org/Time
......
......@@ -102,6 +102,9 @@ and field configuration settings.
- **[Schema.org Blueprints Options](https://git.drupalcode.org/project/schemadotorg/-/tree/1.0.x/modules/schemadotorg_options)**
Set allowed values for list (options) fields.
- **[Schema.org Blueprints Physical Fields](https://git.drupalcode.org/project/schemadotorg/-/tree/1.0.x/modules/schemadotorg_physical)**
Allows an Physical fields to be used to create a http://schema.org/QuantitativeValue.
- **[Schema.org Blueprints Smart Date](https://git.drupalcode.org/project/schemadotorg/-/tree/1.0.x/modules/schemadotorg_smart_date)**
Allows a Smart date field to be used to create date ranges and event schedules included in a site's Schema.org JSON-LD.
......@@ -222,4 +225,3 @@ The below API related modules creates APIs based on Schema.org types
- **[Schema.org Blueprints Demo](https://git.drupalcode.org/project/schemadotorg_demo/-/tree/1.0.x/README.md)**
Provides an opinionated demo of the Schema.org Blueprints built on top of Drupal's standard profile with default content and translations.
......@@ -19,7 +19,7 @@ function schemadotorg_office_hours_install(bool $is_syncing): void {
$schema_config_manager = \Drupal::service('schemadotorg.config_manager');
// Add openingHoursSpecification to Place's default properties.
$schema_config_manager->setSchemaTypeDefaultProperties('Place', 'openingHoursSpecification');
$schema_config_manager->setSchemaTypeDefaultProperties('Place', ['openingHoursSpecification', 'specialOpeningHoursSpecification']);
$config = \Drupal::configFactory()->getEditable('schemadotorg.settings');
......@@ -52,7 +52,7 @@ function schemadotorg_office_hours_uninstall(bool $is_syncing): void {
$schema_config_manager = \Drupal::service('schemadotorg.config_manager');
// Remove openingHoursSpecification to Place's default properties.
$schema_config_manager->unsetSchemaTypeDefaultProperties('Place', 'openingHoursSpecification');
$schema_config_manager->unsetSchemaTypeDefaultProperties('Place', ['openingHoursSpecification', 'specialOpeningHoursSpecification']);
$config = \Drupal::configFactory()->getEditable('schemadotorg.settings');
......
......@@ -55,7 +55,7 @@ class SchemaDotOrgOfficeHoursInstallTest extends TokenKernelTestBase {
// Check adding openingHoursSpecification to Place's default properties.
$this->assertEquals(
['address', 'description', 'image', 'latitude', 'longitude', 'name', 'openingHoursSpecification', 'telephone'],
['address', 'description', 'image', 'latitude', 'longitude', 'name', 'openingHoursSpecification', 'specialOpeningHoursSpecification', 'telephone'],
\Drupal::config('schemadotorg.settings')->get('schema_types.default_properties.Place')
);
......
Table of contents
-----------------
* Introduction
* Features
* Requirements
Introduction
------------
The **Schema.org Blueprints Physical module** allows an physical fields to be
used to create a https://schema.org/QuantitativeValue.
Features
--------
- Support physical measurement and dimensions field type.
- Defaults https://schema.org/QuantitativeValue to use the physical fields.
Requirements
------------
- **[Physical](https://www.drupal.org/project/physical)**
Provides an API for storing and manipulating physical measurements.
name: 'Schema.org Blueprints Physical'
type: module
description: 'Allows an physical fields to be used to create a https://schema.org/QuantitativeValue.'
package: 'Schema.org Blueprints Fields'
core_version_requirement: ^10.3 || ^11
schemadotorg_production: true
dependencies:
- 'schemadotorg:schemadotorg'
- 'physical:physical'
<?php
/**
* @file
* Installation hooks for the Schema.org Blueprints Physical module.
*/
declare(strict_types=1);
/**
* Implements hook_install().
*/
function schemadotorg_physical_install(bool $is_syncing): void {
if ($is_syncing) {
return;
}
/** @var \Drupal\schemadotorg\SchemaDotOrgConfigManagerInterface $schema_config_manager */
$schema_config_manager = \Drupal::service('schemadotorg.config_manager');
// Add width, height and weight to Person's default properties.
$schema_config_manager->setSchemaTypeDefaultProperties('IndividualProduct', ['depth', 'height', 'width']);
// Add physical_dimensions and physical_measurement field to QuantitativeValue field types.
$config = \Drupal::configFactory()->getEditable('schemadotorg.settings');
$quantitative_value_config_key = 'schema_types.default_field_types.QuantitativeValue';
$config->set($quantitative_value_config_key, array_merge(
['physical_dimensions', 'physical_measurement'],
$config->get($quantitative_value_config_key) ?: []
));
// Default to physical_measurement field type for depth, height, weight and width properties.
$config->set('schema_properties.default_fields.depth.type', 'field_ui:physical_measurement:length');
$config->set('schema_properties.default_fields.height.type', 'field_ui:physical_measurement:length');
$config->set('schema_properties.default_fields.weight.type', 'field_ui:physical_measurement:weight');
$config->set('schema_properties.default_fields.width.type', 'field_ui:physical_measurement:length');
// Add length field to Distance field types.
$config->set('schema_types.default_field_types.Distance', ['field_ui:physical_measurement:length']);
$config->save();
}
/**
* Implements hook_uninstall().
*/
function schemadotorg_physical_uninstall(bool $is_syncing): void {
if ($is_syncing) {
return;
}
/** @var \Drupal\schemadotorg\SchemaDotOrgConfigManagerInterface $schema_config_manager */
$schema_config_manager = \Drupal::service('schemadotorg.config_manager');
// Remove width, height and weight to Person's default properties.
$schema_config_manager->unsetSchemaTypeDefaultProperties('IndividualProduct', ['depth', 'height', 'width']);
// Remove physical_dimensions and physical_measurement field from QuantitativeValue field types.
$config = \Drupal::configFactory()->getEditable('schemadotorg.settings');
$quantitative_value_config_key = 'schema_types.default_field_types.QuantitativeValue';
$config->set($quantitative_value_config_key, array_values(array_diff(
$config->get($quantitative_value_config_key) ?: [],
['physical_dimensions', 'physical_measurement']
)));
// Restore field type for depth, height, weight and width properties.
$config->set('schema_properties.default_fields.depth.type', NULL);
$config->set('schema_properties.default_fields.height.type', 'string');
$config->set('schema_properties.default_fields.weight.type', NULL);
$config->set('schema_properties.default_fields.width.type', 'string');
// Remove length field from Distance field types.
$config->clear('schema_types.default_field_types.Distance');
$config->save();
}
<?php
/**
* @file
* Allows Physical fields to be used to create a http://schema.org/QuantitativeValue.
*/
declare(strict_types=1);
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\physical\Plugin\Field\FieldType\DimensionsItem;
use Drupal\physical\Plugin\Field\FieldType\MeasurementItem;
/**
* Implements hook_schemadotorg_jsonld_schema_property_alter().
*/
function schemadotorg_physical_schemadotorg_jsonld_schema_property_alter(mixed &$value, FieldItemInterface $item, BubbleableMetadata $bubbleable_metadata): void {
if ($item instanceof MeasurementItem) {
$value = [
'@type' => 'QuantitativeValue',
'value' => $item->number,
'unitText' => $item->unit,
];
}
elseif ($item instanceof DimensionsItem) {
$value = [];
$dimension_properties = ['length', 'height', 'width'];
foreach ($dimension_properties as $dimension_property) {
$value[] = [
'@type' => 'QuantitativeValue',
'name' => $dimension_property,
'value' => $item->{$dimension_property},
'unitText' => $item->unit,
];
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\schemadotorg_physical\Kernel;
use Drupal\Tests\schemadotorg_jsonld\Kernel\SchemaDotOrgJsonLdKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\physical\LengthUnit;
use Drupal\schemadotorg_jsonld\SchemaDotOrgJsonLdBuilderInterface;
/**
* Tests the functionality of the Schema.org Physical module JSON-LD integration.
*
* @covers schemadotorg_physical_schemadotorg_jsonld_schema_property_alter(()
* @group schemadotorg
*/
class SchemaDotOrgPhysicalJsonLdKernelTest extends SchemaDotOrgJsonLdKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'physical',
'schemadotorg_physical',
];
/**
* Schema.org JSON-LD builder.
*/
protected SchemaDotOrgJsonLdBuilderInterface $builder;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
\Drupal::moduleHandler()->loadInclude('schemadotorg_physical', 'install');
schemadotorg_physical_install(FALSE);
}
/**
* Test Schema.org Physical JSON-LD.
*/
public function testJsonLdPhysical(): void {
\Drupal::currentUser()->setAccount($this->createUser(['access content']));
$this->config('schemadotorg.settings')
->set('schema_properties.default_fields.size.type', 'physical_dimensions')
->save();
$this->appendSchemaTypeDefaultProperties('IndividualProduct', ['size']);
$this->createSchemaEntity('node', 'IndividualProduct');
// Create a product node.
$product_node = Node::create([
'type' => 'individual_product',
'title' => 'Individual product',
'schema_depth' => [['number' => '100', 'unit' => LengthUnit::MILLIMETER]],
'schema_height' => [['number' => '200', 'unit' => LengthUnit::MILLIMETER]],
'schema_width' => [['number' => '300', 'unit' => LengthUnit::MILLIMETER]],
'schema_size' => [['length' => '10', 'height' => '10', 'width' => '10', 'unit' => LengthUnit::MILLIMETER]],
]);
$product_node->save();
$product_jsonld = $this->builder->buildEntity($product_node);
// Check depth JSON-LD.
$expected_depth = [
'@type' => 'QuantitativeValue',
'unitText' => LengthUnit::MILLIMETER,
'value' => '100',
];
$this->assertEquals($expected_depth, $product_jsonld['depth']);
// Check height JSON-LD.
$expected_height = [
'@type' => 'QuantitativeValue',
'unitText' => LengthUnit::MILLIMETER,
'value' => '200',
];
$this->assertEquals($expected_height, $product_jsonld['height']);
// Check width JSON-LD.
$expected_width = [
'@type' => 'QuantitativeValue',
'unitText' => LengthUnit::MILLIMETER,
'value' => '300',
];
$this->assertEquals($expected_width, $product_jsonld['width']);
// Check size JSON-LD.
$expected_size = [
[
'@type' => 'QuantitativeValue',
'value' => '10',
'unitText' => 'mm',
'name' => 'length',
],
[
'@type' => 'QuantitativeValue',
'value' => '10',
'unitText' => 'mm',
'name' => 'height',
],
[
'@type' => 'QuantitativeValue',
'value' => '10',
'unitText' => 'mm',
'name' => 'width',
],
];
$this->assertEquals($expected_size, $product_jsonld['size']);
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\schemadotorg_physical\Kernel;
use Drupal\Tests\schemadotorg\Kernel\SchemaDotOrgEntityKernelTestBase;
/**
* Tests the functionality of the Schema.org Physical module.
*
* @covers schemadotorg_physical_install()
* @covers schemadotorg_physical_uninstall()
* @group schemadotorg
*/
class SchemaDotOrgPhysicalKernelTest extends SchemaDotOrgEntityKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'physical',
'schemadotorg_physical',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['schemadotorg_physical']);
}
/**
* Test Schema.org Blueprints Physical installation.
*/
public function testInstall(): void {
\Drupal::moduleHandler()->loadInclude('schemadotorg_physical', 'install');
$config = \Drupal::config('schemadotorg.settings');
// Check IndividualProduct's default properties.
$this->assertEquals(NULL, \Drupal::config('schemadotorg.settings')->get('schema_types.default_properties.IndividualProduct'));
// Check QuantitativeValue default field types is 'number'.
$default_field_types = [
'integer',
'decimal',
'float',
'range_integer',
'range_decimal',
'range_float',
];
$this->assertEquals($default_field_types, $config->get('schema_types.default_field_types.QuantitativeValue'));
// Check some properties initial default field types.
$this->assertEquals(NULL, $config->get('schema_properties.default_fields.depth.type'));
$this->assertEquals('string', $config->get('schema_properties.default_fields.height.type'));
$this->assertEquals(NULL, $config->get('schema_properties.default_fields.weight.type'));
$this->assertEquals('string', $config->get('schema_properties.default_fields.width.type'));
// Install the Schema.org Blueprints Physical module.
schemadotorg_physical_install(FALSE);
// Check adding width, height and weight to IndividualProduct's default properties.
$new_default_properties = [
'depth',
'height',
'width',
];
$this->assertEquals($new_default_properties, \Drupal::config('schemadotorg.settings')->get('schema_types.default_properties.IndividualProduct'));
// Check QuantitativeValue default field types has now also Physical field types.
$new_default_field_types = [
'physical_dimensions',
'physical_measurement',
'integer',
'decimal',
'float',
'range_integer',
'range_decimal',
'range_float',
];
$this->assertEquals($new_default_field_types, $config->get('schema_types.default_field_types.QuantitativeValue'));
// Check some properties altered default field types.
$this->assertEquals('field_ui:physical_measurement:length', $config->get('schema_properties.default_fields.depth.type'));
$this->assertEquals('field_ui:physical_measurement:length', $config->get('schema_properties.default_fields.height.type'));
$this->assertEquals('field_ui:physical_measurement:weight', $config->get('schema_properties.default_fields.weight.type'));
$this->assertEquals('field_ui:physical_measurement:length', $config->get('schema_properties.default_fields.width.type'));
// Check length field used for Distance.
$this->assertEquals(['field_ui:physical_measurement:length'], $config->get('schema_types.default_field_types.Distance'));
// Uninstall the Schema.org Blueprints Physical module.
schemadotorg_physical_uninstall(FALSE);
// Check QuantitativeValue default field types is 'string_long'.
$this->assertEquals($default_field_types, $config->get('schema_types.default_field_types.QuantitativeValue'));
// Check some properties initial default field types.
$this->assertEquals('string', $config->get('schema_properties.default_fields.height.type'));
$this->assertEquals(NULL, $config->get('schema_properties.default_fields.weight.type'));
$this->assertEquals('string', $config->get('schema_properties.default_fields.width.type'));
// Check removing width, height and weight to IndividualProduct's default properties.
$this->assertEquals([], \Drupal::config('schemadotorg.settings')->get('schema_types.default_properties.IndividualProduct'));
// Check remove length field used for Distance.
$this->assertEquals(NULL, $config->get('schema_types.default_field_types.Distance'));
}
}
modules/schemadotorg_type_tray/images/schemadotorg_type_tray/icon/individual_product.png

1.19 KiB

......@@ -93,6 +93,10 @@ parameters:
count: 1
path: modules/schemadotorg_smart_date/src/SchemaDotOrgSmartDateJsonLdManager.php
- message: "#^Access to an undefined property Drupal\\\\physical\\\\Plugin\\\\Field\\\\FieldType\\\\(Measurement|Dimensions)Item\\:\\:\\$(number|unit|height|width|length)\\.$#"
count: 6
path: modules/schemadotorg_physical/schemadotorg_physical.module
##########################################################################
# Fields.
##########################################################################
......@@ -100,7 +104,7 @@ parameters:
- message: "#^Parameter \\#1 \\$item of method Drupal\\\\schemadotorg_jsonld\\\\SchemaDotOrgJsonLdManagerInterface\\:\\:getSchemaPropertyValue\\(\\) expects Drupal\\\\Core\\\\Field\\\\FieldItemInterface, Drupal\\\\Core\\\\TypedData\\\\TypedDataInterface\\|null given\\.$#"
count: 5
path: modules/schemadotorg_jsonld/tests/src/Kernel/SchemaDotOrgJsonLdManagerKernelTest.php
- message: "#^Parameter \\#2 \\$times of function str_repeat expects int, Drupal\\\\Core\\\\Field\\\\FieldItemListInterface given.$#"
count: 1
path: src/Plugin/EntityReferenceSelection/SchemaDotOrgTermReferenceSelection.php
......
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