You are here

public function DoubleField::fieldSettingsForm in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldType/DoubleField.php \Drupal\double_field\Plugin\Field\FieldType\DoubleField::fieldSettingsForm()

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/DoubleField.php, line 161

Class

DoubleField
Plugin implementation of the 'double_field' field type.

Namespace

Drupal\double_field\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) : array {
  $settings = $this
    ->getSettings();
  $types = static::subfieldTypes();
  $element = [];
  foreach ([
    'first',
    'second',
  ] as $subfield) {
    $type = $settings['storage'][$subfield]['type'];
    $title = $subfield == 'first' ? $this
      ->t('First subfield') : $this
      ->t('Second subfield');
    $title .= ' - ' . $types[$type];
    $element[$subfield] = [
      '#type' => 'details',
      '#title' => $title,
      '#open' => FALSE,
      '#tree' => TRUE,
    ];
    $element[$subfield]['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#default_value' => $settings[$subfield]['label'],
    ];
    $element[$subfield]['required'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Required'),
      '#default_value' => $settings[$subfield]['required'],
    ];
    if (static::isListAllowed($type)) {
      $element[$subfield]['list'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Limit allowed values'),
        '#default_value' => $settings[$subfield]['list'],
      ];
      $description[] = $this
        ->t('The possible values this field can contain. Enter one value per line, in the format key|label.');
      $description[] = $this
        ->t('The label will be used in displayed values and edit forms.');
      $description[] = $this
        ->t('The label is optional: if a line contains a single item, it will be used as key and label.');
      $element[$subfield]['allowed_values'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Allowed values list'),
        '#description' => implode('<br/>', $description),
        '#default_value' => $this
          ->allowedValuesString($settings[$subfield]['allowed_values']),
        '#rows' => 10,
        '#element_validate' => [
          [
            get_class($this),
            'validateAllowedValues',
          ],
        ],
        // @see: DoubleField::validateAllowedValues()
        '#storage_type' => $type,
        '#storage_maxlength' => $settings['storage'][$subfield]['maxlength'],
        '#field_name' => $this
          ->getFieldDefinition()
          ->getName(),
        '#entity_type' => $this
          ->getEntity()
          ->getEntityTypeId(),
        '#allowed_values' => $settings[$subfield]['allowed_values'],
        '#states' => [
          'invisible' => [
            ":input[name='settings[{$subfield}][list]']" => [
              'checked' => FALSE,
            ],
          ],
        ],
      ];
    }
    else {
      $element[$subfield]['list'] = [
        '#type' => 'value',
        '#default_value' => FALSE,
      ];
      $element[$subfield]['allowed_values'] = [
        '#type' => 'value',
        '#default_value' => [],
      ];
    }
    if (in_array($type, [
      'integer',
      'float',
      'numeric',
    ])) {
      $element[$subfield]['min'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Minimum'),
        '#description' => $this
          ->t('The minimum value that should be allowed in this field. Leave blank for no minimum.'),
        '#default_value' => $settings[$subfield]['min'] ?? NULL,
        '#states' => [
          'visible' => [
            ":input[name='settings[{$subfield}][list]']" => [
              'checked' => FALSE,
            ],
          ],
        ],
      ];
      $element[$subfield]['max'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Maximum'),
        '#description' => $this
          ->t('The maximum value that should be allowed in this field. Leave blank for no maximum.'),
        '#default_value' => $settings[$subfield]['max'] ?? NULL,
        '#states' => [
          'visible' => [
            ":input[name='settings[{$subfield}][list]']" => [
              'checked' => FALSE,
            ],
          ],
        ],
      ];
    }
    else {
      $element[$subfield]['min'] = $element[$subfield]['max'] = [
        '#type' => 'value',
        '#default_value' => '',
      ];
    }
    if ($type == 'boolean') {
      $element[$subfield]['on_label'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('"On" label'),
        '#default_value' => $settings[$subfield]['on_label'],
      ];
      $element[$subfield]['off_label'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('"Off" label'),
        '#default_value' => $settings[$subfield]['off_label'],
      ];
    }
    else {
      $element[$subfield]['on_label'] = [
        '#type' => 'value',
        '#default_value' => $settings[$subfield]['on_label'],
      ];
      $element[$subfield]['off_label'] = [
        '#type' => 'value',
        '#default_value' => $settings[$subfield]['off_label'],
      ];
    }
  }
  return $element;
}