You are here

public function NameItem::fieldSettingsForm in Name Field 8

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

File

src/Plugin/Field/FieldType/NameItem.php, line 191

Class

NameItem
Plugin implementation of the 'name' field type.

Namespace

Drupal\name\Plugin\Field\FieldType

Code

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',
  ];

  // Add the overwrite user name option.
  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,
    ];

    // Store the machine name of the Name field.
    $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 {

    // We may extend this feature to Profile2 latter.
    $element['override_format'] = [
      '#type' => 'value',
      '#value' => $this
        ->getSetting('override_format'),
      '#table_group' => 'none',
    ];
  }
  return $element;
}