2.1 区块与配置

龶龳龲龯龭龬龭龨龧龤龢龟龛龗龔龒龑龎龍龊龉龈龇龉 Drupal 8 齰齯齭齩龟齥龎齢齟龢齛齙齘齖

Drupal 8 齰龟 龢 API 齌龭龍齉齇龢齆齂 鼾龎鼺plugins鼯 鼮齂鼫鼩鼺entities鼯齖

Drupal 8 齰龟龢鼢鼞鼚鼙 API 齥鼗齢

  • 龢鼾龎 API 齢鼓龗鼏鼍鼉鼇齩龟 API
  • 龢鼫鼩 API 齢鼅鼄龢龟鼁鼀齛鼉黼黹黵黴

黱黭黩黧齉齇龢齂鼉黤黠黟龟鼫鼩齛黝齇黛齩默龟鼾龎黔黒鼺 change record here 鼯齖

鼮黭黩黧龶龳鼉黆黅龧默龟黁黀龍麿麾黤黠龍黛齩麺麹麸麴麳龇麯龳齖

齉齇鼓龗齟龢鼉黆麹麫麨麧齇麣龗齟麠鼺regions鼯麞龍黱齇 Drupal 8 麚麖齂麺鼉黆龟齖
鼫麒麐齉齇齂鼉黆黅龧麹鼓龗龢龟麣龗鼫麌龭齖
麊鼮鼉黆麈麄 UI 麀鼓龗龢麹麫鹼齇麺麹龟齟麠龍麺鹺鹹鹶鹲鹮齖

鹭鹪龶龳鹧黛齩 Examples 鹜鹘龟 block_example 龤龢齖

本课学习内容

  • 黅龧 examples/block_example 龤龢
  • 鹋鹇 Drupal 8 齰龟鹅龬
  • 鹃鹀鸿鸾黛齩 Drupal GUI 鸹麾齟龢
  • 鸸鸷 Form API
  • 鹃鹀鸿鸾黛齩 Form API
  • 鸱鸭鸿鸾黅龧鸪鸧齙齘龟齟龢齛鸦鸢

Drupal 8 区块

麺龔黛齩 4 龗鸝鸙黀鸖齟龢龭鼺Drupal 7 齰麊龔鸓黱鸐鸌鼯龍鼾龎黔黒鸋鸇黆龉麞鸃齢

  • 鼓龗齟龢鼓龗鸀龎
  • 鸿鷾麊鷽黅龧默齟龢龍鼉黆鷹龊拷鷵鷳鷯鷬麚麖齟龢鸀龎龍鷨鷦鷤默齟龢齖
  • 黆鷡鷯鷬齟龢鷟龒鼅鼄鼁鷞麺麹鼁鼀龟鷜龗鷙鷖龍鷔黱鷒鷙鷖鼉鷑齇鼞龗鸀龎鷏齖
    齉齇齟龢麞鸃鷋齇鼓龗鸀龎麞齖
  • 鷉鸧鷆鷂鷁齇鶽龗鸀龎
  • 麊麺鷟龒鶼鶻鸾 info 鸀龎鶷鶵麊龟鶴鶲鶯龍鶮鶬麯鹺鶫鶪鶦麾鶥齖
  • 齟龢鶡鶠鶝鶫鶜鶙齇鼓龗鼏鼍鸀龎麞

创建一个简单的区块

Example 鹜鹘鸧龗 block_example 龤龢龍齇 src/Plugin/Block 鸀龎鶔麞鸧 3 龗麌鸙龍鼙鶏鶌麧 3 鶊麺麹齟龢齖
龶龳龇龉齯鶆齘龟鶽龗齢黅龧鼓龗鶄鸧鶻鸾麞鸃龟鶁龢齖

鵽鵺齇 examples 鸀龎鶔麞黅龧默龤龢 block_example鼺黅龧龤龢麸鵶鵲鵮鵭 麐鼓鹪 鼯齖
麚鵪齇 block_example 鹘鵧龉黅龧 src/Plugin/Block 鹘鵧齖
齯鵪齇 Block 鹘鵧黅龧 ExampleEmptyBlock.php 鸀龎龍鹼鼀鵡鵞龍黼鵝鵜麞鸃齟齖

<?php

namespace Drupal\block_example\Plugin\Block;
use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Example: empty block' block.
 *
 * @Block(
 *   id = "example_empty",
 *   admin_label = @Translation("Example: empty block")
 * )
 */
class ExampleEmptyBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#type' => 'markup',
      '#markup' => '',
    ];
  }

}

类和注解

鷉鸧鶴鵙鵘龟麫鵔龔鸓黀鸖鵒黤鶁鵏龍麚鵪龶龳鵎鶌 BlockBase 黅龧鶴 ExampleEmptyBlock 齖齇黱龗鶴鷏龶龳鼉黆黛齩 Drupal 8 鵍鵊龟鹅龬齖

麈齭 PHP 鹅龬齂鼓黠鶪鵈鵆龟鹅鵂鸀鹭龍麯鵁鴾鶴鸀龎鹅鴼鴸鴵齛鴱鷖鴰龟鶪鶦鹹齉齖

麐鸢鴯 1 齥鹅龬齂鴫鴨 Drupal 龶龳龒黅龧鼓龗 id 鶬 example_empty 齛 鴧鼄 label 鶬 鴦Example: empty block鴡龟齟龢鼺@Block鼯齖鴚鷔黱龗鴧鼄 label 齇黛齩麚麖龒龯麄鴖鴓黔黒鼅鼄齖

齇鴑鴏鶴鷙鷖麫鼉黆黛齩 {@inheritdoc} 鹅龬龍麯鴫鴨黔黒黱鷏黛齩鴆鶴龟鹅龬齖

龶龳鼫齉龭鼓龗 build() 麸鵶龍黱齂鼓龗齯鼇龒鷳齭齩龟麸鵶龍麯鹺鳿鳻鼓龗鼉鳷鳵鷖齥鼺齩鷞龢鳴鳰鼯齖

鳯鳬 block_example 龤龢鶫鳫鳩龍鳦鶁鳣鳡齖
齇龢鳝鼀鸦鼺 /admin/structure/block 鼯龟鶻黭齟麠麞鳛鳚鴦鹼鼀齟龢鴡鳖鳒龍鳐鳰龟鳏鳋鳇麞麊鹺龇鷤黱龗默齟龢齖

注意: 只有书写了正确的注解后 Drupal 才能监测到这个块。

评论 (写第一个评论)