You are here

function rdf_preprocess_user in Drupal 10

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

Implements hook_preprocess_HOOK() for user templates.

File

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

Code

function rdf_preprocess_user(&$variables) {

  /** @var \Drupal\user\UserInterface $account */
  $account = $variables['elements']['#user'];
  $uri = $account
    ->toUrl();
  $mapping = rdf_get_mapping('user', 'user');
  $bundle_mapping = $mapping
    ->getPreparedBundleMapping();

  // Adds RDFa markup to the user profile page. Fields displayed in this page
  // will automatically describe the user.
  if (!empty($bundle_mapping['types'])) {
    $variables['attributes']['typeof'] = $bundle_mapping['types'];
    $variables['attributes']['about'] = $account
      ->toUrl()
      ->toString();
  }

  // If we are on the user account page, add the relationship between the
  // sioc:UserAccount and the foaf:Person who holds the account.
  if (\Drupal::routeMatch()
    ->getRouteName() == $uri
    ->getRouteName()) {

    // Adds the markup for username as language neutral literal, see
    // rdf_preprocess_username().
    $name_mapping = $mapping
      ->getPreparedFieldMapping('name');
    if (!empty($name_mapping['properties'])) {
      $username_meta = [
        '#tag' => 'meta',
        '#attributes' => [
          'about' => $account
            ->toUrl()
            ->toString(),
          'property' => $name_mapping['properties'],
          'content' => $account
            ->getDisplayName(),
          'lang' => '',
        ],
      ];
      $variables['#attached']['html_head'][] = [
        $username_meta,
        'rdf_user_username',
      ];
    }
  }
}