diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldBundle.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldBundle.php index 4d086d1..6ea8ca2 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldBundle.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldBundle.php @@ -38,6 +38,10 @@ public function build(ContainerBuilder $container) { 'entity' => array( 'jsonld' => 'Drupal\jsonld\JsonldEntityNormalizer', ), + // RDF Schema. + 'rdf_schema' => array( + 'jsonld' => 'Drupal\jsonld\JsonldRdfSchemaNormalizer', + ), ); // Encoders can only specify which format they support in // Encoder::supportsEncoding(). diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldRdfSchemaNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldRdfSchemaNormalizer.php new file mode 100644 index 0000000..ab2ed9a --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldRdfSchemaNormalizer.php @@ -0,0 +1,51 @@ +getGraph(); + + foreach ($graph as $termUri => $properties) { + // JSON-LD uses the @type keyword as a stand-in for rdf:type. Replace any + // use of rdf:type and move the type to the front of the property array. + if (isset($properties[RdfConstants::RDF_TYPE])) { + $properties = array( + '@type' => $properties[RdfConstants::RDF_TYPE], + ) + $properties; + } + unset($properties[RdfConstants::RDF_TYPE]); + + // Add the @id keyword to the front of the array. + $normalized[] = array( + '@id' => $termUri, + ) + $properties; + } + + return $normalized; + } + +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php new file mode 100644 index 0000000..7560c04 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php @@ -0,0 +1,52 @@ + 'Site schema JSON-LD serialization', + 'description' => 'Tests the JSON-LD serialization of the RDF site schema.', + 'group' => 'JSON-LD', + ); + } + + /** + * Tests the serialization of site schemas. + */ + function testSchemaSerialization() { + // In order to use url() the url_alias table must be installed, so system + // is enabled. + $this->enableModules(array('system')); + + $entityType = $bundle = 'entity_test'; + + // Set up the bundle schema for the entity_test bundle. + $schema = new BundleSchema(new SiteSchema(SiteSchema::CONTENT_DEPLOYMENT), $entityType, $bundle); + // Set up the serializer. + $serializer = new Serializer(array(new JsonldRdfSchemaNormalizer()), array(new JsonldEncoder())); + + $serialized = $serializer->serialize($schema, 'jsonld'); + $decoded = json_decode($serialized); + $parsedTerm = $decoded[0]; + + $this->assertEqual($parsedTerm->{'@id'}, $schema->getUri(), 'JSON-LD for schema term uses correct @id.'); + $this->assertEqual($parsedTerm->{'@type'}, 'http://www.w3.org/2000/01/rdf-schema#class', 'JSON-LD for schema term uses correct @type.'); + // The @id and @type should be placed in the beginning of the array. + $arrayKeys = array_keys((array) $parsedTerm); + $this->assertEqual(array('@id', '@type'), array_slice($arrayKeys, 0, 2), 'JSON-LD keywords are placed before other properties.'); + $this->assertTrue(isset($parsedTerm->{'http://www.w3.org/2000/01/rdf-schema#isDefinedBy'}), 'Other properties of the term are included.'); + } +} diff --git a/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/RouteSubscriber.php b/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/RouteSubscriber.php index f05d12b..ea045ca 100644 --- a/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/RouteSubscriber.php +++ b/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/RouteSubscriber.php @@ -43,6 +43,7 @@ public function routes(RouteBuildEvent $event) { 'schema_path' => $schemaPath, ), array( '_method' => 'GET', + '_access' => 'TRUE', )); // Create the route name to use in the RouteCollection. Remove the // trailing slash and replace characters, so that a path such as diff --git a/core/modules/rdf/lib/Drupal/rdf/SiteSchema/EntitySchema.php b/core/modules/rdf/lib/Drupal/rdf/SiteSchema/EntitySchema.php index 362494e..6266173 100644 --- a/core/modules/rdf/lib/Drupal/rdf/SiteSchema/EntitySchema.php +++ b/core/modules/rdf/lib/Drupal/rdf/SiteSchema/EntitySchema.php @@ -38,6 +38,17 @@ public function __construct($siteSchema, $entity_type) { } /** + * Implements \Drupal\rdf\SiteSchema\SchemaBase::getGraph(). + * + * @todo Loop through all fields and add their RDF descriptions. + */ + public function getGraph() { + $graph = array(); + $graph[$this->getUri()] = $this->getProperties(); + return $graph; + } + + /** * Implements \Drupal\rdf\SiteSchema\SchemaBase::getUri(). */ public function getUri() { diff --git a/core/modules/rdf/lib/Drupal/rdf/SiteSchema/SchemaBase.php b/core/modules/rdf/lib/Drupal/rdf/SiteSchema/SchemaBase.php index 429788b..11ac3db 100644 --- a/core/modules/rdf/lib/Drupal/rdf/SiteSchema/SchemaBase.php +++ b/core/modules/rdf/lib/Drupal/rdf/SiteSchema/SchemaBase.php @@ -8,6 +8,7 @@ namespace Drupal\rdf\SiteSchema; use Drupal\rdf\RdfConstants; +use Drupal\rdf\SiteSchema\SiteSchema; /** * Base class to define an RDF term in a schema. @@ -17,14 +18,14 @@ /** * The schema in which this term is defined. * - * @var \Drupal\rdf\RdfSiteSchema + * @var \Drupal\rdf\SiteSchema\SiteSchema */ protected $siteSchema; /** * Constructor. * - * @param \Drupal\rdf\SiteSchemaNamespace $ns + * @param \Drupal\rdf\SiteSchema\SiteSchema $siteSchema * The namespace. */ public function __construct($siteSchema) { @@ -32,6 +33,17 @@ public function __construct($siteSchema) { } /** + * Get the full graph of terms and properties to display. + * + * When an RDF term URI is dereferenced, it usually contains a description of + * the term in RDF. To make it easier to use this description, include + * information about all related terms. For example, when viewing the RDF + * description for the RDF class which corresponds to a Drupal bundle, data + * about its fields would also be included. + */ + abstract public function getGraph(); + + /** * Get the term properties. * * @return array