Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #70 from ec-europa/limit_graphs
Browse files Browse the repository at this point in the history
Allow limiting the default of graphs when no candidate graphs are passed
  • Loading branch information
idimopoulos authored Oct 8, 2018
2 parents 400c24d + af7a52d commit 400d1af
Show file tree
Hide file tree
Showing 16 changed files with 401 additions and 137 deletions.

This file was deleted.

114 changes: 0 additions & 114 deletions modules/rdf_draft/tests/src/Kernel/RdfDraftGraphTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion rdf_entity.module
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function rdf_entity_type_mapping_submit(array &$form, FormStateInterface $form_s
/** @var \Drupal\rdf_entity\RdfEntityTypeInterface $entity */
$bundle_entity = $form_state->getFormObject()->getEntity();
$mapping = RdfEntityMapping::create([
'entity_type_id' => $bundle_entity->getEntityType()->getBundleOf() ,
'entity_type_id' => $bundle_entity->getEntityType()->getBundleOf(),
'bundle' => $form_state->getFormObject()->getEntity()->id(),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion rdf_entity.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
- { name: route_processor_outbound, priority: 200 }
sparql.graph_handler:
class: \Drupal\rdf_entity\RdfGraphHandler
arguments: ['@entity_type.manager']
arguments: ['@entity_type.manager', '@event_dispatcher']
sparql.field_handler:
class: \Drupal\rdf_entity\RdfFieldHandler
arguments: ['@entity_type.manager', '@entity_field.manager', '@event_dispatcher']
Expand Down
7 changes: 3 additions & 4 deletions src/Entity/Query/Sparql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function graphs(array $graph_ids = NULL): SparqlQueryInterface {
* @deprecated Use the ::graphs() method instead.
*/
public function setGraphType(array $graph_ids = NULL) {
@trigger_error('Drupal\rdf_entity\Entity\Query\Sparql\Query::setGraphType() is deprecated. Please use the ::graphs() method instead.', E_USER_DEPRECATED);
@trigger_error('Drupal\rdf_entity\Entity\Query\Sparql\Query::setGraphType() is deprecated. Use the ::graphs() method instead.', E_USER_DEPRECATED);
$graph_ids = $graph_ids ?: [$this->graphHandler->getDefaultGraphId($this->getEntityTypeId())];
$this->graphs($graph_ids);
}
Expand All @@ -197,9 +197,8 @@ protected function prepare() {
$this->query .= "\n";

if (!$this->graphIds) {
// If no graph IDs were requested, allow all graphs that Drupal is aware
// for this entity type.
$this->graphIds = $this->graphHandler->getEntityTypeGraphIds($this->getEntityTypeId());
// Allow all default graphs for this entity type.
$this->graphIds = $this->graphHandler->getEntityTypeDefaultGraphIds($this->getEntityTypeId());
}
$graph_uris = $this->graphHandler->getEntityTypeGraphUrisFlatList($this->getEntityTypeId(), $this->graphIds);
foreach ($graph_uris as $graph_uri) {
Expand Down
15 changes: 8 additions & 7 deletions src/Entity/RdfEntitySparqlStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,16 +707,17 @@ public function delete(array $entities) {
/** @var \Drupal\Core\Entity\EntityInterface $keyed_entity */
foreach ($keyed_entities as $keyed_entity) {
// Determine all possible graphs for the entity.
$graphs = $this->getGraphHandler()->getEntityTypeGraphUris($this->getEntityTypeId());
foreach ($graphs[$keyed_entity->bundle()] as $graph_name => $graph_uri) {
$graphs_by_bundle = $this->getGraphHandler()->getEntityTypeGraphUris($this->getEntityTypeId());
$graphs = $graphs_by_bundle[$keyed_entity->bundle()];
foreach ($graphs as $graph_name => $graph_uri) {
$entities_by_graph[$graph_uri][$keyed_entity->id()] = $keyed_entity;
}
}
/** @var string $id */
foreach ($entities_by_graph as $graph => $entities_to_delete) {
$this->doDeleteFromGraph($entities_to_delete, $graph);
}
$this->resetCache(array_keys($keyed_entities));
$this->resetCache(array_keys($keyed_entities), array_keys($graphs));

// Allow code to run after deleting.
$entity_class::postDelete($this, $keyed_entities);
Expand Down Expand Up @@ -1284,14 +1285,14 @@ public function idExists(string $id, string $graph = NULL): bool {
* If at least one of passed graphs doesn't exist for this entity type.
*/
protected function checkGraphs(array &$graph_ids = NULL): void {
$entity_type_graph_ids = $this->getGraphHandler()->getEntityTypeGraphIds($this->getEntityTypeId());

if (!$graph_ids) {
// No passed graph means "all graphs for this entity type".
$graph_ids = $entity_type_graph_ids;
// No passed graph means "all default graphs for this entity type".
$graph_ids = $this->getGraphHandler()->getEntityTypeDefaultGraphIds($this->getEntityTypeId());
return;
}

$entity_type_graph_ids = $this->getGraphHandler()->getEntityTypeGraphIds($this->getEntityTypeId());

// Validate each passed graph.
array_walk($graph_ids, function (string $graph_id) use ($entity_type_graph_ids): void {
if (!in_array($graph_id, $entity_type_graph_ids)) {
Expand Down
62 changes: 62 additions & 0 deletions src/Event/DefaultGraphsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Drupal\rdf_entity\Event;

use Symfony\Component\EventDispatcher\Event;

/**
* An event dispatched when the default graph IDs list is built.
*/
class DefaultGraphsEvent extends Event {

/**
* The entity type ID.
*
* @var string
*/
protected $entityTypeId;

/**
* The list of default graph IDs.
*
* @var array
*/
protected $defaultGraphIds = [];

/**
* Instantiates a new event object.
*
* @param string $entity_type_id
* The entity type ID.
* @param array $default_graph_ids
* A list of graph IDs.
*/
public function __construct(string $entity_type_id, array $default_graph_ids) {
$this->entityTypeId = $entity_type_id;
$this->defaultGraphIds = $default_graph_ids;
}

/**
* Sets the list of default graph IDs.
*
* @param array $default_graph_ids
* A list of graph IDs.
*
* @return $this
*/
public function setDefaultGraphIds(array $default_graph_ids): self {
$this->defaultGraphIds = $default_graph_ids;
return $this;
}

/**
* Returns the list of default graph IDs.
*
* @return string[]
* A list of default graph IDs.
*/
public function getDefaultGraphIds(): array {
return $this->defaultGraphIds;
}

}
11 changes: 11 additions & 0 deletions src/Event/RdfEntityEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ final class RdfEntityEvents {
*/
const OUTBOUND_VALUE = 'rdf_entity.outbound_value';

/**
* The name of the event triggered when building the list of default graphs.
*
* @Event
*
* @see \Drupal\rdf_entity\RdfGraphHandler::getEntityTypeDefaultGraphIds()
*
* @var string
*/
const DEFAULT_GRAPHS = 'rdf_entity.default_graphs';

}
34 changes: 33 additions & 1 deletion src/RdfGraphHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\rdf_entity\Entity\RdfEntityMapping;
use Drupal\rdf_entity\Event\DefaultGraphsEvent;
use Drupal\rdf_entity\Event\RdfEntityEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Contains helper methods for managing the RDF entity graphs.
Expand Down Expand Up @@ -35,14 +38,24 @@ class RdfGraphHandler implements RdfGraphHandlerInterface {
*/
protected $rdfEntityGraphStorage;

/**
* The event dispatcher service.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* Constructs a RDF graph handler object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher) {
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
}

/**
Expand Down Expand Up @@ -89,6 +102,25 @@ public function getEntityTypeGraphIds(string $entity_type_id, array $limit_to_gr
return $graph_ids;
}

/**
* {@inheritdoc}
*/
public function getEntityTypeDefaultGraphIds(string $entity_type_id): array {
if (!isset($this->cache['default_graphs'][$entity_type_id])) {
$entity_graph_ids = $this->getEntityTypeGraphIds($entity_type_id);
/** @var \Drupal\rdf_entity\Event\DefaultGraphsEvent $event */
$event = $this->eventDispatcher->dispatch(
RdfEntityEvents::DEFAULT_GRAPHS,
new DefaultGraphsEvent($entity_type_id, $entity_graph_ids)
);
// Do not allow 3rd party code to add invalid or disabled graphs.
$default_graph_ids = array_intersect($event->getDefaultGraphIds(), $entity_graph_ids);

$this->cache['default_graphs'][$entity_type_id] = $default_graph_ids;
}
return $this->cache['default_graphs'][$entity_type_id];
}

/**
* {@inheritdoc}
*/
Expand Down
42 changes: 42 additions & 0 deletions src/RdfGraphHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface RdfGraphHandlerInterface {
*/
const EMPTY_CACHE = [
'definition' => [],
'default_graphs' => [],
'structure' => [],
];

Expand Down Expand Up @@ -55,6 +56,47 @@ public function getGraphDefinitions(string $entity_type_id): array;
*/
public function getEntityTypeGraphIds(string $entity_type_id, array $limit_to_graph_ids = NULL): array;

/**
* Returns a list of default graph IDs.
*
* When requesting an RDF entity, callers are passing a list of candidate
* graph IDs. If the list is missed, the value returned by this method is
* used. This is not necessary the list of all enabled graphs. Third party
* modules might restrict this list. For instance, if graphs 'default', 'foo',
* 'bar' are enabled, a call such as:
* @codingStandardsIgnoreStart
* RdfEntitySparqlStorage::load('http://example.com', ['bar']);
* @codingStandardsIgnoreEnd
* will return the entity from the 'bar' graph (if exists). A module might
* decide to set the default graphs list to 'default', 'foo'. A call such as:
* @codingStandardsIgnoreStart
* RdfEntitySparqlStorage::load('http://example.com');
* @codingStandardsIgnoreEnd
* will search the entity first in 'default' and will fallback to 'foo'. If
* the entity doesn't exist in 'default' or 'foo', will return NULL because
* 'bar' graph is not in the list of default graph IDs.
*
* By default, all enabled graphs are returned. Third party code which intends
* to alter this list should subscribe to the RdfEntityEvents::DEFAULT_GRAPHS
* event and use the \Drupal\rdf_entity\Event\DefaultGraphsEvent event setter
* to set its own preferences.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return array
* A list of default graph IDs.
*
* @see \Drupal\rdf_entity\Entity\RdfEntitySparqlStorage::load()
* @see \Drupal\rdf_entity\Entity\RdfEntitySparqlStorage::loadMultiple()
* @see \Drupal\rdf_entity\Entity\RdfEntitySparqlStorage::loadUnchanged()
* @see \Drupal\rdf_entity\Entity\RdfEntitySparqlStorage::loadByProperties()
* @see \Drupal\rdf_entity\Entity\Query\Sparql\SparqlQueryInterface::graphs()
* @see \Drupal\rdf_entity\Event\RdfEntityEvents::DEFAULT_GRAPHS
* @see \Drupal\rdf_entity\Event\DefaultGraphsEvent
*/
public function getEntityTypeDefaultGraphIds(string $entity_type_id): array;

/**
* Gets the default graph ID for an entity type.
*
Expand Down
Loading

0 comments on commit 400d1af

Please sign in to comment.