Problem/Motivation

Datatype callbacks are used to convert a field value to a specific format for machine processing. For example, the date_iso8601 callback transforms a date to its ISO8601 equivalent.

Currently, there is no way to pass other parameters to this callback. For example, you might want to have a schemaorg_interaction_count callback which transforms the data (e.g. 78) to the correctly formatted string ("UserComments:78"). But you wouldn't want to have to create a new function for each of the 9 different kinds of interaction specified on schema.org.

Proposed resolution

Add support for passing additional parameters into the callback.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

linclark’s picture

Here is a first pass. It changes the datatype_callback mapping from this:

    datatype_callback: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'

to this:

    datatype_callback:
      method: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
      arguments:
        interaction_type: 'UserComments'
scor’s picture

Status: Needs review » Needs work
+++ b/core/modules/forum/config/rdf.mapping.node.forum.yml
@@ -16,13 +16,15 @@ fieldMappings:
+    datatype_callback:
+      method: 'date_iso8601'

I understand the key 'method' is used because we're typically expecting a class method here, but date_iso8601() is a function. Should we use the key 'callable' instead?

Could we have this patch rerolled to not include other patches so it can possibly be committed before the other patches? It looks good otherwise, and both Drupal\rdf\SchemaOrgDataConverter::interactionCount and Drupal\rdf\CommonDataConverter::rawValue are tested.

linclark’s picture

Status: Needs work » Needs review
FileSize
14.03 KB

This patch just rerolls it without the dependency on the other issue. I'll switch to 'callable' in the next patch.

linclark’s picture

Ok, this changes method to callable.

scor’s picture

Status: Needs review » Needs work
+++ b/core/modules/rdf/lib/Drupal/rdf/SchemaOrgDataConverter.php
@@ -16,14 +15,19 @@ class SchemaOrgDataConverter {
+   * @param array $args
+   *   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, $args) {

Could '$arguments' be used here instead of '$args' in order to be consistent with the naming used in the YAML file and the rest of the patch? (for example below)

+++ b/core/modules/rdf/rdf.module
@@ -174,8 +178,9 @@ function rdf_rdfa_attributes($mapping, $data = NULL) {
+          $callback = $mapping['datatype_callback']['callable'];
+          $arguments = isset($mapping['datatype_callback']['arguments']) ? $mapping['datatype_callback']['arguments'] : NULL;
+          $attributes['content'] = call_user_func($callback, $data, $arguments);
linclark’s picture

Good catch. $args came from an earlier commit, which is how the inconsistency slipped in, but we should be consistent.

jesse.d’s picture

Status: Needs work » Needs review
FileSize
990 bytes
14.39 KB

Makes the requested change to use $arguments instead of $args in SchemaOrgDataConverter.php.

scor’s picture

Status: Needs review » Reviewed & tested by the community

This looks good with this change.

alexpott’s picture

Title: Make datatype callback handling more flexible » Change notice: Make datatype callback handling more flexible
Priority: Normal » Critical
Status: Reviewed & tested by the community » Active
Issue tags: +Needs change record

Committed e9ca0d8 and pushed to 8.x. Thanks!

mlncn’s picture

Assigned: Unassigned » mlncn

Writing the change notices.

mlncn’s picture

API change notification

Summary

  • Datatype callbacks are used to convert a field value to a specific format for machine processing. We accept arguments to this callback in addition to the value. For instance, instead of hard-coding UserComments UserInteraction into RDF module's Schema.org Data Converter callback, we pass it in as an argument with the key 'interaction_type'.
  • You may use any of the UserInteraction types as the 'interaction_type' argument to the datatype_callback callable method or function 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'.

Configuration example

In Drupal 7 this was defined in hook_rdf_mapping(). In Drupal 8, this goes in the YAML for a content type. For instance, a created at field and a comment count:

Before

/**
 * Implements hook_rdf_mapping().
 */
function node_rdf_mapping() {
  return array(
    array(
// ...
        'created' => array(
          'predicates' => array('dc:date', 'dc:created'),
          'datatype' => 'xsd:dateTime',
          'callback' => 'date_iso8601',
        ),
// ...
        'comment_count' => array(
          'predicates' => array('sioc:num_replies'),
          'datatype' => 'xsd:integer',
        ),
// ...
      ),
    ),
  );
}

After

  created:
    properties:
      - 'schema:dateCreated'
    datatype_callback:
      callable: 'date_iso8601'

and

  comment_count:
    properties:
      - 'schema:interactionCount'
    datatype_callback:
      callable: 'Drupal\rdf\SchemaOrgDataConverter::interactionCount'
      arguments:
        interaction_type: 'UserComments'
catch’s picture

linclark’s picture

@mlncn, thanks for the change notice text. Did this ever get input as a draft change notice?

linclark’s picture

Issue summary: View changes

clarifies that the interactions are specified on schema.org.

mlncn’s picture

Issue summary: View changes
Status: Active » Needs review

Added a change notice: https://drupal.org/node/2159855

First time doing one of those, please review!

star-szr’s picture

Title: Change notice: Make datatype callback handling more flexible » Make datatype callback handling more flexible
Assigned: mlncn » Unassigned
Priority: Major » Normal
Issue tags: -Needs change record

Further edits can be made if necessary but that looks good to me @mlncn, thanks! Closing out this issue.

star-szr’s picture

Status: Needs review » Fixed

Actually marking as fixed this time :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.