You are here

function rdf_rdfa_attributes in Drupal 8

Same name and namespace in other branches
  1. 7 modules/rdf/rdf.module \rdf_rdfa_attributes()
  2. 9 core/modules/rdf/rdf.module \rdf_rdfa_attributes()

Builds an array of RDFa attributes for a given mapping.

This array will typically be passed through Drupal\Core\Template\Attribute to create the attributes variables that are available to template files. These include $attributes, $title_attributes, $content_attributes and the field-specific $item_attributes variables.

Parameters

array $mapping: An array containing a mandatory 'properties' key and optional 'datatype', 'datatype_callback' and 'type' keys. For example:

array(
  'properties' => array(
    'schema:interactionCount',
  ),
  'datatype' => 'xsd:integer',
  'datatype_callback' => array(
    'callable' => 'Drupal\\rdf\\SchemaOrgDataConverter::interactionCount',
    'arguments' => array(
      'interaction_type' => 'UserComments',
    ),
  ),
);

mixed $data: (optional) A value that needs to be converted by the provided callback function.

Return value

array RDFa attributes suitable for Drupal\Core\Template\Attribute.

Related topics

4 calls to rdf_rdfa_attributes()
RdfaAttributesTest::_testAttributes in core/modules/rdf/tests/src/Kernel/RdfaAttributesTest.php
Helper function to test attribute generation.
rdf_comment_storage_load in core/modules/rdf/rdf.module
Implements hook_ENTITY_TYPE_storage_load() for comment entities.
rdf_entity_prepare_view in core/modules/rdf/rdf.module
Implements hook_entity_prepare_view().
rdf_preprocess_node in core/modules/rdf/rdf.module
Implements hook_preprocess_HOOK() for node templates.

File

core/modules/rdf/rdf.module, line 169
Enables semantically enriched output for Drupal sites in the form of RDFa.

Code

function rdf_rdfa_attributes($mapping, $data = NULL) {
  $attributes = [];

  // The type of mapping defaults to 'property'.
  $type = isset($mapping['mapping_type']) ? $mapping['mapping_type'] : 'property';
  switch ($type) {

    // The mapping expresses the relationship between two resources.
    case 'rel':
    case 'rev':
      $attributes[$type] = $mapping['properties'];
      break;

    // The mapping expresses the relationship between a resource and some
    // literal text.
    case 'property':
      if (!empty($mapping['properties'])) {
        $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']['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'];
        }
        break;
      }
  }
  return $attributes;
}