@ -13,16 +13,18 @@ use Drupal\islandora\IslandoraUtils;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
/**
* Map URI to predicate context reaction.
* Create a self-reference in RDF when creating JSON-LD.
*
* Formerly called "Map URI to predicate". Renamed for clarity.
*
*
* @ContextReaction(
* @ContextReaction(
* id = "islandora_map_uri_predicate",
* id = "islandora_map_uri_predicate",
* label = @Translation("Map URI to predicat e")
* label = @Translation("JSON-LD self-referenc e")
* )
* )
*/
*/
class MappingUriPredicat eReaction extends NormalizerAlterReaction {
class JsonldSelfReferenc eReaction extends NormalizerAlterReaction {
const URI _PREDICATE = 'drupal_uri_predicate';
const SELF_REFERENCE _PREDICATE = 'drupal_uri_predicate';
/**
/**
* Media source service.
* Media source service.
@ -69,7 +71,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function summary() {
public function summary() {
return $this->t('Map Drupal URI to configured predicate.');
return $this->t('When creating the JSON-LD for this Drupal entity, add a relationship to itself using this predicate.');
}
}
/**
/**
@ -77,11 +79,11 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
*/
*/
public function execute(EntityInterface $entity = NULL, array & $normalized = NULL, array $context = NULL) {
public function execute(EntityInterface $entity = NULL, array & $normalized = NULL, array $context = NULL) {
$config = $this->getConfiguration();
$config = $this->getConfiguration();
$drupal_predicate = $config[self::URI _PREDICATE];
$self_ref_predicate = $config[self::SELF_REFERENCE _PREDICATE];
if (!is_null($drupal_predicate) & & !empty($drupal _predicate)) {
if (!is_null($self_ref_predicate) & & !empty($self_ref _predicate)) {
$url = $this->getSubjectUrl($entity);
$url = $this->getSubjectUrl($entity);
if ($context['needs_jsonldcontext'] === FALSE) {
if ($context['needs_jsonldcontext'] === FALSE) {
$drupal_predicate = NormalizerBase::escapePrefix($drupal _predicate, $context['namespaces']);
$self_ref_predicate = NormalizerBase::escapePrefix($self_ref _predicate, $context['namespaces']);
}
}
if (isset($normalized['@graph']) & & is_array($normalized['@graph'])) {
if (isset($normalized['@graph']) & & is_array($normalized['@graph'])) {
foreach ($normalized['@graph'] as & $graph) {
foreach ($normalized['@graph'] as & $graph) {
@ -91,24 +93,24 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
$file = $this->mediaSource->getSourceFile($entity);
$file = $this->mediaSource->getSourceFile($entity);
$graph['@id'] = $this->utils->getDownloadUrl($file);
$graph['@id'] = $this->utils->getDownloadUrl($file);
}
}
if (isset($graph[$drupal _predicate])) {
if (isset($graph[$self_ref _predicate])) {
if (!is_array($graph[$drupal _predicate])) {
if (!is_array($graph[$self_ref _predicate])) {
if ($graph[$drupal _predicate] == $url) {
if ($graph[$self_ref _predicate] == $url) {
// Don't add it if it already exists.
// Don't add it if it already exists.
return;
return;
}
}
$tmp = $graph[$drupal _predicate];
$tmp = $graph[$self_ref _predicate];
$graph[$drupal _predicate] = [$tmp];
$graph[$self_ref _predicate] = [$tmp];
}
}
elseif (array_search($url, array_column($graph[$drupal _predicate], '@id'))) {
elseif (array_search($url, array_column($graph[$self_ref _predicate], '@id'))) {
// Don't add it if it already exists.
// Don't add it if it already exists.
return;
return;
}
}
}
}
else {
else {
$graph[$drupal _predicate] = [];
$graph[$self_ref _predicate] = [];
}
}
$graph[$drupal _predicate][] = ["@id" => $url];
$graph[$self_ref _predicate][] = ["@id" => $url];
return;
return;
}
}
}
}
@ -121,11 +123,11 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
*/
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this->getConfiguration();
$config = $this->getConfiguration();
$form[self::URI _PREDICATE] = [
$form[self::SELF_REFERENCE _PREDICATE] = [
'#type' => 'textfield',
'#type' => 'textfield',
'#title' => $this->t('Drupal URI predicate'),
'#title' => $this->t('Self-reference predicate'),
'#description' => $this->t("The Drupal object's URI will be added to the resource with this predicate. Must use a defined prefix."),
'#description' => $this->t("When creating the JSON-LD for this Drupal entity, add a relationship from the entity to itself using this predicate. It must use a defined RDF namespace prefix."),
'#default_value' => isset($config[self::URI_PREDICATE]) ? $config[self::URI _PREDICATE] : '',
'#default_value' => isset($config[self::SELF_REFERENCE_PREDICATE]) ? $config[self::SELF_REFERENCE _PREDICATE] : '',
'#size' => 35,
'#size' => 35,
];
];
return $form;
return $form;
@ -135,19 +137,19 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function validateConfigurationForm(array & $form, FormStateInterface $form_state) {
public function validateConfigurationForm(array & $form, FormStateInterface $form_state) {
$drupal_predicate = $form_state->getValue(self::URI _PREDICATE);
$self_ref_predicate = $form_state->getValue(self::SELF_REFERENCE _PREDICATE);
if (!is_null($drupal_predicate) and !empty($drupal _predicate)) {
if (!is_null($self_ref_predicate) and !empty($self_ref _predicate)) {
if (preg_match('/^https?:\/\//', $drupal _predicate)) {
if (preg_match('/^https?:\/\//', $self_ref _predicate)) {
// Can't validate all URIs so we have to trust them.
// Can't validate all URIs so we have to trust them.
return;
return;
}
}
elseif (preg_match('/^([^\s:]+):/', $drupal _predicate, $matches)) {
elseif (preg_match('/^([^\s:]+):/', $self_ref _predicate, $matches)) {
$predicate_prefix = $matches[1];
$predicate_prefix = $matches[1];
$rdf = rdf_get_namespaces();
$rdf = rdf_get_namespaces();
$rdf_prefixes = array_keys($rdf);
$rdf_prefixes = array_keys($rdf);
if (!in_array($predicate_prefix, $rdf_prefixes)) {
if (!in_array($predicate_prefix, $rdf_prefixes)) {
$form_state->setErrorByName(
$form_state->setErrorByName(
self::URI _PREDICATE,
self::SELF_REFERENCE _PREDICATE,
$this->t('Namespace prefix @prefix is not registered.',
$this->t('Namespace prefix @prefix is not registered.',
['@prefix' => $predicate_prefix]
['@prefix' => $predicate_prefix]
)
)
@ -156,7 +158,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
}
}
else {
else {
$form_state->setErrorByName(
$form_state->setErrorByName(
self::URI _PREDICATE,
self::SELF_REFERENCE _PREDICATE,
$this->t('Predicate must use a defined prefix or be a full URI')
$this->t('Predicate must use a defined prefix or be a full URI')
);
);
}
}
@ -168,7 +170,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction {
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function submitConfigurationForm(array & $form, FormStateInterface $form_state) {
public function submitConfigurationForm(array & $form, FormStateInterface $form_state) {
$this->setConfiguration([self::URI_PREDICATE => $form_state->getValue(self::URI _PREDICATE)]);
$this->setConfiguration([self::SELF_REFERENCE_PREDICATE => $form_state->getValue(self::SELF_REFERENCE _PREDICATE)]);
}
}
}
}