schemadotorg_starterkit_medical-1.0.x-dev/schemadotorg_starterkit_medical.module
schemadotorg_starterkit_medical.module
<?php
/**
* @file
* A starter kit that creates Schema.org MedicalEntity types with default content.
*/
declare(strict_types=1);
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\node\NodeInterface;
use Drupal\schemadotorg\SchemaDotOrgMappingInterface;
/**
* Implements hook_node_links_alter().
*/
function schemadotorg_starterkit_medical_node_links_alter(array &$links, NodeInterface $node, array &$context): void {
// Alters the Schema.org Blueprints Entity Prepopulate node links to use
// the node's about target id instead of the node's id.
$prepopulate_links = &NestedArray::getValue($links, ['schemadotorg_epp', '#links']);
if (!$prepopulate_links) {
return;
}
$about = ($node->hasField('schema_about'))
? $node->schema_about->target_id
: NULL;
if (empty($about)) {
return;
}
foreach ($prepopulate_links as &$prepopulate_link) {
/** @var \Drupal\Core\Url $url */
$url =& $prepopulate_link['url'];
$query = $url->getOption('query');
if (isset($query['about'])) {
$query['about'] = $about;
$url->setOption('query', $query);
}
}
}
/**
* Implements hook_schemadotorg_jsonld_schema_type_entity_alter().
*
* For HealthTopicContent JSON-LD, copy Schema.org properties from the
* https://schema.org/about target entity.
*/
function schemadotorg_starterkit_medical_schemadotorg_jsonld_schema_type_entity_alter(array &$data, EntityInterface $entity, ?SchemaDotOrgMappingInterface $mapping, BubbleableMetadata $bubbleable_metadata): void {
if (!$entity instanceof NodeInterface
|| !$mapping
|| $mapping->getSchemaType() !== 'HealthTopicContent') {
return;
}
$about_node = ($entity->hasField('schema_about'))
? $entity->schema_about->entity
: NULL;
if (empty($about_node)) {
return;
}
// HealthAspectEnumeration enumerates several aspects of health content online.
// @see https://schema.org/HealthAspectEnumeration
$health_aspects = [
'CausesHealthAspect' => [
'differentialDiagnosis',
'pathophysiology',
],
'PreventionHealthAspect' => [
'primaryPrevention',
'secondaryPrevention',
],
'PrognosisHealthAspect' => [
'expectedPrognosis',
'naturalProgression',
],
'RisksOrComplicationsHealthAspect' => [
'riskFactor',
],
'ScreeningHealthAspect' => [
'typicalTest',
],
'SymptomsHealthAspect' => [
'signOrSymptom',
],
'TreatmentsHealthAspect' => [
'possibleTreatment',
],
];
// Set about properties that should always be copied to the 'about' entity.
$about_properties = [
'description' => 'description',
];
// Append health aspect properties to should be copied to the 'about' entity.
if (isset($data['hasHealthAspect'])) {
foreach ($data['hasHealthAspect'] as $health_aspect) {
if (!empty($health_aspects[$health_aspect])) {
$about_properties += array_combine($health_aspects[$health_aspect], $health_aspects[$health_aspect]);
}
}
}
// Copy about properties to the main JSON-LD.
/** @var \Drupal\schemadotorg_jsonld\SchemaDotOrgJsonLdBuilderInterface $jsonld_builder */
$jsonld_builder = \Drupal::service('schemadotorg_jsonld.builder');
$about_data = $jsonld_builder->buildEntity($about_node);
$data['about'][0] += array_intersect_key($about_data, $about_properties);
}
