5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
9 * Defines a custom field item class for the 'name' user entity field.
11 class UserNameItem extends StringItem {
16 public function isEmpty() {
17 $value = $this->get('value')->getValue();
19 // Take into account that the name of the anonymous user is an empty string.
20 if ($this->getEntity()->isAnonymous()) {
21 return $value === NULL;
24 return $value === NULL || $value === '';
30 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
31 $values = parent::generateSampleValue($field_definition);
32 // User names larger than 60 characters won't pass validation.
33 $values['value'] = substr($values['value'], 0, UserInterface::USERNAME_MAX_LENGTH);