Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / user / src / UserNameItem.php
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
7
8 /**
9  * Defines a custom field item class for the 'name' user entity field.
10  */
11 class UserNameItem extends StringItem {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function isEmpty() {
17     $value = $this->get('value')->getValue();
18
19     // Take into account that the name of the anonymous user is an empty string.
20     if ($this->getEntity()->isAnonymous()) {
21       return $value === NULL;
22     }
23
24     return $value === NULL || $value === '';
25   }
26
27   /**
28    * {@inheritdoc}
29    */
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);
34     return $values;
35   }
36
37 }