You are here

protected function NameFormatParser::renderComponent in Name Field 8

Renders a name component value.

This function does not by default sanitize the output unless the markup flag is set. If set, it runs the component through Html::escape() and wraps the component in a span with the component name set as the class.

Parameters

string $value: A value to render.

string $component_key: The componenet context.

string $modifier: Internal flag for processing.

Return value

string The rendered componenet.

2 calls to NameFormatParser::renderComponent()
NameFormatParser::generateTokens in src/NameFormatParser.php
Generates the tokens from the name item.
NameFormatParser::renderFirstComponent in src/NameFormatParser.php
Finds and renders the first renderable name component value.

File

src/NameFormatParser.php, line 480

Class

NameFormatParser
Converts a name from an array of components into a defined format.

Namespace

Drupal\name

Code

protected function renderComponent($value, $component_key, $modifier = NULL) {
  if (empty($value) || !mb_strlen($value)) {
    return NULL;
  }
  switch ($modifier) {

    // First letter first word.
    case 'initial':
      $value = mb_substr($value, 0, 1);
      break;

    // First letter all words.
    case 'initials':

      // First letter all words.
      $value = NameUnicodeExtras::initials($value);
      break;
  }

  // Based on http://schema.org/Person that doesn't cover generational suffix
  // or preferred names directly.
  $map = [
    'title' => 'honorificPrefix',
    'given' => 'givenName',
    'middle' => 'additionalName',
    'family' => 'familyName',
    'credential' => 'honorificSuffix',
    'alternative' => 'alternateName',
  ];
  switch ($this->markup) {
    case 'simple':
      return '<span class="' . Html::escape($component_key) . '">' . Html::escape($value) . '</span>';
    case 'microdata':
      return '<span class="' . Html::escape($component_key) . '"' . (isset($map[$component_key]) ? ' itemprop="' . $map[$component_key] . '"' : '') . '>' . Html::escape($value) . '</span>';
    case 'rdfa':
      return '<span class="' . Html::escape($component_key) . '"' . (isset($map[$component_key]) ? ' property="schema:' . $map[$component_key] . '"' : '') . '>' . Html::escape($value) . '</span>';
    case 'none':
    default:
      return $value;
  }
}