class UserNameItem
Same name in other branches
- 9 core/modules/user/src/UserNameItem.php \Drupal\user\UserNameItem
- 8.9.x core/modules/user/src/UserNameItem.php \Drupal\user\UserNameItem
- 11.x core/modules/user/src/UserNameItem.php \Drupal\user\UserNameItem
Defines a custom field item class for the 'name' user entity field.
Hierarchy
- class \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\TypedDataInterface, \Drupal\Component\Plugin\PluginInspectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\TypedData\TypedDataTrait
- class \Drupal\Core\TypedData\Plugin\DataType\Map extends \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface
- class \Drupal\Core\Field\FieldItemBase extends \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\Field\FieldItemInterface
- class \Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase extends \Drupal\Core\Field\FieldItemBase
- class \Drupal\Core\Field\Plugin\Field\FieldType\StringItem extends \Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase
- class \Drupal\user\UserNameItem extends \Drupal\Core\Field\Plugin\Field\FieldType\StringItem
- class \Drupal\Core\Field\Plugin\Field\FieldType\StringItem extends \Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase
- class \Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase extends \Drupal\Core\Field\FieldItemBase
- class \Drupal\Core\Field\FieldItemBase extends \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\Field\FieldItemInterface
- class \Drupal\Core\TypedData\Plugin\DataType\Map extends \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface
Expanded class hierarchy of UserNameItem
2 files declare their use of UserNameItem
- EntityUser.php in core/
modules/ user/ src/ Plugin/ migrate/ destination/ EntityUser.php - UserNameItemTest.php in core/
modules/ user/ tests/ src/ Unit/ UserNameItemTest.php
File
-
core/
modules/ user/ src/ UserNameItem.php, line 12
Namespace
Drupal\userView source
class UserNameItem extends StringItem {
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')
->getValue();
// Take into account that the name of the anonymous user is an empty string.
if ($this->getEntity()
->isAnonymous()) {
return $value === NULL;
}
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$max_length = min(UserInterface::USERNAME_MAX_LENGTH, $field_definition->getSetting('max_length'));
// Generate a list of words, which can be used to generate a string.
$words = explode(' ', $random->sentences(8));
// Begin with a username that is either 2 or 3 words.
$count = mt_rand(2, 3);
// Capitalize the words used in usernames 50% of the time.
$words = mt_rand(0, 1) ? array_map('ucfirst', $words) : $words;
// Username is a single long word 50% of the time. In the case of a single
// long word, sometimes the generated username may also contain periods in
// the middle of the username.
$separator = ' ';
if (mt_rand(0, 1)) {
$separator = '';
$count = mt_rand(2, 8);
// The username will start with a capital letter 50% of the time.
$words = mt_rand(0, 1) ? array_map('strtolower', $words) : $words;
}
$string = implode($separator, array_splice($words, 0, $count));
// Normalize the string to not be longer than the maximum length, and to not
// end with a space or a period.
$values['value'] = rtrim(mb_substr($string, 0, $max_length), ' .');
return $values;
}
}
Members
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.