You are here

function rdf_preprocess_username in Drupal 8

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

Implements hook_preprocess_HOOK() for username.html.twig.

File

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

Code

function rdf_preprocess_username(&$variables) {

  // Because lang is set on the HTML element that wraps the page, the
  // username inherits this language attribute. However, since the username
  // might not be transliterated to the same language that the content is in,
  // we do not want it to inherit the language attribute, so we set the
  // attribute to an empty string.
  if (empty($variables['attributes']['lang'])) {
    $variables['attributes']['lang'] = '';
  }

  // The profile URI is used to identify the user account. The about attribute
  // is used to set the URI as the default subject of the properties embedded
  // as RDFa in the child elements. Even if the user profile is not accessible
  // to the current user, we use its URI in order to identify the user in RDF.
  // We do not use this attribute for the anonymous user because we do not have
  // a user profile URI for it (only a homepage which cannot be used as user
  // profile in RDF.)
  if ($variables['uid'] > 0) {
    $variables['attributes']['about'] = Url::fromRoute('entity.user.canonical', [
      'user' => $variables['uid'],
    ])
      ->toString();
  }

  // Add RDF type of user.
  $mapping = rdf_get_mapping('user', 'user');
  $bundle_mapping = $mapping
    ->getPreparedBundleMapping();
  if (!empty($bundle_mapping['types'])) {
    $variables['attributes']['typeof'] = $bundle_mapping['types'];
  }

  // Annotate the username in RDFa. A property attribute is used with an empty
  // datatype attribute to ensure the username is parsed as a plain literal
  // in RDFa 1.0 and 1.1.
  $name_mapping = $mapping
    ->getPreparedFieldMapping('name');
  if (!empty($name_mapping)) {
    $variables['attributes']['property'] = $name_mapping['properties'];
    $variables['attributes']['datatype'] = '';
  }

  // Add the homepage RDFa markup if present.
  $homepage_mapping = $mapping
    ->getPreparedFieldMapping('homepage');
  if (!empty($variables['homepage']) && !empty($homepage_mapping)) {
    $variables['attributes']['rel'] = $homepage_mapping['properties'];
  }

  // Long usernames are truncated by template_preprocess_username(). Store the
  // full name in the content attribute so it can be extracted in RDFa.
  if ($variables['truncated']) {
    $variables['attributes']['content'] = $variables['name_raw'];
  }
}