9.4 配置shema/metadata

  • schema 龪龦龣龢龡龝龚龗龖
  • metadata 龣龢龚龏龌龉

1龄龀齼齹齶龚齳齱

齮齭齫齪齧齦:

<?php
$config = \Drupal::config('system.maintenance');
$message  = $config->get('message');
$langcode = $config->get('langcode');
?>

config龌龉龗龖鼯龚鼫鼨鼧鼥鼢鼡鼟龣龢龡龝core/modules/system/config/install/system.maintenance.yml

齪齧齭齫:

message: '@site is currently under maintenance. We should be back shortly. Thank you for your patience.'
langcode: en

鼟core/modules/system/config/schema/system.schema.yml龡龝鼊鼉鼅龌龉龗龖鼯鼄

system.maintenance:
  type: config_object
  label: 'Maintenance mode'
  mapping:
    message:
      type: text
      label: 'Message to display when in maintenance mode'

schema鼉鼅黴龀齼mapping龌龉龗龖鼯黱黮黬鼨黨鼥龀齼龌黧. 黤黠點龀齼schema.yml龡龝黮黬鼉鼅黛齼龌龉龗龖鼯黱黚龀齼龌龉龗龖鼯龚默黕黓黒黏黋setting.yml龡龝黓龀黈鼄

2龄schema黂麾

  • type: 齮麺麶麵麲麮麬麫麵麲

麺麶麵麲(core.data_types.schema.yml)點

# Undefined type used by the system to assign to elements at any level where
# configuration schema is not defined. Using explicitly has the same effect as
# not defining schema, so there is no point in doing that.
undefined:
  label: 'Undefined'
  class: '\Drupal\Core\Config\Schema\Undefined'

# Explicit type to use when no data typing is possible. Instead of using this
# type, we strongly suggest you use configuration structures that can be
# described with other structural elements of schema, and describe your schema
# with those elements.
ignore:
  label: 'Ignore'
  class: '\Drupal\Core\Config\Schema\Ignore'

# Basic scalar data types from typed data.
boolean:
  label: 'Boolean'
  class: '\Drupal\Core\TypedData\Plugin\DataType\BooleanData'
email:
  label: 'Email'
  class: '\Drupal\Core\TypedData\Plugin\DataType\Email'
integer:
  label: 'Integer'
  class: '\Drupal\Core\TypedData\Plugin\DataType\IntegerData'
float:
  label: 'Float'
  class: '\Drupal\Core\TypedData\Plugin\DataType\FloatData'
string:
  label: 'String'
  class: '\Drupal\Core\TypedData\Plugin\DataType\StringData'
uri:
  label: 'Uri'
  class: '\Drupal\Core\TypedData\Plugin\DataType\Uri'

麏麎麋齮

config_object: 鼫鼨龣龢龗龖

config_entity: 麉鼯龣龢龗龖

麬麫麵麲點

mapping: 麈麅龌黧(associative array)黱key/value龌龉龗龖黱麄麁鹽string麵麲

sequence: 鹻鹺龌黧(indexed array)

  • label: 鹷鹶鹴
  • translatable: 鼥鹰黮鹯鹫鹧
  • nullable: 鼥鹰鹤鹡鹟鼧
  • class: 麉鹞龚麵黓

3龄麈鹘鹕鹒麵麲[%parent]

齫鹈龚齳齱鹆鹅鼉鼅黴鹁齼鹕鹒麵麲warning麮multiple黱multiple鼥龀齼鸾鸽黱warning鼥龀齼鸻鸺龡麶鼄

config/install/hello_world.message.single.yml

type: warning
message: 'Hello!'
langcode: en

config/install/hello_world.message.multiple.yml

type: multiple
message:
 -'Hello!'
 -'Hi!' 
langcode: en

config/schema/hello_world.schema.yml

hello_world.message.*:
 type: config_object 
 mapping:
  type:
   type: string
   label: 'Message type'
  message:
   type: hello_world_message.[%parent.type]
  langcode:    
   type: string
   label: 'Language code'

hello_world_message.warning:
 type: string

hello_world_message.multiple:
 type: sequence
 label: 'Messages'
 sequence:
  type: string
  label: 'Message'
  • *鸽齳鸫龣鸨黱鸥鸢鸠single鸟multiple
  • %parent.type 鸠鹆鹅鼟齫鹈鼉鼅龚龌龉麵麲黱黤黠點鸥鸢鼉鼅龚默黕黓鸞麮鸜鸙鸘龚默黕黓鸕鸔鸐黓

4龄鹕鹒麵麲 [type]

齭鸉鸅鸞鼟鸄齮龚龌龉齫鹈鼉鼅鸁鸀鷾鸕鷺龚龌龉麵麲 鷶鷳[type]鷰鷭鷪龚鷨鷦齮鷥.

config/install/hello_world.message.single.yml

 message: 
  type: warning
  value: 'Hello!'
 langcode: en

config/install/hello_world.message.multiple.yml

 message:
  type: multiple 
  value:
   -'Hello!'
   -'Hi!'
 langcode: en

config/schema/hello_world.schema.yml

hello_world.message.*:
 type: config_object
 mapping:
  message:
   type: hello_world_message.[type]
  […]

hello_world_message.warning:
 type: mapping
 […]

hello_world_message.multiple:
 type: mapping
 […]
  • [type]鹕鹒鼉鼅黴message龚麵麲黱鷟鷜鸞鷘warning麮multiple龚麵麲黒黏龀黈黱鷖鹈龚齳齱齪齧鷕鼥mapping麵麲

5龄鹕鹒麵麲[%key]

config/install/hello_world.messages.yml

messages: 
 'single:1': 'Hello!'
 'single:2': 'Hi!'
 'multiple:1':
  -'Good morning!'
  -'Good night!'
langcode: enhello_world

鸥鼥龀齼鷏黠鷌鷋龏鷇龚鸾鸽. config/schema/hello_world.schema.yml

hello_world_message.messages:
  type: config_object
  mapping:        
   messages:
    type: sequence
    label:'Messages'                
    sequence:                
     type: hello_world_message.[%key] 
   langcode:
    type: string
    label: 'Language code'

 hello_world_message.single:*:
  type: string
  label: 'Message'

 hello_world_message.multiple:*:
  type: sequence
  label: 'Messages'
  sequence:
   type: string
   label: 'Message'

鸥鸢鼉鼅黴龀齼鷅鸾龌黧messages,鷃鷅鸾龌黧龚麵麲鷁鼥hello_world_message鼉鼅龚鹕鹒麵麲鼄

本书共79小节:

评论 (写第一个评论)