You are here

protected function NameFieldSettingsTrait::getDefaultNameFieldSettingsForm in Name Field 8

Returns a form for the default settings defined above.

The following keys are closely tied to the pre-render function to theme the settings into a nicer table.

  • #indent_row: Adds an empty TD cell and adds an 'elements' child that contains the children (if given).
  • #table_group: Used to either position within the table by the element key, or set to 'none', to append it below the table.

Any element within the table should have component keyed children.

Other elements are rendered directly.

Parameters

array $settings: The settings.

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.

bool $has_data: A flag to indicate if the field has data.

Return value

array The form definition for the field settings.

1 call to NameFieldSettingsTrait::getDefaultNameFieldSettingsForm()
NameItem::fieldSettingsForm in src/Plugin/Field/FieldType/NameItem.php
Returns a form for the field-level settings.

File

src/Traits/NameFieldSettingsTrait.php, line 137

Class

NameFieldSettingsTrait
Name settings trait.

Namespace

Drupal\name\Traits

Code

protected function getDefaultNameFieldSettingsForm(array $settings, array &$form, FormStateInterface $form_state, $has_data = TRUE) {
  $components = _name_translations();
  $field_options = [
    'select' => $this
      ->t('Drop-down'),
    'text' => $this
      ->t('Text field'),
    'autocomplete' => $this
      ->t('Autocomplete'),
  ];

  // @todo: Refactor out for alternative sources.
  $autocomplete_sources_options = [
    'title' => $this
      ->t('Title options'),
    'generational' => $this
      ->t('Generational options'),
  ];
  $element = [];
  $element['components'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Components'),
    '#default_value' => array_keys(array_filter($settings['components'])),
    '#required' => TRUE,
    '#description' => $this
      ->t('Only selected components will be activated on this field. All non-selected components / component settings will be ignored.'),
    '#options' => $components,
  ];
  $element['minimum_components'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Minimum components'),
    '#default_value' => array_keys(array_filter($settings['minimum_components'])),
    '#required' => TRUE,
    '#description' => $this
      ->t('The minimal set of components required before the field is considered completed enough to save.'),
    '#options' => $components,
    '#element_validate' => [
      [
        get_class($this),
        'validateMinimumComponents',
      ],
    ],
  ];

  // Placeholder for additional fields to couple with the components section.
  $element['components_extra'] = [
    '#indent_row' => TRUE,
  ];
  $element['allow_family_or_given'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow a single valid given or family value to fulfill the minimum component requirements for both given and family components.'),
    '#default_value' => !empty($settings['allow_family_or_given']),
    '#table_group' => 'components_extra',
  ];
  $element['field_type'] = [
    '#title' => $this
      ->t('Field type'),
    '#description' => $this
      ->t('The Field type controls how the field is rendered. Autocomplete is a text field with autocomplete, and the behaviour of this is controlled by the field settings.'),
  ];
  $element['max_length'] = [
    '#title' => $this
      ->t('Maximum length'),
    '#description' => $this
      ->t('The maximum length of the field in characters. This must be between 1 and 255.'),
  ];
  $sort_options = is_array($settings['sort_options']) ? $settings['sort_options'] : [
    'title' => 'title',
    'generational' => '',
  ];
  $element['sort_options'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Sort options'),
    '#default_value' => $sort_options,
    '#description' => $this
      ->t("This enables sorting on the options after the vocabulary terms are added and duplicate values are removed."),
    '#options' => _name_translations([
      'title' => '',
      'generational' => '',
    ]),
  ];
  $element['autocomplete_source'] = [
    '#title' => $this
      ->t('Autocomplete sources'),
    '#description' => $this
      ->t('At least one value must be selected before you can enable the autocomplete option on the input textfields.'),
  ];
  $element['autocomplete_separator'] = [
    '#title' => $this
      ->t('Autocomplete separator'),
    '#description' => $this
      ->t('This allows you to override the default handling that the autocomplete uses to handle separations between components. If empty, this defaults to a single space.'),
  ];
  foreach ($components as $key => $title) {
    $min_length = 1;
    $element['max_length'][$key] = [
      '#type' => 'number',
      '#min' => $min_length,
      '#max' => 255,
      '#title' => $this
        ->t('Maximum length for @title', [
        '@title' => $title,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $settings['max_length'][$key],
      '#required' => TRUE,
      '#size' => 5,
    ];
    $element['autocomplete_source'][$key] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Autocomplete options'),
      '#title_display' => 'invisible',
      '#default_value' => $settings['autocomplete_source'][$key],
      '#options' => $autocomplete_sources_options,
    ];
    if ($key != 'title') {
      unset($element['autocomplete_source'][$key]['#options']['title']);
    }
    if ($key != 'generational') {
      unset($element['autocomplete_source'][$key]['#options']['generational']);
    }
    $element['autocomplete_separator'][$key] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Autocomplete separator for @title', [
        '@title' => $title,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $settings['autocomplete_separator'][$key],
      '#size' => 10,
    ];
    $element['field_type'][$key] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('@title field type', [
        '@title' => $components['title'],
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $settings['field_type'][$key],
      '#required' => TRUE,
      '#options' => $field_options,
    ];
    if (!($key == 'title' || $key == 'generational')) {
      unset($element['field_type'][$key]['#options']['select']);
    }
  }

  // TODO - Grouping & grouping sort
  // TODO - Allow reverse free tagging back into the vocabulary.
  $title_options = implode("\n", array_filter($settings['title_options']));
  $element['title_options'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('@title options', [
      '@title' => $components['title'],
    ]),
    '#default_value' => $title_options,
    '#required' => TRUE,
    '#description' => $this
      ->t("Enter one @title per line. Prefix a line using '--' to specify a blank value text. For example: '--Please select a @title'.", [
      '@title' => $components['title'],
    ]),
    '#element_validate' => [
      [
        get_class($this),
        'validateTitleOptions',
      ],
    ],
    '#table_group' => 'none',
  ];
  $generational_options = implode("\n", array_filter($settings['generational_options']));
  $element['generational_options'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('@generational options', [
      '@generational' => $components['generational'],
    ]),
    '#default_value' => $generational_options,
    '#required' => TRUE,
    '#description' => $this
      ->t("Enter one @generational suffix option per line. Prefix a line using '--' to specify a blank value text. For example: '----'.", [
      '@generational' => $components['generational'],
    ]),
    '#element_validate' => [
      [
        get_class($this),
        'validateGenerationalOptions',
      ],
    ],
    '#table_group' => 'none',
  ];
  if (\Drupal::moduleHandler()
    ->moduleExists('taxonomy')) {

    // TODO - Make the labels more generic.
    // Generational suffixes may be imported from one or more vocabularies
    // using the tag '[vocabulary:xxx]', where xxx is the vocabulary id.
    // Terms that exceed the maximum length of the generational suffix are
    // not added to the options list.
    $element['title_options']['#description'] .= ' ' . $this
      ->t("%label_plural may be also imported from one or more vocabularies using the tag '[vocabulary:xxx]', where xxx is the vocabulary machine-name or id. Terms that exceed the maximum length of the %label are not added to the options list.", [
      '%label_plural' => $this
        ->t('Titles'),
      '%label' => $this
        ->t('Title'),
    ]);
    $element['generational_options']['#description'] .= ' ' . $this
      ->t("%label_plural may be also imported from one or more vocabularies using the tag '[vocabulary:xxx]', where xxx is the vocabulary machine-name or id. Terms that exceed the maximum length of the %label are not added to the options list.", [
      '%label_plural' => $this
        ->t('Generational suffixes'),
      '%label' => $this
        ->t('Generational suffix'),
    ]);
  }
  $items = [
    $this
      ->t('The order for Asian names is Family Middle Given Title Credentials'),
    $this
      ->t('The order for Eastern names is Title Family Given Middle Credentials'),
    $this
      ->t('The order for German names is Title Credentials Given Middle Surname'),
    $this
      ->t('The order for Western names is Title Given Middle Surname Credentials'),
  ];
  $item_list = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  $layout_description = $this
    ->t('<p>This controls the order of the widgets that are displayed in the form.</p>') . \Drupal::service('renderer')
    ->render($item_list) . $this
    ->t('<p>Note that when you select the Asian and German name formats, the Generational field is hidden and defaults to an empty string.</p>');
  $element['component_layout'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Language layout'),
    '#default_value' => $this
      ->getSetting('component_layout'),
    '#options' => [
      'default' => $this
        ->t('Western names'),
      'asian' => $this
        ->t('Asian names'),
      'eastern' => $this
        ->t('Eastern names'),
      'german' => $this
        ->t('German names'),
    ],
    '#description' => $layout_description,
    '#table_group' => 'above',
    '#required' => TRUE,
    '#weight' => -49,
  ];
  return $element;
}