Skip to content
Snippets Groups Projects
Commit 89910a29 authored by Seth Shaw's avatar Seth Shaw Committed by Diego Pino Navarro
Browse files

add hook_jsonld_alter_field_mappings (#31)

* add hook_jsonld_alter_field_mappings

* coding standards

* implement hook_jsonld_alter_field_mappings

* cache field mappings and issue warnings for duplicates

* trying to make phpcpd happy

* revised field mappings hook name

* revised field mappings hook name (again)

* update const to match
parent fa3bd9a7
Branches
Tags 0.1.0 8.x-0.1
No related merge requests found
......@@ -30,3 +30,23 @@ function hook_jsonld_alter_normalized_array(EntityInterface $entity, array &$nor
}
}
}
/**
* Hook to alter the field type mappings.
*
* Be aware that drupal field definitions can be complex.
* e.g text_with_summary has a text, a summary, a number of lines, etc
* we are only dealing with the resulting ->value() of all this separate
* pieces and mapping only that as a whole.
*
* @return string[]
* An associative array of field type mappings where the key is the field type
* and the value is the type mapping.
*/
function hook_jsonld_field_mappings() {
return [
"string" => [
"@type" => "xsd:string",
],
];
}
......@@ -20,3 +20,96 @@ function jsonld_help($route_name, RouteMatchInterface $route_match) {
return $output;
}
}
/**
* Implements hook_jsonld_field_mappings().
*/
function jsonld_jsonld_field_mappings() {
return [
"comment" => [
"@type" => "xsd:string",
],
"datetime" => [
"@type" => "xsd:dateTime",
],
"file" => [
"@type" => "@id",
],
"image" => [
"@type" => "@id",
],
"link" => [
"@type" => "xsd:anyURI",
],
"list_float" => [
"@type" => "xsd:float",
"@container" => "@list",
],
"list_integer" => [
"@type" => "xsd:int",
"@container" => "@list",
],
"list_string" => [
"@type" => "xsd:string",
"@container" => "@list",
],
"path" => [
"@type" => "xsd:anyURI",
],
"text" => [
"@type" => "xsd:string",
],
"text_with_summary" => [
"@type" => "xsd:string",
],
"text_long" => [
"@type" => "xsd:string",
],
"uuid" => [
"@type" => "xsd:string",
],
"uri" => [
"@type" => "xsd:anyURI",
],
"language" => [
"@type" => "xsd:language",
],
"string_long" => [
"@type" => "xsd:string",
],
"changed" => [
"@type" => "xsd:dateTime",
],
"map" => "xsd:",
"boolean" => [
"@type" => "xsd:boolean",
],
"email" => [
"@type" => "xsd:string",
],
"integer" => [
"@type" => "xsd:int",
],
"decimal" => [
"@type" => "xsd:decimal",
],
"created" => [
"@type" => "xsd:dateTime",
],
"float" => [
"@type" => "xsd:float",
],
"entity_reference" => [
"@type" => "@id",
],
"timestamp" => [
"@type" => "xsd:dateTime",
],
"string" => [
"@type" => "xsd:string",
],
"password" => [
"@type" => "xsd:string",
],
];
}
......@@ -26,6 +26,11 @@ class JsonldContextGenerator implements JsonldContextGeneratorInterface {
*/
const CACHE_BASE_CID = 'jsonld:context';
/**
* Constant hook alter name.
*/
const FIELD_MAPPPINGS_HOOK = 'jsonld_field_mappings';
/**
* Injected EntityFieldManager.
......@@ -63,6 +68,13 @@ class JsonldContextGenerator implements JsonldContextGeneratorInterface {
*/
protected $logger;
/**
* Cached field type mappings.
*
* @var array
*/
protected $fieldMappings = [];
/**
* Constructs a ContextGenerator object.
*
......@@ -317,96 +329,29 @@ class JsonldContextGenerator implements JsonldContextGeneratorInterface {
"@type" => "xsd:string",
];
$field_mappings = [
"comment" => [
"@type" => "xsd:string",
],
"datetime" => [
"@type" => "xsd:dateTime",
],
"file" => [
"@type" => "@id",
],
"image" => [
"@type" => "@id",
],
"link" => [
"@type" => "xsd:anyURI",
],
"list_float" => [
"@type" => "xsd:float",
"@container" => "@list",
],
"list_integer" => [
"@type" => "xsd:int",
"@container" => "@list",
],
"list_string" => [
"@type" => "xsd:string",
"@container" => "@list",
],
"path" => [
"@type" => "xsd:anyURI",
],
"text" => [
"@type" => "xsd:string",
],
"text_with_summary" => [
"@type" => "xsd:string",
],
"text_long" => [
"@type" => "xsd:string",
],
"uuid" => [
"@type" => "xsd:string",
],
"uri" => [
"@type" => "xsd:anyURI",
],
"language" => [
"@type" => "xsd:language",
],
"string_long" => [
"@type" => "xsd:string",
],
"changed" => [
"@type" => "xsd:dateTime",
],
"map" => "xsd:",
"boolean" => [
"@type" => "xsd:boolean",
],
"email" => [
"@type" => "xsd:string",
],
"integer" => [
"@type" => "xsd:int",
],
"decimal" => [
"@type" => "xsd:decimal",
],
"created" => [
"@type" => "xsd:dateTime",
],
"float" => [
"@type" => "xsd:float",
],
"entity_reference" => [
"@type" => "@id",
],
"timestamp" => [
"@type" => "xsd:dateTime",
],
"string" => [
"@type" => "xsd:string",
],
"password" => [
"@type" => "xsd:string",
],
];
return array_key_exists($field_type, $field_mappings) ? $field_mappings[$field_type] : $default_mapping;
// Only load mappings from hooks if we haven't done it
// yet for this instance.
if (empty($this->fieldMappings)) {
// Cribbed from rdf module's rdf_get_namespaces.
foreach (\Drupal::moduleHandler()->getImplementations(self::FIELD_MAPPPINGS_HOOK) as $module) {
$function = $module . '_' . self::FIELD_MAPPPINGS_HOOK;
foreach ($function() as $field => $mapping) {
if (array_key_exists($field, $this->fieldMappings)) {
$this->logger->warning(
t('Tried to map @field_type to @new_type, but @field_type is already mapped to @orig_type.', [
'@field_type' => $field,
'@new_type' => $mapping['@type'],
'@orig_type' => $this->fieldMappings[$field]['@type'],
])
);
}
else {
$this->fieldMappings[$field] = $mapping;
}
}
}
}
return array_key_exists($field_type, $this->fieldMappings) ? $this->fieldMappings[$field_type] : $default_mapping;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment