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 鴔鴑龇龂龖鴐鴌龇黠黜齓

表单

评论 (写第一个评论)