Skip to content

Commit

Permalink
add hook_jsonld_alter_field_mappings (#31)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
seth-shaw-unlv authored and DiegoPino committed Dec 19, 2018
1 parent fa3bd9a commit 89910a2
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 90 deletions.
20 changes: 20 additions & 0 deletions jsonld.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
];
}
93 changes: 93 additions & 0 deletions jsonld.module
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
];
}
125 changes: 35 additions & 90 deletions src/ContextGenerator/JsonldContextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -63,6 +68,13 @@ class JsonldContextGenerator implements JsonldContextGeneratorInterface {
*/
protected $logger;

/**
* Cached field type mappings.
*
* @var array
*/
protected $fieldMappings = [];

/**
* Constructs a ContextGenerator object.
*
Expand Down Expand Up @@ -317,96 +329,29 @@ protected function getTermContextFromField($field_type) {
"@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 comments on commit 89910a2

Please sign in to comment.