You are here

public function KeyValueWidgetTrait::settingsForm in Key value field 8

File

src/Plugin/Field/FieldWidget/KeyValueWidgetTrait.php, line 32

Class

KeyValueWidgetTrait
Common traits for key value field widgets inheriting from different widgets.

Namespace

Drupal\key_value_field\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {

  // Get the default textfield form.
  $parent_form = parent::settingsForm($form, $form_state);

  // Change the title for the Rows field.
  $parent_form['rows']['#title'] = t('Value Rows');

  // Change the title for the Placeholder field.
  $parent_form['placeholder']['#title'] = t('Value Placeholder');

  // Get the field machine_id.
  $field_machine = $this->fieldDefinition
    ->getName();

  // Add an element for the label of the key field.
  $element['key_label'] = [
    '#type' => 'textfield',
    '#title' => t('Key Label'),
    '#default_value' => $this
      ->getSetting('key_label'),
    '#description' => t('Label for the "Key" field.'),
    '#weight' => -3,
  ];

  // Add an element for the size of the key field.
  $element['key_size'] = [
    '#type' => 'number',
    '#title' => t('Size of key textfield'),
    '#default_value' => $this
      ->getSetting('key_size'),
    '#required' => TRUE,
    '#weight' => -2,
    '#min' => 1,
  ];

  // Add a placeholder field for the key text field.
  $element['key_placeholder'] = [
    '#type' => 'textfield',
    '#title' => t('Key Placeholder'),
    '#default_value' => $this
      ->getSetting('key_placeholder'),
    '#description' => t('Text that will be shown inside the "Key" field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
    '#weight' => -1,
  ];

  // Add an element for the label of the value field.
  $element['value_label'] = [
    '#type' => 'textfield',
    '#title' => t('Value Label'),
    '#default_value' => $this
      ->getSetting('value_label'),
    '#description' => t('Label for the "Value" field.'),
    '#weight' => 0,
  ];

  // Let the description field be hidden.
  $element['description_enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable Description'),
    '#default_value' => $this
      ->getSetting('description_enabled'),
    '#description' => t('Enable the description field (Generally used for administrative purposes).'),
    '#weight' => 2,
  ];

  // Add an element for the label of the value field.
  $element['description_label'] = [
    '#type' => 'textfield',
    '#title' => t('Description Label'),
    '#default_value' => $this
      ->getSetting('description_label'),
    '#description' => t('Label for the "Description" field.'),
    '#weight' => 3,
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $field_machine . '][settings_edit_form][settings][description_enabled]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Add a placeholder for teh description field.
  $element['description_placeholder'] = [
    '#type' => 'textfield',
    '#title' => t('Description Placeholder'),
    '#default_value' => $this
      ->getSetting('description_placeholder'),
    '#description' => t('Text that will be shown inside the "Description" field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
    '#weight' => 4,
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $field_machine . '][settings_edit_form][settings][description_enabled]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $element['description_rows'] = [
    '#type' => 'number',
    '#title' => t('Description Rows'),
    '#default_value' => $this
      ->getSetting('description_rows'),
    '#min' => 1,
    '#weight' => 5,
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $field_machine . '][settings_edit_form][settings][description_enabled]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  return $element + $parent_form;
}