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 鹜鹛龎鵿鼱鼭鼫鶞鼩鶕龠鶝麳齱

评论 (写第一个评论)