9.7 如何更改配置

龹龶龳龲龯龮龫{module}.settings.yml龄龁齾齻齺齶齵齴齳龯龮齴齰form齦齢齟齝齛齘

龹龯龮齖齝齛齒齏齾齍齌 图片

example/src/Form/exampleSettingsForm.php

namespace Drupal\example\Form;

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

/**
 * Configure example settings for this site.
 */
class exampleSettingsForm extends ConfigFormBase {
  /** 
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'example_admin_settings';
  }

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

  /** 
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('example.settings');

    $form['example_thing'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Things'),
      '#default_value' => $config->get('things'),
    );  

    $form['other_things'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Other things'),
      '#default_value' => $config->get('other_things'),
    );  

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

  /** 
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Retrieve the configuration
    $this->config('example.settings')
      // Set the submitted configuration setting
      ->set('things', $form_state->getValue('example_thing'))
      // You can set multiple configurations at once by making
      // multiple calls to set()
      ->set('other_things', $form_state->getValue('other_things'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}

example.routing.yml

example.settings:
  path: '/admin/structure/example/settings'
  defaults:
    _form: '\Drupal\example\Form\exampleSettingsForm'
    _title: 'example'
  requirements:
    _permission: 'administer site configuration'

黨黦黤黢黠黝黛黚黙齾黗黖黕龹龶齴齰form齦齢齟龳龲黔黑齾齻齺齘

本书共79小节:

评论 (写第一个评论)