cc-1.0.x-dev/modules/cc_dex/src/Form/NetworkSettingsForm.php

modules/cc_dex/src/Form/NetworkSettingsForm.php
<?php

namespace Drupal\cc_dex\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a form for configuring DEX network settings.
 */
class NetworkSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'cc_dex_network_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['cc.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('cc.settings');
    $networks = $config->get('dex_networks') ?: [];

    $form['networks'] = [
      '#type' => 'table',
      '#header' => [
        $this->t('Network Name'),
        $this->t('Chain ID'),
        $this->t('RPC URL'),
        $this->t('Explorer URL'),
        $this->t('Native Currency'),
        $this->t('Operations'),
      ],
      '#empty' => $this->t('No networks configured yet.'),
    ];

    foreach ($networks as $delta => $network) {
      $form['networks'][$delta]['name'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Network Name'),
        '#title_display' => 'invisible',
        '#default_value' => $network['name'] ?? '',
        '#required' => TRUE,
        '#size' => 30,
      ];

      $form['networks'][$delta]['chain_id'] = [
        '#type' => 'number',
        '#title' => $this->t('Chain ID'),
        '#title_display' => 'invisible',
        '#default_value' => $network['chain_id'] ?? '',
        '#required' => TRUE,
        '#min' => 1,
      ];

      $form['networks'][$delta]['rpc_url'] = [
        '#type' => 'url',
        '#title' => $this->t('RPC URL'),
        '#title_display' => 'invisible',
        '#default_value' => $network['rpc_url'] ?? '',
        '#required' => TRUE,
        '#size' => 50,
      ];

      $form['networks'][$delta]['explorer_url'] = [
        '#type' => 'url',
        '#title' => $this->t('Explorer URL'),
        '#title_display' => 'invisible',
        '#default_value' => $network['explorer_url'] ?? '',
        '#size' => 50,
      ];

      $form['networks'][$delta]['native_currency'] = [
        '#type' => 'details',
        '#title' => $this->t('Native Currency'),
        '#open' => TRUE,
      ];

      $form['networks'][$delta]['native_currency']['symbol'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Symbol'),
        '#default_value' => $network['native_currency']['symbol'] ?? '',
        '#required' => TRUE,
        '#size' => 10,
      ];

      $form['networks'][$delta]['native_currency']['decimals'] = [
        '#type' => 'number',
        '#title' => $this->t('Decimals'),
        '#default_value' => $network['native_currency']['decimals'] ?? 18,
        '#required' => TRUE,
        '#min' => 1,
        '#max' => 36,
      ];

      $form['networks'][$delta]['remove'] = [
        '#type' => 'submit',
        '#value' => $this->t('Remove'),
        '#name' => 'remove_' . $delta,
        '#submit' => ['::removeNetwork'],
        '#ajax' => [
          'callback' => '::updateAjax',
          'wrapper' => 'edit-networks',
        ],
      ];
    }

    $form['add_network'] = [
      '#type' => 'submit',
      '#value' => $this->t('Add Network'),
      '#submit' => ['::addNetwork'],
      '#ajax' => [
        '#callback' => '::updateAjax',
        '#wrapper' => 'edit-networks',
      ],
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('cc.settings');
    $networks = [];

    foreach ($form_state->getValue('networks') as $delta => $network) {
      if (!empty($network['name'])) {
        $networks[] = [
          'name' => $network['name'],
          'chain_id' => (int) $network['chain_id'],
          'rpc_url' => $network['rpc_url'],
          'explorer_url' => $network['explorer_url'],
          'native_currency' => [
            'symbol' => $network['native_currency']['symbol'],
            'decimals' => (int) $network['native_currency']['decimals'],
          ],
        ];
      }
    }

    $config->set('dex_networks', $networks)->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Ajax callback for updating the form.
   */
  public function updateAjax(array &$form, FormStateInterface $form_state) {
    return $form['networks'];
  }

  /**
   * Submit handler for adding a new network.
   */
  public function addNetwork(array &$form, FormStateInterface $form_state) {
    $networks = $form_state->getValue('networks', []);
    $networks[] = [];
    $form_state->setValue('networks', $networks);
    $form_state->setRebuild();
  }

  /**
   * Submit handler for removing a network.
   */
  public function removeNetwork(array &$form, FormStateInterface $form_state) {
    $trigger = $form_state->getTriggeringElement();
    $delta = str_replace('remove_', '', $trigger['#name']);
    $networks = $form_state->getValue('networks');
    unset($networks[$delta]);
    $form_state->setValue('networks', array_values($networks));
    $form_state->setRebuild();
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc