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小节:

评论 (写第一个评论)