Commit e9ca0d86 authored by alexpott's avatar alexpott

Issue #2020001 by linclark, jesse.d: Make datatype callback handling more flexible.

parent 36d647e6
......@@ -25,4 +25,7 @@ fieldMappings:
comment_count:
properties:
- 'schema:interactionCount'
datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
datatype_callback:
callable: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
arguments:
interaction_type: 'UserComments'
......@@ -6,7 +6,6 @@
namespace Drupal\rdf;
class SchemaOrgDataConverter {
/**
......@@ -16,14 +15,19 @@ class SchemaOrgDataConverter {
*
* @param int $count
* The interaction count.
* @param array $arguments
* An array of arguments defined in the mapping.
* Expected keys are:
* - interaction_type: The string to use for the type of interaction
* (e.g. UserComments).
*
* @return string
* The formatted string.
*
* @see http://schema.org/UserInteraction
* @todo Support other interaction types, see https://drupal.org/node/2020001
*/
static function interactionCount($count) {
return "UserComment:$count";
static function interactionCount($count, $arguments) {
$interaction_type = $arguments['interaction_type'];
return "$interaction_type:$count";
}
}
......@@ -54,7 +54,7 @@ public function setUp() {
$comment_count_mapping = array(
'properties' => array('sioc:num_replies'),
'datatype' => 'xsd:integer',
'datatype_callback' => 'Drupal\rdf\CommonDataConverter::rawValue',
'datatype_callback' => array('callable' => 'Drupal\rdf\CommonDataConverter::rawValue'),
);
$article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save();
......@@ -76,12 +76,12 @@ public function setUp() {
'created' => array(
'properties' => array('dc:date', 'dc:created'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
),
'changed' => array(
'properties' => array('dc:modified'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
),
'comment_body' => array(
'properties' => array('content:encoded'),
......
......@@ -81,7 +81,7 @@ function testFieldMapping() {
$mapping = array(
'properties' => array('dc:created'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
);
rdf_get_mapping($this->entity_type, $this->bundle)
->setFieldMapping($field_name, $mapping)
......@@ -94,7 +94,7 @@ function testFieldMapping() {
$mapping = array(
'properties' => array('dc:date'),
'datatype' => 'foo:bar',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
);
rdf_get_mapping($this->entity_type, $this->bundle)
->setFieldMapping($field_name, $mapping)
......
......@@ -42,7 +42,7 @@ public function setUp() {
->setFieldMapping('created', array(
'properties' => array('dc:date', 'dc:created'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
))
->save();
}
......
......@@ -69,7 +69,7 @@ protected function _testUnalteredMappingUpgrade() {
$expected_mapping = array(
'properties' => array('dc:date', 'dc:created'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
);
$this->assertEqual($mapping, $expected_mapping, 'Unaltered field mapping with datatype and datatype callback upgraded correctly.');
......
......@@ -73,7 +73,7 @@ function testDatatypeCallback() {
$mapping = array(
'datatype' => $datatype,
'properties' => $properties,
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
);
$expected_attributes = array(
'datatype' => $datatype,
......@@ -92,11 +92,14 @@ function testDatatypeCallbackWithConverter() {
$properties = array('schema:interactionCount');
$data = "23";
$content = "UserComment:23";
$content = "UserComments:23";
$mapping = array(
'properties' => $properties,
'datatype_callback' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
'datatype_callback' => array(
'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
'arguments' => array('interaction_type' => 'UserComments'),
),
);
$expected_attributes = array(
'property' => $properties,
......
......@@ -220,7 +220,7 @@ protected function doFrontPageRdfaTests() {
// Test interaction count.
$expected_value = array(
'type' => 'literal',
'value' => 'UserComment:1',
'value' => 'UserComments:1',
'lang' => 'en',
);
$this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/interactionCount', $expected_value), "Teaser comment count was found (schema:interactionCount).");
......
......@@ -47,12 +47,12 @@ function setUp() {
'created' => array(
'properties' => array('dc:date', 'dc:created'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
),
'changed' => array(
'properties' => array('dc:modified'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
),
'body' => array(
'properties' => array('content:encoded'),
......@@ -71,7 +71,7 @@ function setUp() {
'last_activity' => array(
'properties' => array('sioc:last_activity_date'),
'datatype' => 'xsd:dateTime',
'datatype_callback' => 'date_iso8601',
'datatype_callback' => array('callable' => 'date_iso8601'),
),
);
// Iterate over field mappings and save.
......
......@@ -49,7 +49,7 @@ function rdf_update_8000() {
}
!empty($value['predicates']) ? $field_mappings[$key]['properties'] = $value['predicates'] : NULL;
!empty($value['datatype']) ? $field_mappings[$key]['datatype'] = $value['datatype'] : NULL;
!empty($value['callback']) ? $field_mappings[$key]['datatype_callback'] = $value['callback'] : NULL;
!empty($value['callback']) ? $field_mappings[$key]['datatype_callback']['callable'] = $value['callback'] : NULL;
}
}
$config->set('fieldMappings', $field_mappings);
......
......@@ -141,9 +141,13 @@ function rdf_get_namespaces() {
* 'datatype_callback' and 'type' keys. For example:
* @code
* array(
* 'properties' => array('dc:created'),
* 'datatype' => 'xsd:dateTime',
* 'datatype_callback' => 'date_iso8601',
* 'properties' => array('schema:interactionCount'),
* 'datatype' => 'xsd:integer',
* 'datatype_callback' => array(
* 'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
* 'arguments' => array(
* 'interaction_type' => 'UserComments'
* ),
* ),
* );
* @endcode
......@@ -174,8 +178,9 @@ function rdf_rdfa_attributes($mapping, $data = NULL) {
$attributes['property'] = $mapping['properties'];
// Convert $data to a specific format as per the callback function.
if (isset($data) && !empty($mapping['datatype_callback'])) {
$callback = $mapping['datatype_callback'];
$attributes['content'] = call_user_func($callback, $data);
$callback = $mapping['datatype_callback']['callable'];
$arguments = isset($mapping['datatype_callback']['arguments']) ? $mapping['datatype_callback']['arguments'] : NULL;
$attributes['content'] = call_user_func($callback, $data, $arguments);
}
if (isset($mapping['datatype'])) {
$attributes['datatype'] = $mapping['datatype'];
......
......@@ -10,11 +10,13 @@ fieldMappings:
created:
properties:
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
changed:
properties:
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
comment_body:
properties:
- 'schema:text'
......
......@@ -10,11 +10,13 @@ fieldMappings:
created:
properties:
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
changed:
properties:
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
comment_body:
properties:
- 'schema:text'
......
......@@ -10,11 +10,13 @@ fieldMappings:
created:
properties:
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
changed:
properties:
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
body:
properties:
- 'schema:text'
......@@ -25,7 +27,10 @@ fieldMappings:
comment_count:
properties:
- 'schema:interactionCount'
datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
datatype_callback:
callable: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
arguments:
interaction_type: 'UserComments'
field_image:
properties:
- 'schema:image'
......
......@@ -10,11 +10,13 @@ fieldMappings:
created:
properties:
- 'schema:dateCreated'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
changed:
properties:
- 'schema:dateModified'
datatype_callback: 'date_iso8601'
datatype_callback:
callable: 'date_iso8601'
body:
properties:
- 'schema:text'
......@@ -25,4 +27,7 @@ fieldMappings:
comment_count:
properties:
- 'schema:interactionCount'
datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
datatype_callback:
callable: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
arguments:
interaction_type: 'UserComments'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment