2016-09-20 3 views
1

여러 날 동안 나는 다단계 형식 인 Drupal 8 사용자 정의 폼 모듈에서 작업한다.이 작업은 SessionManagerInterface를 사용하여 완성되었지만, 한 페이지에 모든 데이터를 표시하기 전에 어떻게 수행 할 수 있는가? snapshot hereDrupal 8의 미리보기 데이터 채우기 페이지로 다중 단계 양식을 작성하는 방법은 무엇입니까?

demo.routing.yml

demo.multistep_one: 
    path: '/demo/multistep-one' 
    defaults: 
    _form: '\Drupal\demo\Form\Multistep\MultistepOneForm' 
    _title: 'First form' 
    requirements: 
    _permission: 'access content' 
demo.multistep_two: 
    path: '/demo/multistep-two' 
    defaults: 
    _form: '\Drupal\demo\Form\Multistep\MultistepTwoForm' 
    _title: 'Second form' 
    requirements: 
    _permission: 'access content'ent' 

MultistepFormBase.php

namespace Drupal\demo\Form\Multistep; 

use Drupal\Core\Form\FormBase; 
use Drupal\Core\Form\FormStateInterface; 
use Drupal\Core\Session\AccountInterface; 
use Drupal\Core\Session\SessionManagerInterface; 
use Drupal\user\PrivateTempStoreFactory; 
use Symfony\Component\DependencyInjection\ContainerInterface; 

abstract class MultistepFormBase extends FormBase { 

    /** 
    * @var \Drupal\user\PrivateTempStoreFactory 
    */ 
    protected $tempStoreFactory; 

    /** 
    * @var \Drupal\Core\Session\SessionManagerInterface 
    */ 
    private $sessionManager; 

    /** 
    * @var \Drupal\Core\Session\AccountInterface 
    */ 
    private $currentUser; 

    /** 
    * @var \Drupal\user\PrivateTempStore 
    */ 
    protected $store; 

