function NestedConfigTargetForm::buildForm

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/form_test/src/Form/NestedConfigTargetForm.php \Drupal\form_test\Form\NestedConfigTargetForm::buildForm()

Overrides TreeConfigTargetForm::buildForm

File

core/modules/system/tests/modules/form_test/src/Form/NestedConfigTargetForm.php, line 34

Class

NestedConfigTargetForm
Test form for testing config targets that are not 1:1.

Namespace

Drupal\form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['favorites'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#tree' => TRUE,
    '#title' => 'Favorite fruits',
  ];
  $form['favorites']['first'] = [
    '#type' => 'textfield',
    '#title' => 'First choice',
    '#config_target' => new ConfigTarget('form_test.object', 'favorite_fruits', fromConfig: fn(?array $favorite_fruits): string => $favorite_fruits[0] ?? 'Mango', toConfig: fn(string $first, FormStateInterface $form_state): array => [
      0 => $first,
      1 => $form_state->getValue([
        'favorites',
        'second',
      ]),
    ]),
  ];
  $form['favorites']['second'] = [
    '#type' => 'textfield',
    '#title' => 'Second choice',
    '#config_target' => new ConfigTarget('form_test.object', 'favorite_fruits.1', fn(?string $second_favorite_fruit): string => $second_favorite_fruit ?? 'Orange', fn() => ToConfig::NoOp),
  ];
  $form['could_not_live_without'] = [
    '#weight' => 10,
    '#type' => 'textfield',
    '#title' => 'I could not live without',
    '#placeholder' => 'vegetables',
    '#wrapper_attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
    '#config_target' => new ConfigTarget('form_test.object', 'could_not_live_without', toConfig: function (string $could_not_live_without, FormStateInterface $form_state) : ToConfig|string {
      if (empty($form_state->getValue([
        'favorites',
        'first',
      ]))) {
        return ToConfig::DeleteKey;
      }
      if (empty($form_state->getValue([
        'favorites',
        'second',
      ]))) {
        return ToConfig::DeleteKey;
      }
      if (empty($form_state->getValue([
        'vegetables',
        'favorite',
      ]))) {
        return ToConfig::DeleteKey;
      }
      if (empty($form_state->getValue([
        'vegetables',
        'nemesis',
      ]))) {
        return ToConfig::DeleteKey;
      }
      return $could_not_live_without;
    }),
    // Only if everything else is answered will this be asked.
'#states' => [
      'visible' => [
        // 2 favorite fruits.
':input[name="favorites[first]"]' => [
          'empty' => FALSE,
        ],
        ':input[name="favorites[second]"]' => [
          'empty' => FALSE,
        ],
        // Favorite & nemesis vegetable.
':input[name="vegetables[favorite]"]' => [
          'empty' => FALSE,
        ],
        ':input[name="vegetables[nemesis]"]' => [
          'empty' => FALSE,
        ],
      ],
    ],
  ];
  return parent::buildForm($form, $form_state);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.