2.2 创建表单

Drupal 8 表单

龵龲龰龭龫龩龧龣龢龠龞龚龘龕龑龍龉龇龅龄龃齿齾龚龘齽齼齹齷齳齰齬齨齦齿 API 齞齚齗齕齓齒齐齎齊齈 齆齕齄齾齓齒齐齎齂齁鼽龃齿齿鼻鼺龉鼶齒鼵鼱龲龚龘鼰齾齗齕鼮鼫鼩鼦鼢齈

鼡鼟龉齾鼝鼛鼘鼔鼒鼎齾齓齒齐齎鼋鼈鼇

  • 龃齿 blockForm() 鼝黣黡點黜齾 $form 黒黐齞齚鼋鼈齈
  • 龃齿 blockSubmit() 鼝黣黅黂龞齐齎點黜齾黒黀齈
  • 龃齿 defaultConfiguration() 鼝黣麫麨麧麣齾齓齒麠齈

为表单创建个区块

麞麛齽龰龭龢龠齗齕麗麓齆齕齐齎齾龚龘鼇

  • 龢龠鼮麐 block_example/src/Plugin/Block/ExampleConfigurableTextBlock.php
  • 鹮鹭鼡鼟鹫鹩齽鹨鹧鹤鼰鹣龚
  • 鹠鹞鹛黂齽鹘鹕龚龘齈
<?php

/** 
 * @file 
 * Contains \Drupal\block_example\Plugin\Block\ExampleConfigurableTextBlock. 
 */

namespace Drupal\block_example\Plugin\Block;

use Drupal\Core\Block\Annotation\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'Example: configurable text string' block.
 *
 * Drupal\block\BlockBase gives us a very useful set of basic functionality for
 * this configurable block. We can just fill in a few of the blanks with
 * defaultConfiguration(), blockForm(), blockSubmit(), and build().
 *
 * @Block(
 *   id = "example_configurable_text",
 *   admin_label = @Translation("Title of first block (example_configurable_text)"),
 *   category = @Translation("Example")
 * )
 */
class ExampleConfigurableTextBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'block_example_string' => $this->t('A default value. This block was created at %time', ['%time' => date('c')]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form['block_example_string_text'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Block contents'),
      '#size' => 60,
      '#description' => $this->t('This text will appear in the example block.'),
      '#default_value' => $this->configuration['block_example_string'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['block_example_string']
      = $form_state->getValue('block_example_string_text');
  }

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

}

理解新特征和函数

  • 鹓鹒鹑龑鹎黒
    Drupal 8 鼰鹇鹆鹫鹩鹂鹁龇龧齾鸾麗麓齈鸺鸹鸵鸴鸰黒鹎黒鸯鸬鸨鸧龲鹑龑鸥鸡鼰鸟齼鸝鸚鸖龲鸔鸒齈
    鸑鸎鸯鸌鸊鸇 Drupal 7 鸃齨鸁點龃齿 l() t() 齆齨齾鹎黒齈
    龵龲鷼鷹鷶 鷳鷲鷮鷬鷩dependency injection鷢 齾鷟鷛黡鸔鷚鷗鷓龧齽齆齕龲鷏龉齾鷍鷌鷊鷉鷅齈
  • 鷄鷂鶾鸔
    鸴鸰黒黐麐鷩鶺鶹龚龘鶵鶲鶮鶬鷢鹂鷊龃齿鷄鷂鶾鸔齆鶫鶩鼛齈
    鶾鸔鸚鸖齊鶧鸰鸇 t() 齆齨齾鷼齿鶤龧齽鶢鼡鹁龇龧齾鶟鶛鶙鶾鸔齈龰龭龢龠龘齾齹齷鶛鶙齊 BlockBase 齆齕鶾鸔齽鶖鶒麫麨齊鸔鶏 Drupal 7 齾 t() 鼝黣齈
  • Traits
    Traits 鼫鶌鶋鶒齼龰龭 Drupal 7 龃齿齾 include齽鶖鷚鷗龧龣鸝麗麓龲鶊鶉鸔鶅齾鼒鼎鼝黣齈
    鷼鷹龲 BlockBase 鶅麗麓 StringTranslationTrait 齽龰龭龇鼡龃齿鶖齾鶊鶉鼝黣齽鶒鶹鶁齆鶀鼝黣龲 BlockBase 鶅齗齨齈
    $this->t() 麫麨龲 StringTranslationTrait 鼰齈 BlockBase 鶛鶙齊 ContextAwarePluginBase ContextAwarePluginBase 龃齿齊 StringTranslationTrait。 鶢鼡 $this->t() 鸝鵶鷬鵴齊 BlockBase 鼰齈

创建表单

龵龲龢龠齕鵰齎齾齐齎齈鸯鵯齾齼 Examples 鵮鵬麗麓齐齎齾鶺鵪齽龰龭齦齿鶖鸒龉齾 page_example 鶩龘龢龠鼟齈

龢龠齾齐齎麗麓齗齕 email 鼩鵧齽齆齼 Drupal 8 齄鵤齚齾齐齎鼋鼈齈
麧麣 Drupal 8 鷊鵣鵢鵟鷬齾麠齼鵛齼 email 鵙鵕齽鹫鹩鵔龃齿齊鵣鵢鹎黒鵑黅鵟鷬齾 email 齼 .com 齾齈
鵟鷬鵏鵎鷊鼶齒齕齐齎鵏鵎齽鵍鵑鷊鵟鵌齗鵋鷚鼱鵊鵆齈鹫鹩鹨鹧鹤鼰鹣龚齈

齆鵧鹫鹩鵄 Drupal 7 龢龠齐齎鶧鵁鶏齽鴽齼龃齿齊龉黡鴻鴷齾鼝鼛齈齆齼鸑鴵 Drupal 8 齾齐齎 API 鴲齊鵤齚齄鼋鼈鵄鴯齊鸔鸚鸖鼡鼎鶾鼫鵮鴭齈

PageExampleForm 鶅齾鹫鹩鴪鼟龉鴦麐鴢黐鴡鼇

  • 鴵 PageExampleForm 鸔鼶齒齊鴟鼩鹞鴜齽鶛鶙齊鴘鴔 FormBase 鸔齈
  • 鴓龵齊 4 齕鼝黣齽3 齕鵄 Drupal 7 鸔鶏齈
    • getFormId() 齼齗齕齄齾鶵鴏鴌麫麨齾鼝黣齽鴽齼鴊鴇齐齎鶤鶬鴟齈
    • buildForm() 鵔齼鴏鴌麫麨齾齽鶖鴊鴇齗齕鴃鳿黒黐齽鸔鶏 Drupal 7 齾 hook_form 齈
    • validateForm() 齼龇鳺齾齽鳶鳵鵣鵢齽鸔鶏 Drupal 7 齾 hook_form_validate
    • submitForm() 鳲鳵鷚鳮鼘鼔齽鸔鶏 Drupal 7 齾 hook_form_submit
<?php

/** 
 * @file 
 * Contains \Drupal\page_example\Form\PageExampleForm. 
 */ 

namespace Drupal\page_example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class PageExampleForm extends FormBase {

  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'page_example_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['email'] = [
      '#type' => 'email',
      '#title' => $this->t('Your .com email address.')
    ];
    $form['show'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    ];
    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) {
    drupal_set_message($this->t('Your email address is @email', ['@email' => $form_state->getValue('email')]));
  }
}

定义路由

鴵齊龃齿齐齎齽鳪鴪齼鴏鴌麫麨齾齽龰龭鳧齚鵴鳦鳤齾 page_example.routing.yml 鼮麐鼰齈鹫鹩鹨鹧鹤鼰鹣龚齈

龲 defaults 鼟龃齿 _form 鳢鳟 _controller 齽鳛麫鳚鳘齼齗齕 Form 鸔齈

page_example_form:  
  path: '/examples/page_example/form'
  defaults:
    _form: '\Drupal\page_example\Form\PageExampleForm'
    _title: 'Demo Form'
  requirements:
    _permission: 'access simple page'

鹠鹞鹛黂齽鳖鳒 http://mysite.com/examples/page_example/form 齽鼽鷊鹕鵴齆齕齐齎齈

鶹鳎鼽鳋鳉 drupal_get_form() 齽龇龧鷊鳈齬齨鸇 Drupal 7 鸃齨齚鳄齗齕齐齎鳀鲽

Drupal 8 鶅龃齿鹑龑鸔齽鼽鷊齿鵴 formBuilder() 鼝黣鼇

$form = \Drupal::formBuilder()->getForm('Drupal\demo\Form\DemoForm');

齆齕 $form 鴊鴇齾齼龇鴃鳿齾黒黐齈

表单

评论 (写第一个评论)