2017-12-08 13 views
0

イムまま現在のDrupal 8を使用して、私ははdrupal8に小枝テンプレートでフォームをレンダリング

モジュールディレクトリの下に以下のコードは

// module/src/Form/ContributeForm 

    class ContributeForm extends FormBase { 

    public function getFormId() { 
    return 'amazing_forms_contribute_form'; 
    } 

    public function buildForm(array $form, FormStateInterface $form_state) 
    { 
     $form['title'] = array(
      '#type' => 'textfield', 
      '#title' => t('Title'), 
      '#required' => TRUE, 
    ); 
     $form['video'] = array(
      '#type' => 'textfield', 
      '#title' => t('Youtube video'), 
    ); 
     $form['video'] = array(
      '#type' => 'textfield', 
      '#title' => t('Youtube video'), 
    ); 
    $form['develop'] = array(
      '#type' => 'checkbox', 
      '#title' => t('I would like to be involved in developing this 
      material'), 
    ); 
    $form['submit'] = array(
      '#type' => 'submit', 
      '#value' => t('Submit'), 
); 
return $form; 
} 

public function validateForm(array &$form, FormStateInterface $form_state) { 
} 

public function submitForm(array &$form, FormStateInterface $form_state) { 
} 
} 

今、私はvairables上にレンダリングする必要がある、フォームのAPIを使用してフォームを作成しましたtwigテンプレートは以下のようになります

//themes/page.html.twig 

<body> 
    {{form.title}} 
    {{form.video}} 
    {{form.video}} 
</body> 

twigはテーマフォルダの下にあります.page.html.twigファイルで変数を取得できますか?

答えて

1

フォームをレンダリングしてカスタム化するには、フォームに$form['#theme']の値を使用します。例以下:

モジュール名/ SCR /フォーム/ your_form.php

public function buildForm(array $form, FormStateInterface $form_state){ 
     ..... 
     ..... 

     $form['#theme'] = 'your_form_theme'; 

     return $form; 
} 

モジュール名/ form.moduleカスタム小枝を作成し、あなたを挿入されて残っているもの

function form_theme() { 

    $themes['your_form_theme'] = ['render element' => 'form']; 

    return $themes; 
} 

フォームフィールド。

モジュール名/テンプレート/あなた-フォームtheme.html.twig

<body> 
    {{form.field_1}} 
    {{form.field_2}} 
    {{form.field_3}} 
</body> 

それはあなたのお役に立てば幸い!

関連する問題