跳转到主要内容
learningxm 提交于 30 November 2012

drupal8表单定义的变化:

扩展基础表单类EntityFormControllerNG来自定义表单

别忘了定义位置和调用核心类库

<?php /** * @file * Definition of Drupal\entity_test\EntityTestFormController. */ namespace Drupal\entity_test; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityFormControllerNG; class EntityTestFormController extends EntityFormControllerNG { public function form(array $form, array &$form_state, EntityInterface $entity) { //--继承父表单 $form = parent::form($form, $form_state, $entity); //--相关语言的扩展 $langcode = $this->getFormLangcode($form_state); $translation = $entity->getTranslation($langcode); $form['name'] = array( '#type' => 'textfield', '#title' => t('Name'), '#default_value' => $translation->name->value, '#size' => 60, '#maxlength' => 128, '#required' => TRUE, '#weight' => -10, ); $form['user_id'] = array( '#type' => 'textfield', '#title' => 'UID', '#default_value' => $translation->user_id->value, '#size' => 60, '#maxlength' => 128, '#required' => TRUE, '#weight' => -10, ); $form['langcode'] = array( '#title' => t('Language'), '#type' => 'language_select', '#default_value' => $entity->language()->langcode, '#languages' => LANGUAGE_ALL, ); return $form; } } ?>

调用显示表单的方法:

 

<?php /** * Menu callback: displays the 'Add new entity_test' form. * * @return array * The processed form for a new entity_test. * * @see entity_test_menu() */ function entity_test_add() { drupal_set_title(t('Create an entity_test')); $entity = entity_create('entity_test', array()); return entity_get_form($entity); } ?>