View source
<?php
namespace Drupal\name\Plugin\Field\FieldType;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\name\Traits\NameFieldSettingsTrait;
use Drupal\name\Traits\NameFormDisplaySettingsTrait;
use Drupal\name\Traits\NameFormSettingsHelperTrait;
use Drupal\name\Traits\NameAdditionalPreferredTrait;
class NameItem extends FieldItemBase implements TrustedCallbackInterface {
use NameFieldSettingsTrait;
use NameFormDisplaySettingsTrait;
use NameFormSettingsHelperTrait;
use NameAdditionalPreferredTrait;
protected static $components = [
'title',
'given',
'middle',
'family',
'generational',
'credentials',
];
public static function trustedCallbacks() {
return [
'fieldSettingsFormPreRender',
];
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$columns = [];
foreach (static::$components as $key) {
$columns[$key] = [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
];
}
return [
'columns' => $columns,
'indexes' => [
'given' => [
'given',
],
'family' => [
'family',
],
],
];
}
public static function defaultFieldSettings() {
$settings = self::getDefaultNameFieldSettings();
$settings += self::getDefaultNameFormDisplaySettings();
$settings += self::getDefaultAdditionalPreferredSettings();
$settings['override_format'] = 'default';
return $settings + parent::defaultFieldSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = [];
$properties['title'] = DataDefinition::create('string')
->setLabel(t('Title'));
$properties['given'] = DataDefinition::create('string')
->setLabel(t('Given'));
$properties['middle'] = DataDefinition::create('string')
->setLabel(t('Middle name(s)'));
$properties['family'] = DataDefinition::create('string')
->setLabel(t('Family'));
$properties['generational'] = DataDefinition::create('string')
->setLabel(t('Generational'));
$properties['credentials'] = DataDefinition::create('string')
->setLabel(t('Credentials'));
return $properties;
}
public static function mainPropertyName() {
return NULL;
}
public function isEmpty() {
foreach ($this->properties as $property) {
$definition = $property
->getDataDefinition();
if (!$definition
->isComputed() && $property
->getValue() !== NULL) {
return FALSE;
}
}
if (isset($this->values)) {
foreach ($this->values as $name => $value) {
if ($name == 'title' || $name == 'generational') {
continue;
}
if (isset($value) && strlen($value) && !isset($this->properties[$name])) {
return FALSE;
}
}
}
return TRUE;
}
public function filteredArray() {
$values = [];
$field = $this
->getFieldDefinition();
$settings = $field
->getSettings();
$active_components = array_filter($settings['components']);
foreach ($this
->getProperties() as $name => $property) {
if (isset($active_components[$name]) && $active_components[$name]) {
$values[$name] = $property
->getValue();
}
}
return $values;
}
public function activeComponents() {
$settings = $this
->getFieldDefinition()
->getSettings();
$components = [];
foreach (_name_translations() as $key => $label) {
if (!empty($settings['components'][$key])) {
$components[$key] = empty($settings['labels'][$key]) ? $label : $settings['labels'][$key];
}
}
return $components;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$settings = $this
->getSettings();
$element = $this
->getDefaultNameFieldSettingsForm($settings, $form, $form_state);
$element += $this
->getDefaultNameFormDisplaySettingsForm($settings, $form, $form_state);
foreach ($this
->getNameAdditionalPreferredSettingsForm($form, $form_state) as $key => $value) {
$element[$key] = $value;
$element[$key]['#table_group'] = 'none';
$element[$key]['#weight'] = 50;
}
$element['#pre_render'][] = [
$this,
'fieldSettingsFormPreRender',
];
if ($this
->getFieldDefinition()
->getTargetEntityTypeId() == 'user') {
$preferred_field = \Drupal::config('name.settings')
->get('user_preferred');
$element['name_user_preferred'] = [
'#type' => 'checkbox',
'#title' => $this
->t("Use this field to override the user's login name?"),
'#description' => $this
->t('You may need to clear the @cache_link before this change is seen everywhere.', [
'@cache_link' => Link::fromTextAndUrl('Performance cache', Url::fromRoute('system.performance_settings'))
->toString(),
]),
'#default_value' => $preferred_field == $this
->getFieldDefinition()
->getName() ? 1 : 0,
'#table_group' => 'above',
'#weight' => -100,
];
$element['name_user_preferred_fieldname'] = [
'#type' => 'hidden',
'#default_value' => $this
->getFieldDefinition()
->getName(),
'#table_group' => 'above',
'#weight' => -99,
];
$element['override_format'] = [
'#type' => 'select',
'#title' => $this
->t('User name override format to use'),
'#default_value' => $this
->getSetting('override_format'),
'#options' => name_get_custom_format_options(),
'#table_group' => 'above',
'#weight' => -98,
];
$element['#element_validate'] = [
[
get_class($this),
'validateUserPreferred',
],
];
}
else {
$element['override_format'] = [
'#type' => 'value',
'#value' => $this
->getSetting('override_format'),
'#table_group' => 'none',
];
}
return $element;
}
public static function validateUserPreferred(&$element, FormStateInterface $form_state, &$complete_form) {
$value = NULL;
$config = \Drupal::configFactory()
->getEditable('name.settings');
if (!empty($element['name_user_preferred']) && $element['name_user_preferred']['#value'] == 1) {
$value = $element['name_user_preferred_fieldname']['#default_value'];
}
if ($config
->get('user_preferred') != $value) {
$config
->set('user_preferred', $value)
->save();
$query = \Drupal::entityQuery('user');
$uids = $query
->execute();
foreach ($uids as $uid) {
Cache::invalidateTags([
'user:' . $uid,
]);
}
\Drupal::logger('name')
->notice('Cache cleared for data tagged as %tag.', [
'%tag' => 'user:{$uid}',
]);
}
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$names =& drupal_static(__FUNCTION__, []);
if (empty($names)) {
$names = \Drupal::service('name.generator')
->generateSampleNames(100, $field_definition);
}
return $names[array_rand($names)];
}
}