schemadotorg_experimental-1.0.x-dev/modules/schemadotorg_identifier/schemadotorg_identifier.module
modules/schemadotorg_identifier/schemadotorg_identifier.module
<?php
/**
* @file
* Manages identifiers (https://schema.org/identifier) as Schema.org types.
*/
declare(strict_types=1);
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\schemadotorg\Entity\SchemaDotOrgMapping;
use Drupal\schemadotorg\SchemaDotOrgMappingInterface;
/**
* Implements hook_schemadotorg_mapping_insert().
*/
function schemadotorg_identifier_schemadotorg_mapping_insert(SchemaDotOrgMappingInterface $mapping): void {
if ($mapping->isSyncing()) {
return;
}
/** @var \Drupal\schemadotorg_identifier\SchemaDotOrgIdentifierManagerInterface $identifier_manager */
$identifier_manager = \Drupal::service('schemadotorg_identifier.manager');
$identifier_manager->mappingInsert($mapping);
}
/* ************************************************************************** */
// JSON-LD.
/* ************************************************************************** */
/**
* Implements hook_schemadotorg_jsonld_schema_type_entity_load().
*/
function schemadotorg_identifier_schemadotorg_jsonld_schema_type_entity_load(array &$data, EntityInterface $entity, ?SchemaDotOrgMappingInterface $mapping, BubbleableMetadata $bubbleable_metadata): void {
// Make sure this is a content entity with a mapping.
if (!$entity instanceof ContentEntityInterface
|| !$mapping) {
return;
}
// Get the Schema.org identifier fields.
/** @var \Drupal\schemadotorg_identifier\SchemaDotOrgIdentifierManagerInterface $identifier_manager */
$identifier_manager = \Drupal::service('schemadotorg_identifier.manager');
$identifier_field_definitions = $identifier_manager->getMappingFieldDefinitions($mapping);
if (empty($identifier_field_definitions)) {
return;
}
// Add the Schema.org identifier property values.
$data += ['identifier' => []];
foreach ($identifier_field_definitions as $identifier_field_definition) {
$field_name = $identifier_field_definition['field_name'];
if (!$entity->hasField($field_name)
|| !$entity->get($field_name)->access('view')) {
continue;
}
$value = $entity->get($field_name)->value;
if (is_null($value) || $value === '') {
continue;
}
$data['identifier'][] = [
'@type' => 'PropertyValue',
'propertyID' => $identifier_field_definition['property_id'],
'value' => $value,
];
}
}
/* ************************************************************************** */
// JSON:API.
/* ************************************************************************** */
/**
* Implements hook_jsonapi_resource_config_presave().
*/
function schemadotorg_identifier_jsonapi_resource_config_presave(EntityInterface $entity): void {
/** @var \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $entity */
// Make sure the Schema.org Blueprints JSON:API module is installed.
if (!\Drupal::moduleHandler()->moduleExists('schemadotorg_jsonapi')) {
return;
}
// Make sure this is a new JSON:API resource config.
if (!$entity->isNew()) {
return;
}
// Get the Schema.org mapping.
[$entity_type_id, $bundle] = explode('--', $entity->id());
$mapping = SchemaDotOrgMapping::load("$entity_type_id.$bundle");
if (!$mapping) {
return;
}
// Get the Schema.org identifier fields.
/** @var \Drupal\schemadotorg_identifier\SchemaDotOrgIdentifierManagerInterface $identifier_manager */
$identifier_manager = \Drupal::service('schemadotorg_identifier.manager');
$identifier_field_definitions = $identifier_manager->getMappingFieldDefinitions($mapping);
if (empty($identifier_field_definitions)) {
return;
}
// Enable and update public name for all identifier fields.
$resource_fields = $entity->get('resourceFields');
foreach ($identifier_field_definitions as $identifier_name => $identifier_field_definition) {
$field_name = $identifier_field_definition['field_name'];
if (isset($resource_fields[$field_name])) {
$resource_fields[$field_name]['publicName'] = $identifier_name;
$resource_fields[$field_name]['disabled'] = FALSE;
}
}
$entity->set('resourceFields', $resource_fields);
}
/* ************************************************************************** */
// Schema.org properties settings form.
/* ************************************************************************** */
/**
* Implements hook_form_FORM_ID_alter().
*
* @see \Drupal\schemadotorg\Form\SchemaDotOrgSettingsFormBase::afterBuildDetails
* @see \Drupal\schemadotorg\Form\SchemaDotOrgSettingsFormBase::formAlter
*/
function schemadotorg_identifier_form_schemadotorg_properties_settings_form_alter(array &$form, FormStateInterface $form_state): void {
$form['schemadotorg_identifier'] = [
'#type' => 'details',
'#title' => t('Identifier settings'),
];
$form['schemadotorg_identifier']['field_definitions'] = [
'#type' => 'schemadotorg_settings',
'#title' => t('Identifier field definitions'),
'#description' => t('Enter identifier base field name or custom field definitions which will be available to Schema.org types.'),
'#example' => '
field_name:
property_id: ID
field_name: name
label: Label
description: Some description
required: true
max_length: 10
',
];
$form['schemadotorg_identifier']['schema_types'] = [
'#type' => 'schemadotorg_settings',
'#title' => t('Schema.org types identifiers'),
'#description' => t('Enter Schema.org types and their identifiers.'),
'#example' => '
SchemaType:
- field_name01
- field_name02
- field_name03
entity_type_id--SchemaType:
- field_name01
- field_name02
- field_name03
entity_type_id--bundle--SchemaType:
- field_name01
- field_name02
- field_name03
entity_type_id--bundle:
- field_name01
- field_name02
- field_name03
',
];
}
