You are here

public function DoubleField::settingsForm in Double Field 8.3

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form definition for the widget settings.

Overrides WidgetBase::settingsForm

File

src/Plugin/Field/FieldWidget/DoubleField.php, line 53

Class

DoubleField
Plugin implementation of the 'double_field' widget.

Namespace

Drupal\double_field\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $settings = $this
    ->getSettings();
  $field_settings = $this
    ->getFieldSettings();
  $types = DoubleFieldItem::subfieldTypes();
  $field_name = $this->fieldDefinition
    ->getName();
  $element['inline'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display as inline element'),
    '#default_value' => $settings['inline'],
  ];
  foreach ([
    'first',
    'second',
  ] as $subfield) {
    $type = $field_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,
    ];
    $element[$subfield]['type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Widget'),
      '#default_value' => $settings[$subfield]['type'],
      '#required' => TRUE,
      '#options' => $this
        ->getSubwidgets($type, $field_settings[$subfield]['list']),
    ];
    $element[$subfield]['label_display'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Label display'),
      '#default_value' => $settings[$subfield]['label_display'],
      '#required' => TRUE,
      '#options' => [
        'block' => $this
          ->t('Block'),
        'inline' => $this
          ->t('Inline'),
        'invisible' => $this
          ->t('Invisible'),
        'hidden' => $this
          ->t('Hidden'),
      ],
      '#access' => self::isLabelSupported($settings[$subfield]['type']),
    ];
    $type_selector = "select[name='fields[{$field_name}][settings_edit_form][settings][{$subfield}][type]'";
    $element[$subfield]['size'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Size'),
      '#default_value' => $settings[$subfield]['size'],
      '#min' => 1,
      '#states' => [
        'visible' => [
          [
            $type_selector => [
              'value' => 'textfield',
            ],
          ],
          [
            $type_selector => [
              'value' => 'email',
            ],
          ],
          [
            $type_selector => [
              'value' => 'tel',
            ],
          ],
          [
            $type_selector => [
              'value' => 'url',
            ],
          ],
        ],
      ],
    ];
    $element[$subfield]['placeholder'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Placeholder'),
      '#default_value' => $settings[$subfield]['placeholder'],
      '#states' => [
        'visible' => [
          [
            $type_selector => [
              'value' => 'textfield',
            ],
          ],
          [
            $type_selector => [
              'value' => 'textarea',
            ],
          ],
          [
            $type_selector => [
              'value' => 'email',
            ],
          ],
          [
            $type_selector => [
              'value' => 'tel',
            ],
          ],
          [
            $type_selector => [
              'value' => 'url',
            ],
          ],
        ],
      ],
    ];
    $element[$subfield]['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#default_value' => $settings[$subfield]['label'],
      '#required' => TRUE,
      '#states' => [
        'visible' => [
          $type_selector => [
            'value' => 'checkbox',
          ],
        ],
      ],
    ];
    $element[$subfield]['cols'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Columns'),
      '#default_value' => $settings[$subfield]['cols'],
      '#min' => 1,
      '#description' => $this
        ->t('How many columns wide the textarea should be'),
      '#states' => [
        'visible' => [
          $type_selector => [
            'value' => 'textarea',
          ],
        ],
      ],
    ];
    $element[$subfield]['rows'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Rows'),
      '#default_value' => $settings[$subfield]['rows'],
      '#min' => 1,
      '#description' => $this
        ->t('How many rows high the textarea should be.'),
      '#states' => [
        'visible' => [
          $type_selector => [
            'value' => 'textarea',
          ],
        ],
      ],
    ];
    $element[$subfield]['prefix'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Prefix (deprecated)'),
      '#default_value' => $settings[$subfield]['prefix'],
    ];
    $element[$subfield]['suffix'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Suffix (deprecated)'),
      '#default_value' => $settings[$subfield]['suffix'],
    ];
  }
  return $element;
}