View source
<?php
namespace Drupal\name;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\name\Render\NameListFormattableMarkup;
class NameFormatter implements NameFormatterInterface {
use StringTranslationTrait;
protected $parser;
protected $nameFormatStorage;
protected $listFormatStorage;
protected $languageManager;
protected $configFactory;
protected $settings = [
'sep1' => ' ',
'sep2' => ', ',
'sep3' => '',
'markup' => 'none',
];
public function __construct(EntityTypeManagerInterface $entityTypeManager, NameFormatParser $parser, LanguageManagerInterface $language_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory) {
$this->nameFormatStorage = $entityTypeManager
->getStorage('name_format');
$this->listFormatStorage = $entityTypeManager
->getStorage('name_list_format');
$this->parser = $parser;
$this->languageManager = $language_manager;
$this->stringTranslation = $translation;
$this->configFactory = $config_factory;
$config = $this->configFactory
->get('name.settings');
$this->settings['sep1'] = $config
->get('sep1');
$this->settings['sep2'] = $config
->get('sep2');
$this->settings['sep3'] = $config
->get('sep3');
}
public function setSetting($key, $value) {
$this->settings[$key] = $value;
return $this;
}
public function getSetting($key) {
return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
}
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;
}
public function formatList(array $items, $type = 'default', $list_type = 'default', $langcode = NULL) {
$name_count = count($items);
if (!$name_count) {
return '';
}
if ($name_count == 1) {
$item = reset($items);
return $this
->format($item, $type, $langcode);
}
$settings = $this
->getListSettings($list_type);
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',
]);
}
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);
}
}
}
protected function getNameFormatString($format) {
$config = $this->nameFormatStorage
->load($format);
if (!$config) {
$config = $this->nameFormatStorage
->load('default');
}
return $config
->get('pattern');
}
protected function getListSettings($format) {
$listFormat = $this->listFormatStorage
->load($format);
if (!$format) {
$listFormat = $this->listFormatStorage
->load('default');
}
return $listFormat
->listSettings();
}
public function getLastDelimitorTypes($include_examples = TRUE) {
if ($include_examples) {
return [
'text' => $this
->t('Textual (and)'),
'symbol' => $this
->t('Ampersand (&)'),
'inherit' => $this
->t('Inherit delimiter'),
];
}
else {
return [
'text' => $this
->t('Textual'),
'symbol' => $this
->t('Ampersand'),
'inherit' => $this
->t('Inherit delimiter'),
];
}
}
public function getLastDelimitorBehaviors($include_examples = TRUE) {
if ($include_examples) {
return [
'never' => $this
->t('Never (i.e. "J. Doe and T. Williams")'),
'always' => $this
->t('Always (i.e. "J. Doe<strong>,</strong> and T. Williams")'),
'contextual' => $this
->t('Contextual (i.e. "J. Doe and T. Williams" <em>or</em> "J. Doe, S. Smith<strong>,</strong> and T. Williams")'),
];
}
else {
return [
'never' => $this
->t('Never'),
'always' => $this
->t('Always'),
'contextual' => $this
->t('Contextual'),
];
}
}
}