You are here

public function NameFormatter::format in Name Field 8

Formats an array of name components.

Parameters

array $components: An array of name components to format.

The expected components are:

  • title (string)
  • given (string)
  • middle (string)
  • family (string)
  • generational (string)
  • credentials (string)

The following tokens are also supported:

  • preferred (string)
  • alternative (string)

string $type: (optional) The name format type to load. If the format does not exist, the 'default' format is used. Defaults to 'default'.

string|null $langcode: (optional) Language code to translate to. NULL (default) means to use the user interface language for the page.

Return value

\Drupal\Component\Render\MarkupInterface A renderable object representing the name.

Overrides NameFormatterInterface::format

1 call to NameFormatter::format()
NameFormatter::formatList in src/NameFormatter.php
Formats a list of author information.

File

src/NameFormatter.php, line 124

Class

NameFormatter
Primary name formatter for an array of name components.

Namespace

Drupal\name

Code

public function format(array $components, $type = 'default', $langcode = NULL) {
  $format_string = $this
    ->getNameFormatString($type);
  $name = $this->parser
    ->parse($components, $format_string, $this->settings);
  if (!empty($components['url'])) {
    $name = new FormattableMarkup('<a href=":link">' . $name . '</a>', [
      ':link' => $components['url']
        ->toString(),
    ]);
  }
  return $name;
}