diff --git a/core/modules/rdf/lib/Drupal/rdf/RdfMappingManager.php b/core/modules/rdf/lib/Drupal/rdf/RdfMappingManager.php index 2f4f637..2fc262a 100644 --- a/core/modules/rdf/lib/Drupal/rdf/RdfMappingManager.php +++ b/core/modules/rdf/lib/Drupal/rdf/RdfMappingManager.php @@ -101,6 +101,55 @@ public function getFieldMapping($entity_type, $bundle, $field_name, $schema_path return $this->mapFieldForOutput($term_schema); } + public function saveBundleMappingConfig($mapping, $entity_type, $bundle) { + $keys = array( + 'mappedEntityType' => $entity_type, + 'mappedBundle' => $bundle, + ); + $bundle_mapping = entity_create('rdf_mapping_bundle', $keys); + if (isset($mapping['types'])) { + $bundle_mapping->types = $mapping['types']; + } + $bundle_mapping->save(); + } + + public function saveFieldMappingConfig($mapping, $entity_type, $bundle, $field_name) { + $keys = array( + 'mappedEntityType' => $entity_type, + 'mappedBundle' => $bundle, + 'mappedField' => $field_name, + ); + $field_mapping = entity_create('rdf_mapping_field', $keys); + if (isset($mapping['properties'])) { + $field_mapping->properties = $mapping['properties']; + } + if (isset($mapping['datatype'])) { + $field_mapping->datatype = $mapping['datatype']; + } + if (isset($mapping['datatype_callback'])) { + $field_mapping->datatypeCallback = $mapping['datatype_callback']; + } + $field_mapping->save(); + } + + public function deleteBundleMappingConfig($entity_type, $bundle) { + // Config can only be stored for the syndication schema. + $schema = $this->siteSchemaManager->getSchema(SiteSchema::SYNDICATION); + $bundle_schema = $schema->bundle($entity_type, $bundle); + $config_id = $bundle_schema->getMappingConfigId(); + $config = config("rdf.mapping.bundle.$config_id"); + $config->delete(); + } + + public function deleteFieldMappingConfig($entity_type, $bundle, $field_name) { + // Config can only be stored for the syndication schema. + $schema = $this->siteSchemaManager->getSchema(SiteSchema::SYNDICATION); + $field_schema = $schema->field($entity_type, $bundle, $field_name); + $config_id = $field_schema->getMappingConfigId(); + $config = config("rdf.mapping.field.$config_id"); + $config->delete(); + } + /** * Map an array of incoming URIs to an internal site schema URI. * @@ -126,9 +175,14 @@ protected function mapTypesFromInput($input_rdf_types) { } protected function mapBundleForOutput($term_schema) { + $mapping = array(); $typeMap = new MapBundleForOutputEvent($term_schema); $this->dispatcher->dispatch(RdfMappingEvents::MAP_BUNDLE_FOR_OUTPUT, $typeMap); - return $typeMap->getCuries(); + $types = $typeMap->getCuries(); + if (!empty($types)) { + $mapping['types'] = $types; + } + return $mapping; } protected function mapFieldForOutput($term_schema) { @@ -138,9 +192,9 @@ protected function mapFieldForOutput($term_schema) { $predicates = $map_event->getPredicates(); if (!empty($predicates)) { - $mapping['predicates'] = $predicates; + $mapping['properties'] = $predicates; $mapping['datatype'] = $map_event->getDatatype(); - $mapping['callback'] = $map_event->getDatatypeCallback(); + $mapping['datatype_callback'] = $map_event->getDatatypeCallback(); } return array_filter($mapping); } diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/CrudTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/CrudTest.php index 42b4d57..ee256e2 100644 --- a/core/modules/rdf/lib/Drupal/rdf/Tests/CrudTest.php +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/CrudTest.php @@ -33,39 +33,55 @@ public static function getInfo() { * Tests inserting, loading, updating, and deleting RDF mappings. */ function testCRUD() { - // Verify saving a mapping. - $mapping = array( - 'type' => 'entity_test', - 'bundle' => 'entity_test', - 'mapping' => array( - 'rdftype' => array('sioc:Post'), - 'title' => array( - 'predicates' => array('dc:title'), - ), - 'user_id' => array( - 'predicates' => array('sioc:has_creator', 'dc:creator'), - ), - ), + $entity_type = $bundle = 'entity_test'; + $bundle_mapping_config_name = "rdf.mapping.bundle.site-schema:syndication:$entity_type:$bundle"; + $field_mapping_config_name = "rdf.mapping.field.site-schema:syndication:$entity_type:$bundle:user_id"; + + // Create mapping arrays to save. + $bundle_mapping = array( + 'types' => array('sioc:Post'), + ); + $user_id_mapping = array( + 'properties' => array('sioc:has_creator', 'dc:creator'), ); - rdf_mapping_save($mapping); - $types = config('rdf.mapping.bundle.site-schema:syndication:entity_test:entity_test')->get('types'); - $this->assert(!empty($types), 'Bundle mapping config saved.'); - $field_properties = config('rdf.mapping.field.site-schema:syndication:entity_test:entity_test:user_id')->get('properties'); - $this->assert(!empty($field_properties), 'Field mapping config saved.'); - // Verify loading of saved mapping. - $loaded_mapping = rdf_mapping_load($mapping['type'], $mapping['bundle']); - $this->assert(in_array('sioc:has_creator', $loaded_mapping['user_id']['predicates']), 'Saved mapping loaded successfully.'); + // Save mappings. + $mapping_manager = drupal_container()->get('rdf.mapping_manager'); + $mapping_manager->saveBundleMappingConfig($bundle_mapping, $entity_type, $bundle); + $mapping_manager->saveFieldMappingConfig($user_id_mapping, $entity_type, $bundle, 'user_id'); + + // Test that config files were saved. + $bundle_mapping_configs = config_get_storage_names_with_prefix('rdf.mapping.bundle'); + $this->assertTrue(in_array($bundle_mapping_config_name, $bundle_mapping_configs), 'Bundle mapping config saved.'); + $field_mapping_configs = config_get_storage_names_with_prefix('rdf.mapping.field'); + $this->assertTrue(in_array($field_mapping_config_name, $field_mapping_configs), 'Field mapping config saved.'); + + // Test that config was property saved. + $types = config($bundle_mapping_config_name)->get('types'); + $this->assertTrue(in_array('sioc:Post', $types), 'Bundle mapping config values saved correctly.'); + $properties = config($field_mapping_config_name)->get('properties'); + $this->assertTrue(in_array('sioc:has_creator', $properties) && in_array('dc:creator', $properties), 'Field mapping config values saved correctly.'); + + // Test loading of saved mapping. + $loaded_mapping = $mapping_manager->getBundleMapping($entity_type, $bundle); + $this->assertTrue(in_array('sioc:Post', $loaded_mapping['types']), 'Bundle mapping loaded.'); + $loaded_mapping = $mapping_manager->getFieldMapping($entity_type, $bundle, 'user_id'); + $this->assertTrue(in_array('sioc:has_creator', $loaded_mapping['properties']) && in_array('dc:creator', $loaded_mapping['properties']), 'Field mapping config loaded.'); - // Verify updating of mapping. - $mapping['mapping']['user_id'] = array( - 'predicates' => array('dc2:bar2'), + // Test that mapping can be updated. + $user_id_mapping = array( + 'properties' => array('schema:author'), ); - rdf_mapping_save($mapping); - $loaded_mapping = rdf_mapping_load($mapping['type'], $mapping['bundle']); - $this->assert(in_array('dc2:bar2', $loaded_mapping['user_id']['predicates']), 'Mapping was updated.'); + $mapping_manager->saveFieldMappingConfig($user_id_mapping, $entity_type, $bundle, 'user_id'); + $loaded_mapping = $mapping_manager->getFieldMapping($entity_type, $bundle, 'user_id'); + $this->assert(in_array('schema:author', $loaded_mapping['properties']) && !in_array('sioc:has_creator', $loaded_mapping['properties']), 'Mapping was updated.'); - // Verify deleting of mapping. - $this->assertTrue(rdf_mapping_delete($mapping['type'], $mapping['bundle']), 'Mapping was deleted.'); + // Test that the mapping can be deleted. + $mapping_manager->deleteBundleMappingConfig($entity_type, $bundle); + $bundle_mapping_configs = config_get_storage_names_with_prefix('rdf.mapping.bundle'); + $this->assertFalse(in_array($bundle_mapping_config_name, $bundle_mapping_configs), 'Bundle mapping config deleted.'); + $mapping_manager->deleteFieldMappingConfig($entity_type, $bundle, 'user_id'); + $field_mapping_configs = config_get_storage_names_with_prefix('rdf.mapping.field'); + $this->assertFalse(in_array($field_mapping_config_name, $field_mapping_configs), 'Field mapping config deleted.'); } } diff --git a/core/modules/rdf/rdf.install b/core/modules/rdf/rdf.install index 10d3f8d..be2cde1 100644 --- a/core/modules/rdf/rdf.install +++ b/core/modules/rdf/rdf.install @@ -4,46 +4,3 @@ * @file * Install, update and uninstall functions for the rdf module. */ - -/** - * Implements hook_schema(). - */ -function rdf_schema() { - $schema['rdf_mapping'] = array( - 'description' => 'Stores custom RDF mappings for user defined content types or overriden module-defined mappings', - 'fields' => array( - 'type' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'description' => 'The name of the entity type a mapping applies to (node, user, comment, etc.).', - ), - 'bundle' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'description' => 'The name of the bundle a mapping applies to.', - ), - 'mapping' => array( - 'description' => 'The serialized mapping of the bundle type and fields to RDF terms.', - 'type' => 'blob', - 'not null' => FALSE, - 'size' => 'big', - 'serialize' => TRUE, - ), - ), - 'primary key' => array('type', 'bundle'), - ); - - return $schema; -} - -/** - * Implements hook_install(). - */ -function rdf_install() { - // Collect any RDF mappings that were declared by modules installed before - // this one. - $modules = module_implements('rdf_mapping'); - rdf_modules_installed($modules); -} diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 2f0f1cc..2cf869c 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -163,69 +163,6 @@ function rdf_mapping_load($type, $bundle) { */ /** - * Saves an RDF mapping to the database. - * - * Takes a mapping structure returned by hook_rdf_mapping() implementations - * and creates or updates a record mapping for each encountered entity - * type/bundle pair. If available, adds default values for non-existent mapping - * keys. - * - * @param $mapping - * The RDF mapping to save. - * - * @return - * MergeQuery object that indicates the outcome of the operation. - */ -function rdf_mapping_save($mapping) { - $keys = array( - 'mappedEntityType' => $mapping['type'], - 'mappedBundle' => $mapping['bundle'], - ); - - // Add bundle mapping. - $bundle_mapping = entity_create('rdf_mapping_bundle', $keys); - if (isset($mapping['mapping']['rdftype'])) { - $bundle_mapping->types = $mapping['mapping']['rdftype']; - } - $bundle_mapping->save(); - - // Add field mapping. - unset($mapping['mapping']['rdftype']); - foreach ($mapping['mapping'] as $field_name => $field) { - $field_mapping = entity_create('rdf_mapping_field', $keys); - $field_mapping->mappedField = $field_name; - if (isset($field['predicates'])) { - $field_mapping->properties = $field['predicates']; - } - if (isset($field['datatype'])) { - $field_mapping->datatype = $field['datatype']; - } - if (isset($field['callback'])) { - $field_mapping->datatypeCallback = $field['callback']; - } - $field_mapping->save(); - } - - // @todo Remove this once mappings are no longer part of entity info. - entity_info_cache_clear(); -} - -/** - * Deletes the mapping config for the given bundle. - * - * @param $type - * The entity type the mapping refers to. - * @param $bundle - * The bundle the mapping refers to. - * - * @return - * TRUE if the mapping is deleted, FALSE if not. - */ -function rdf_mapping_delete($type, $bundle) { - // @todo Implement this. -} - -/** * Builds an array of RDFa attributes for a given mapping. * * This array will typically be passed through Drupal\Core\Template\Attribute @@ -287,29 +224,6 @@ function rdf_rdfa_attributes($mapping, $data = NULL) { */ /** - * Implements hook_modules_installed(). - * - * Checks if the installed modules have any RDF mapping definitions to declare - * and stores them in the rdf_mapping table. - * - * While both default entity mappings and specific bundle mappings can be - * defined in hook_rdf_mapping(), default entity mappings are not stored in the - * database. Only overridden mappings are stored in the database. The default - * entity mappings can be overriden by specific bundle mappings which are - * stored in the database and can be altered via the RDF CRUD mapping API. - */ -function rdf_modules_installed($modules) { - foreach ($modules as $module) { - $function = $module . '_rdf_mapping'; - if (function_exists($function)) { - foreach ($function() as $mapping) { - rdf_mapping_save($mapping); - } - } - } -} - -/** * Implements hook_theme(). */ function rdf_theme() {