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

评论 (写第一个评论)