Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 39 |
| UserNameFormatter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 39 |
| defaultSettings | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
| settingsForm | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 8 |
|||
| viewElements | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 25 |
|||
| isApplicable | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 2 |
|||
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\user\Plugin\Field\FieldFormatter\UserNameFormatter. | |
| */ | |
| namespace Drupal\user\Plugin\Field\FieldFormatter; | |
| use Drupal\Core\Field\FieldDefinitionInterface; | |
| use Drupal\Core\Field\FieldItemListInterface; | |
| use Drupal\Core\Field\FormatterBase; | |
| use Drupal\Core\Form\FormStateInterface; | |
| /** | |
| * Plugin implementation of the 'user_name' formatter. | |
| * | |
| * @FieldFormatter( | |
| * id = "user_name", | |
| * label = @Translation("User name"), | |
| * description = @Translation("Display the user or author name."), | |
| * field_types = { | |
| * "string" | |
| * } | |
| * ) | |
| */ | |
| class UserNameFormatter extends FormatterBase { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function defaultSettings() { | |
| $options = parent::defaultSettings(); | |
| $options['link_to_entity'] = TRUE; | |
| return $options; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function settingsForm(array $form, FormStateInterface $form_state) { | |
| $form = parent::settingsForm($form, $form_state); | |
| $form['link_to_entity'] = [ | |
| '#type' => 'checkbox', | |
| '#title' => $this->t('Link to the user'), | |
| '#default_value' => $this->getSetting('link_to_entity'), | |
| ]; | |
| return $form; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function viewElements(FieldItemListInterface $items, $langcode) { | |
| $elements = []; | |
| foreach ($items as $delta => $item) { | |
| /** @var $user \Drupal\user\UserInterface */ | |
| if ($user = $item->getEntity()) { | |
| if ($this->getSetting('link_to_entity')) { | |
| $elements[$delta] = [ | |
| '#theme' => 'username', | |
| '#account' => $user, | |
| '#link_options' => ['attributes' => ['rel' => 'user']], | |
| '#cache' => [ | |
| 'tags' => $user->getCacheTags(), | |
| ], | |
| ]; | |
| } | |
| else { | |
| $elements[$delta] = [ | |
| '#markup' => $user->getDisplayName(), | |
| '#cache' => [ | |
| 'tags' => $user->getCacheTags(), | |
| ], | |
| ]; | |
| } | |
| } | |
| } | |
| return $elements; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function isApplicable(FieldDefinitionInterface $field_definition) { | |
| return $field_definition->getTargetEntityTypeId() === 'user' && $field_definition->getName() === 'name'; | |
| } | |
| } |