annotate core/modules/user/src/UserNameItem.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\user;
Chris@0 4
Chris@0 5 use Drupal\Core\Field\FieldDefinitionInterface;
Chris@0 6 use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Defines a custom field item class for the 'name' user entity field.
Chris@0 10 */
Chris@0 11 class UserNameItem extends StringItem {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * {@inheritdoc}
Chris@0 15 */
Chris@0 16 public function isEmpty() {
Chris@0 17 $value = $this->get('value')->getValue();
Chris@0 18
Chris@0 19 // Take into account that the name of the anonymous user is an empty string.
Chris@0 20 if ($this->getEntity()->isAnonymous()) {
Chris@0 21 return $value === NULL;
Chris@0 22 }
Chris@0 23
Chris@0 24 return $value === NULL || $value === '';
Chris@0 25 }
Chris@0 26
Chris@0 27 /**
Chris@0 28 * {@inheritdoc}
Chris@0 29 */
Chris@0 30 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
Chris@0 31 $values = parent::generateSampleValue($field_definition);
Chris@0 32 // User names larger than 60 characters won't pass validation.
Chris@0 33 $values['value'] = substr($values['value'], 0, UserInterface::USERNAME_MAX_LENGTH);
Chris@0 34 return $values;
Chris@0 35 }
Chris@0 36
Chris@0 37 }