'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'; } }