    /** 
    * Constructs a \Drupal\demo\Form\Multistep\MultistepFormBase. 
    * 
    * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory 
    * @param \Drupal\Core\Session\SessionManagerInterface $session_manager 
    * @param \Drupal\Core\Session\AccountInterface $current_user 
    */ 
    public function __construct(PrivateTempStoreFactory $temp_store_factory, SessionManagerInterface $session_manager, AccountInterface $current_user) { 
    $this->tempStoreFactory = $temp_store_factory; 
    $this->sessionManager = $session_manager; 
    $this->currentUser = $current_user; 

    $this->store = $this->tempStoreFactory->get('multistep_data'); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public static function create(ContainerInterface $container) { 
    return new static(
     $container->get('user.private_tempstore'), 
     $container->get('session_manager'), 
     $container->get('current_user') 
    ); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function buildForm(array $form, FormStateInterface $form_state) { 
    // Start a manual session for anonymous users. 
    if ($this->currentUser->isAnonymous() && !isset($_SESSION['multistep_form_holds_session'])) { 
     $_SESSION['multistep_form_holds_session'] = true; 
     $this->sessionManager->start(); 
    } 

    $form = array(); 
    $form['actions']['#type'] = 'actions'; 
    $form['actions']['submit'] = array(
     '#type' => 'submit', 
     '#value' => $this->t('Submit'), 
     '#button_type' => 'primary', 
     '#attributes' => array(
       'class' => array(
        'btn btn-register' 
       ), 
    ), 
); 


    return $form; 
    } 

MultistepOneForm.php 자식 폼

namespace Drupal\demo\Form\Multistep; 

use Drupal\Core\Form\FormStateInterface; 

class MultistepOneForm extends MultistepFormBase { 

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

    /** 
    * {@inheritdoc}. 
    */ 
    public function buildForm(array $form, FormStateInterface $form_state) { 

    $form = parent::buildForm($form, $form_state); 

    $form['fname'] = array(
     '#type' => 'textfield', 
     '#title' => $this->t('Your name'), 
     '#default_value' => $this->store->get('fname') ? $this->store->get('fname') : '', 
     '#attributes' => array(
       'class' => array(
        'form-control' 
        ), 
       ), 

    ); 

    $form['lname'] = array(
     '#type' => 'textfield', 
     '#title' => $this->t('Your Last Name'), 
     '#default_value' => $this->store->get('lname') ? $this->store->get('lname') : '', 
     '#attributes' => array(
       'class' => array(
        'form-control' 
        ), 
       ), 
    $form['actions']['submit']['#value'] = $this->t('Continue'); 

    return $form; 
    } 
    /** 
    * {@inheritdoc} 
    */ 
    public function submitForm(array &$form, FormStateInterface $form_state) { 
    $this->store->set('fname', $form_state->getValue('fname')); 
    $this->store->set('lname', $form_state->getValue('lname')); 
$form_state->setRedirect('demo.multistep_two'); 
} 
} 

MultistepTwoForm.php

여기
namespace Drupal\demo\Form\Multistep; 

use Drupal\Core\Form\FormStateInterface; 
use Drupal\Core\Url; 

class MultistepTwoForm extends MultistepFormBase { 

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

    /** 
    * {@inheritdoc}. 
    */ 
    public function buildForm(array $form, FormStateInterface $form_state) { 

    $form = parent::buildForm($form, $form_state); 

    $form['jfname'] = array(
     '#type' => 'textfield', 
     '#title' => $this->t('Joint Account Holder First Name'), 
     '#attributes' => array(
       'class' => array(
        'form-control' 
        ), 
       ), 
    ); 

    $form['jlname'] = array(
     '#type' => 'textfield', 
     '#title' => $this->t('Joint Account Holder Last Name'), 
     '#attributes' => array(
       'class' => array(
        'form-control' 
        ), 
       ), 
    ); 
$form['actions']['previous'] = array(
     '#type' => 'link', 
     '#title' => $this->t('Previous'), 
     '#url' => Url::fromRoute('demo.multistep_one'), 
     '#suffix' => '</div>', 
     '#attributes' => array(
       'class' => array(
        'btn btn-register' 
        ), 
       ), 

    ); 
$form['actions']['submit']['#value'] = $this->t('Continue'); 
    return $form; 
    } 

public function submitForm(array &$form, FormStateInterface $form_state) { 
    $this->store->set('jfname', $form_state->getValue('jfname')); 
    $this->store->set('jlname', $form_state->getValue('jlname')); 
    // $form_state->setRedirect('demo.multistep_three'); 
    } 
} 

내 관심은 내 snapshot.what 조치는 디스플레이 데이터를주의 할 필요에 따라 모든 데이터를 표시하는 방법 발생합니다. 한 가지는 내가 drupal에 대한 3 개월의 경험을 가지고 있으므로 무엇을 할 것인지 결정을 내릴 수 없다는 것입니다. 이 코드는 www.sitepoint.com의 예이며 형식 3을 취하고 세션 데이터는 라벨에 표시 여부가 좋든 모르 든간에 적절한 방향을 제시합니다. 미리 감사드립니다.

+0

문제는 저자가 이미 시도 (여부) 어떤 진술과 그들이 어디에 어려움이없이 특정 기능의 구현을위한 크고 요청입니다. 화제는 이미 다수 블로그 포스트 및 자습서에서 온라인으로 덮여 명백한 검색 키워드를 사용하여 쉽게 찾을 수 있습니다. –

+0

감사합니다, Pierre, 다중 단계 양식을 완료했습니다 그러나 문제 만 표시 모든 데이터 단일 페이지 제출 전. 완료 방법을 모르십니까? – vinny

+0

질문을 다시 작성하고 작업 코드를 제공하십시오 (가능한 한 간단하게 만들고 실제 물건을 더 쉽게 파악하기 위해 제거하십시오). 그리고 정확히 무엇이 작동하고 무엇이 빠지거나 없는지 지적하십시오. http://stackoverflow.com/help/how-to-ask를 참조하십시오. –

답변

0

설정에 따라 MultistepTwoForm에서 다중 형식을 제출하십시오. 다음과 같이 변경하십시오.

  • 하나 이상의 양식 추가 : MultiStepPreview. 미리보기가 보류됩니다. (

    • 미리보기를 포함하는 폼 요소를 구축 가게
    • 에서 키를 읽을 수 :
    • 는 MultistepTwoForm에서 양식을 제출하지만 MultiStepPreview에서 $form_state->setRedirect()

    사용 MultiStepPreview에 양식을 리디렉션하지 않음 미리보기를 포함 할 html 표를 만들었습니다.)

  • 양식을 제출하십시오.

    class OverviewForm extends MultiStepFormBase { 
        public function buildForm(array $form, FormStateInterface $form_state) { 
        // get data from the store 
        $your_details = $this->store->get('your_details'); 
        $order = $this->store->get('order_details'); 
    
        // make a HTML table containing the data in store 
        $markup = '<table>'; 
        // loop through $your_details and $your_order and put them in $markup 
    
        $form = parent::buildForm($form, $form_state); 
        $form['preview'] = [ 
         '#markup' => $markup, 
        ]; 
        //in my setup all the buttons are build by MultiStepFormBase 
        return $form; 
        } 
    

    }

관련 문제