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 鹄鹂龃鵇鼬鼪鼦鵫鼠鵞龜鵧麣齧

评论 (写第一个评论)