Browse Source

Only adding rdf:type to the first entry in the jsonld graph (#131)

* Only adding rdf:type to the first entry in the jsonld graph

* Whitespace

* Searching for entity in graph instead of assuming it's the first
pull/729/head
dannylamb 5 years ago committed by Jared Whiklo
parent
commit
8fbf5163dc
  1. 48
      src/Plugin/ContextReaction/JsonldTypeAlterReaction.php

48
src/Plugin/ContextReaction/JsonldTypeAlterReaction.php

@ -28,30 +28,40 @@ class JsonldTypeAlterReaction extends NormalizerAlterReaction {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL) { public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL) {
// Check that the source field exists and there's some RDF
// to manipulate.
$config = $this->getConfiguration(); $config = $this->getConfiguration();
// Use a pre-configured field as the source of the additional @type. $ok = $entity->hasField($config['source_field']) &&
if (($entity->hasField($config['source_field'])) && !empty($entity->get($config['source_field'])->getValue()) &&
(!empty($entity->get($config['source_field'])->getValue()))) { isset($normalized['@graph']) &&
if (isset($normalized['@graph']) && is_array($normalized['@graph'])) { is_array($normalized['@graph']) &&
foreach ($normalized['@graph'] as &$graph) { !empty($normalized['@graph']);
foreach ($entity->get($config['source_field'])->getValue() as $type) {
// If the configured field is using an entity reference, if (!$ok) {
// we will see if it uses the core config's field_external_uri. return;
if (array_key_exists('target_id', $type)) { }
$target_type = $entity->get($config['source_field'])->getFieldDefinition()->getSetting('target_type');
$referenced_entity = \Drupal::entityTypeManager()->getStorage($target_type)->load($type['target_id']); // Search for the entity in the graph.
if ($referenced_entity->hasField('field_external_uri') && foreach ($normalized['@graph'] as &$elem) {
!empty($referenced_entity->get('field_external_uri')->getValue())) { if ($elem['@id'] === $entity->toUrl()->setAbsolute()->toString() . '?_format=jsonld') {
foreach ($referenced_entity->get('field_external_uri')->getValue() as $value) { foreach ($entity->get($config['source_field'])->getValue() as $type) {
$graph['@type'][] = $value['uri']; // If the configured field is using an entity reference,
} // we will see if it uses the core config's field_external_uri.
if (array_key_exists('target_id', $type)) {
$target_type = $entity->get($config['source_field'])->getFieldDefinition()->getSetting('target_type');
$referenced_entity = \Drupal::entityTypeManager()->getStorage($target_type)->load($type['target_id']);
if ($referenced_entity->hasField('field_external_uri') &&
!empty($referenced_entity->get('field_external_uri')->getValue())) {
foreach ($referenced_entity->get('field_external_uri')->getValue() as $value) {
$elem['@type'][] = $value['uri'];
} }
} }
else { }
$graph['@type'][] = NormalizerBase::escapePrefix($type['value'], $context['namespaces']); else {
} $elem['@type'][] = NormalizerBase::escapePrefix($type['value'], $context['namespaces']);
} }
} }
return;
} }
} }
} }

Loading…
Cancel
Save