You are here

public function DoubleField::storageSettingsForm 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::storageSettingsForm()

Returns a form for the storage-level settings.

Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow administrators to configure storage-level settings.

Field storage might reject settings changes that affect the field storage schema if the storage already has data. When the $has_data parameter is TRUE, the form should not allow changing the settings that take part in the schema() method. It is recommended to set #access to FALSE on the corresponding elements.

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.

bool $has_data: TRUE if the field already has data, FALSE if not.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::storageSettingsForm

File

src/Plugin/Field/FieldType/DoubleField.php, line 54

Class

DoubleField
Plugin implementation of the 'double_field' field type.

Namespace

Drupal\double_field\Plugin\Field\FieldType

Code

public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) : array {
  $settings = $this
    ->getSettings();
  $element = [];
  foreach ([
    'first',
    'second',
  ] as $subfield) {
    $element['storage'][$subfield] = [
      '#type' => 'details',
      '#title' => $subfield == 'first' ? $this
        ->t('First subfield') : $this
        ->t('Second subfield'),
      '#open' => TRUE,
    ];
    $element['storage'][$subfield]['type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Field type'),
      '#default_value' => $settings['storage'][$subfield]['type'],
      '#disabled' => $has_data,
      '#required' => TRUE,
      '#options' => $this
        ->subfieldTypes(),
    ];
    $element['storage'][$subfield]['maxlength'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Maximum length'),
      '#description' => $this
        ->t('The maximum length of the subfield in characters.'),
      '#default_value' => $settings['storage'][$subfield]['maxlength'],
      '#disabled' => $has_data,
      '#required' => TRUE,
      '#min' => 1,
      '#states' => [
        'visible' => [
          ":input[name='settings[storage][{$subfield}][type]']" => [
            'value' => 'string',
          ],
        ],
      ],
    ];
    $element['storage'][$subfield]['precision'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Precision'),
      '#description' => $this
        ->t('The total number of digits to store in the database, including those to the right of the decimal.'),
      '#default_value' => $settings['storage'][$subfield]['precision'],
      '#disabled' => $has_data,
      '#required' => TRUE,
      '#min' => 10,
      '#max' => 32,
      '#states' => [
        'visible' => [
          ":input[name='settings[storage][{$subfield}][type]']" => [
            'value' => 'numeric',
          ],
        ],
      ],
    ];
    $element['storage'][$subfield]['scale'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Scale'),
      '#description' => $this
        ->t('The number of digits to the right of the decimal.'),
      '#default_value' => $settings['storage'][$subfield]['scale'],
      '#disabled' => $has_data,
      '#required' => TRUE,
      '#min' => 0,
      '#max' => 10,
      '#states' => [
        'visible' => [
          ":input[name='settings[storage][{$subfield}][type]']" => [
            'value' => 'numeric',
          ],
        ],
      ],
    ];
    $element['storage'][$subfield]['datetime_type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Date type'),
      '#description' => $this
        ->t('Choose the type of date to create.'),
      '#default_value' => $settings['storage'][$subfield]['datetime_type'],
      '#disabled' => $has_data,
      '#options' => [
        'datetime' => $this
          ->t('Date and time'),
        'date' => $this
          ->t('Date only'),
      ],
      '#states' => [
        'visible' => [
          ":input[name='settings[storage][{$subfield}][type]']" => [
            'value' => 'datetime_iso8601',
          ],
        ],
      ],
    ];
  }
  return $element;
}