You are here

public function FieldFormBase::buildForm in Display Suite 8.4

Same name and namespace in other branches
  1. 8.2 src/Form/FieldFormBase.php \Drupal\ds\Form\FieldFormBase::buildForm()
  2. 8.3 src/Form/FieldFormBase.php \Drupal\ds\Form\FieldFormBase::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

4 calls to FieldFormBase::buildForm()
BlockFieldForm::buildForm in src/Form/BlockFieldForm.php
Form constructor.
CopyFieldForm::buildForm in src/Form/CopyFieldForm.php
Form constructor.
TokenFieldForm::buildForm in src/Form/TokenFieldForm.php
Form constructor.
TwigFieldForm::buildForm in src/Form/TwigFieldForm.php
Form constructor.
5 methods override FieldFormBase::buildForm()
BlockFieldConfigForm::buildForm in src/Form/BlockFieldConfigForm.php
Form constructor.
BlockFieldForm::buildForm in src/Form/BlockFieldForm.php
Form constructor.
CopyFieldForm::buildForm in src/Form/CopyFieldForm.php
Form constructor.
TokenFieldForm::buildForm in src/Form/TokenFieldForm.php
Form constructor.
TwigFieldForm::buildForm in src/Form/TwigFieldForm.php
Form constructor.

File

src/Form/FieldFormBase.php, line 90

Class

FieldFormBase
Base form for fields.

Namespace

Drupal\ds\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $field_key = '') {

  // Initialize field.
  $field = [];

  // Fetch field if it already exists.
  if (!empty($field_key)) {
    $field = $this
      ->config('ds.field.' . $field_key)
      ->get();
  }

  // Save the field for future reuse.
  $this->field = $field;
  $form['name'] = [
    '#title' => $this
      ->t('Label'),
    '#type' => 'textfield',
    '#default_value' => isset($field['label']) ? $field['label'] : '',
    '#description' => $this
      ->t('The human-readable label of the field.'),
    '#maxlength' => 128,
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => isset($field['id']) ? $field['id'] : '',
    '#maxlength' => 32,
    '#description' => $this
      ->t('The machine-readable name of this field. This name must contain only lowercase letters and underscores. This name must be unique.'),
    '#disabled' => !empty($field['id']),
    '#machine_name' => [
      'exists' => [
        $this,
        'uniqueFieldName',
      ],
      'source' => [
        'name',
      ],
    ],
  ];
  $entity_options = [];
  $entities = $this->entityTypeManager
    ->getDefinitions();
  foreach ($entities as $entity_type => $entity_info) {
    if ($entity_info
      ->get('field_ui_base_route') || $entity_type == 'ds_views') {
      $entity_options[$entity_type] = Unicode::ucfirst(str_replace('_', ' ', $entity_type));
    }
  }
  $form['entities'] = [
    '#title' => $this
      ->t('Entities'),
    '#description' => $this
      ->t('Select the entities for which this field will be made available.'),
    '#type' => 'checkboxes',
    '#required' => TRUE,
    '#options' => $entity_options,
    '#default_value' => isset($field['entities']) ? $field['entities'] : [],
  ];
  $form['ui_limit'] = [
    '#title' => $this
      ->t('Limit field'),
    '#description' => $this
      ->t('Limit this field on field UI per bundles and/or view modes. The values are in the form of $bundle|$view_mode, where $view_mode may be either a view mode set to use custom settings, or \'default\'. You may use * to select all, e.g article|*, *|full or *|*. Enter one value per line.'),
    '#type' => 'textarea',
    '#default_value' => isset($field['ui_limit']) ? $field['ui_limit'] : '',
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#weight' => 100,
  ];
  return $form;
}