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 鴗鴓齺齷龈鴒鴎齺黙黗齄

表单

评论 (写第一个评论)