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 鹐鹏龃鵧鼬鼨鼦鶇鼢鵻龠鶃麤齩

评论 (写第一个评论)