3.2 配置和 Config 对象

Drupal 8 龩龧龤龢龡 Config 龒龎 龋龊龇龆龂齿齻齺齶齴齳齰

齯齬齪齨齧齦齤 齢齞齝齜 齙龊齗齕龋齔齓 ConfigFormBase 齃齀齦齤齢齞齝齜龩龧龤龢龡 Config 龒龎齰

鼽鼺鼷龢鼵鼱 configform_example 鼢鼠龋龊齙鼟龤鼜齪鼱 config() 鼔鼒鼎鼋龤 Config 龒龎龋鼊鼟 configform_example.settings 鼀黽龤齿齻黼黸齰黶黴齙鼟 Config 龒龎鼱 get() 鼔鼒黱黰 email 黬黨黥黢 email 黡黝黛默鼱黔黓黐齰

改变 Config 对象

黍黌龡鼔鼒黋黊黇黅黃鼎鼋 Config 龒龎,麾黴齙鼟龊鼱 set() 鼔鼒麼 email_address 麶黢鼟麵龩齴鼱黐齰

麱鼺麯齙鼟 save() 鼔鼒麬麩齿齻齰

麧黴黊黇麣鼟鼜齪鼱龩齴麢麟鼔鼒龋麜黢龊麚麗麼 Drupal 麖麔麓麒麐麌麈鼱麆麃齰

public function submitForm(array &$form, FormStateInterface $form_state) {     
  $config = $this->config('configform_example.settings');
  $config->set('email_address', $form_state->getValue('email'));
  $config->save();
  return parent::submitForm($form, $form_state);

测试这个对象

麼鼷鼵 ConfigFormExampleConfigForm.php 鹼鹸鹴黼黸鹰鼱鼢鼠鹬麶龋鹩鹥鹡麩鹠鹜齰

龩齴龢龡鹘鼱 email龋麩鹗黍齿齻黼齰鹖鹕鼱 configform_example.settings.yml 鹐鹍鹊鹆鹄鹂龋鹁鸿龇龆鸽鸻 configform_example.settings 齿齻龋黶黴麼龊鸽齜麐鸺鸸鸷鸳齰

<?php

/**
 * @file
 * Contains \Drupal\configform_example\Form\ConfigFormExampleConfigForm.
 */

namespace Drupal\configform_example\Form;

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

class ConfigFormExampleConfigForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this->config('configform_example.settings');
    $form['email'] = [
      '#type' => 'email',
      '#title' => $this->t('Your .com email address.'),
      '#default_value' => $config->get('email_address'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (strpos($form_state->getValue('email'), '.com') === FALSE ) {
      $form_state->setErrorByName('email', $this->t('This is not a .com email address.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('configform_example.settings');
    $config->set('email_address', $form_state->getValue('email'));
    $config->save();
    return parent::submitForm($form, $form_state);
  }

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

使用预先写好的代码

龎黶鸱鸰鸬鼱鸫鸩龋龢龡鸨鼟鼱鼔鼒齀齙鼟黶鸱鸥鸢鼱鼢鼠黥黢鹘鸠黥鼱鸜鸳齰龇龆黍鸚 1 鸔鸑鸏鼱 page_example 鹖鹕鸌鸉鼷鹬麶鼢鼠齰

Examples project: configform_example.links.menu.yml

configform_example.form:
 title: Config Form Example
 route_name: configform_example_form
 parent: system.admin_reports

为 configform_example 模块进行 YAML 配置

麼龆鼺鸁龡齿齻鹐鹍鸀黍 configform_example 鹖鹕鷼鷸鷷鼺齰

configform_example.info.yml鷵

name: Config Form example
type: module
description: 'An example module showing how to define a configuration form to be displayed at a given URL.'
package: Example modules
core: 8.x

configform_example.routing.yml鷵

configform_example_form:
 path: 'examples/configform_example/form'
 defaults:
 _form: '\Drupal\configform_example\Form\ConfigFormExampleConfigForm'
 _title: 'Config Form'
 requirements:
 _permission: 'access simple page'
 

配置监测器

drupal.org 鼷鸥鷖鷕 Configuration inspector for Drupal 8 齙鼟龤鷔鷓黼齻鼱齿齻龂 鹖鷐鷏鷎 龋齙鸿麃鷌鷉鹠齿齻黐鷅鹖鷐鼱齙鼟鷃鶿齰齙鶽鶺鶶麃鷌鶴鶳鶯鼽鶫齯齿齻黐龋鼊齺齶鶪鶨鹠鹜鶤鷔鸨鶠鶞齰鶝

鶜鶚黌龡鹖鹕黴龋鸿鹆黍黊黇鼱鶖鶒鶎齿齻鶌黴鶉鼽麐鷕raw data鶝黌龡鶆麱龋鹁鷕鶂黡鶝鶤鶝TREE鶝鶤鶝黡黝鶝鵿鶌鶆麱鵾齯齰黢龤鵻鵹黌龡鸿龇龆黢鵵齻鵲鵯鼺鹖鷐鶒鶎龋 ./config/schema/configform_example.schema.yml

configform_example.settings:
  type: mapping
  label: 'Configform Example settings'
  mapping:
    email_address:
      type: string
      label: 'This is the example email address.'

鵭鹘鶜鶚鼺 configform_example 鹖鹕龋鵪鼽鼺鼷鶉鼱鵿龡鶆麱齰

评论 (写第一个评论)