public function NameFormatter::formatList in Name Field 8
Formats a list of author information.
Parameters
array $items: A nested array of name components to format.
string $type: (optional) The name format type to load. If the format does not exist, the 'default' format is used. Defaults to 'default'.
string $list_type: (optional) The list 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 The processed name in a MarkupInterface object.
Overrides NameFormatterInterface::formatList
File
- src/
NameFormatter.php, line 139
Class
- NameFormatter
- Primary name formatter for an array of name components.
Namespace
Drupal\nameCode
public function formatList(array $items, $type = 'default', $list_type = 'default', $langcode = NULL) {
$name_count = count($items);
// Avoid any computations if none or one names only.
if (!$name_count) {
return '';
}
if ($name_count == 1) {
$item = reset($items);
return $this
->format($item, $type, $langcode);
}
$settings = $this
->getListSettings($list_type);
// Removed names that don't need to be formatted.
if ($settings['el_al_min'] && $name_count > $settings['el_al_min']) {
$items = array_slice($items, 0, $settings['el_al_first']);
}
$names = [];
foreach ($items as $item) {
$names[] = $this
->format($item, $type, $langcode);
}
if ($name_count > $settings['el_al_min']) {
$etal = $this
->t('et al', [], [
'context' => 'name',
]);
if ($this->settings['markup'] !== 'none') {
$etal = new FormattableMarkup('<em>@etal</em>', [
'@etal' => $etal,
]);
}
if (count($names) == 1) {
return $this
->t('@name@delimiter @etal', [
'@name' => reset($names),
'@delimiter' => trim($settings['delimiter']),
'@etal' => $etal,
]);
}
else {
$names = new NameListFormattableMarkup($names, $settings['delimiter']);
return $this
->t('@names@delimiter @etal', [
'@names' => $names,
'@delimiter' => trim($settings['delimiter']),
'@etal' => $etal,
]);
}
}
else {
if ($settings['and'] == 'inherit') {
return new NameListFormattableMarkup($names, $settings['delimiter']);
}
$t_args = [
'@lastname' => array_pop($names),
'@names' => new NameListFormattableMarkup($names, $settings['delimiter']),
'@delimiter' => trim($settings['delimiter']),
];
if ($settings['and'] == 'text') {
$t_args['@and'] = $this
->t('and', [], [
'context' => 'name',
]);
}
else {
$t_args['@and'] = $this
->t('&', [], [
'context' => 'name',
]);
}
// Strange rule from citationstyles.org.
// @see http://citationstyles.org/downloads/specification.html
if ($settings['delimiter_precedes_last'] == 'contextual' && $name_count > 2 || $settings['delimiter_precedes_last'] == 'always') {
return $this
->t('@names@delimiter @and @lastname', $t_args);
}
else {
return $this
->t('@names @and @lastname', $t_args);
}
}
}