UserNameItem.php 944 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace Drupal\user;
  3. use Drupal\Core\Field\FieldDefinitionInterface;
  4. use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
  5. /**
  6. * Defines a custom field item class for the 'name' user entity field.
  7. */
  8. class UserNameItem extends StringItem {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function isEmpty() {
  13. $value = $this->get('value')->getValue();
  14. // Take into account that the name of the anonymous user is an empty string.
  15. if ($this->getEntity()->isAnonymous()) {
  16. return $value === NULL;
  17. }
  18. return $value === NULL || $value === '';
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
  24. $values = parent::generateSampleValue($field_definition);
  25. // User names larger than 60 characters won't pass validation.
  26. $values['value'] = substr($values['value'], 0, UserInterface::USERNAME_MAX_LENGTH);
  27. return $values;
  28. }
  29. }