9.8.2. Drupal में मल्टी-स्टेप पॉपअप फॉर्म।
इस लेख में हम Drupal 8 में Form API को समझना जारी रखेंगे और एक मल्टी-स्टेप फॉर्म बनाएंगे। हम पहले ही मॉड्यूल के लिए सामान्य कॉन्फ़िगरेशन फॉर्म बना चुके हैं, मल्टी-स्टेप फॉर्म उसी तरह बनाया जाता है, जिसमें $form_state का उपयोग डेटा को फॉर्म स्टेप्स के बीच स्टोर करने के लिए किया जाता है।
कोड के उदाहरण आप github पर देख सकते हैं:
https://github.com/levmyshkin/drupalbook8
एक मल्टी-स्टेप फॉर्म के लिए आपको एक फॉर्म क्लास जोड़ने की आवश्यकता होती है:
/modules/custom/drupalbook/src/Form/MultiStepForm.php
<?php
/**
* @file
* Contains Drupal\drupalbook\Form\MultiStepForm.
*/
namespace Drupal\drupalbook\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class MultiStepForm extends FormBase
{
protected $step = 1;
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames()
{
}
/**
* {@inheritdoc}
*/
public function getFormID()
{
return 'multi_step_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
//$form = parent::buildForm($form, $form_state);
// Add a wrapper div that will be used by the Form API to update the form using AJAX
$form['#prefix'] = '<div id="ajax_form_multistep_form">';
$form['#suffix'] = '</div>';
if ($this->step == 1) {
$form['message-step'] = [
'#markup' => '<div class="step">' . $this->t('Step 1 of 2') . '</div>',
];
$form['message-title'] = [
'#markup' => '<h2>' . $this->t('Who are you?') . '</h2>',
];
$form['first_name'] = [
'#type' => 'textfield',
'#title' => $this->t('First name'),
'#placeholder' => $this->t('First name'),
'#required' => TRUE,
];
$form['last_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Last name'),
'#placeholder' => $this->t('Last name'),
'#required' => TRUE,
];
}
if ($this->step == 2) {
$form['message-step'] = [
'#markup' => '<div class="step">' . $this->t('Step 2 of 2') . '</div>',
];
$form['message-title'] = [
'#markup' => '<h2>' . $this->t('Please enter your contact details below:') . '</h2>',
];
$form['phone'] = [
'#type' => 'textfield',
'#title' => $this->t('Phone'),
'#placeholder' => $this->t('Phone'),
'#required' => TRUE,
];
$form['email'] = [
'#type' => 'email',
'#title' => $this->t('Email address'),
'#placeholder' => $this->t('Email address'),
'#attributes' => array('class' => array('mail-first-step')),
'#required' => TRUE,
];
$form['subscribe'] = [
'#type' => 'checkbox',
'#title' => $this->t('Subscribe to newsletter'),
];
$form['agree'] = [
'#markup' => '<p class="agree">' . $this->t(' By signing up you agree to the <a href="@terms">Terms and Conditions</a> and <a href="@policy">Privacy Policy</a>',
array('@terms' => '/terms-and-conditions', '@policy' => '/privacy-policy')) . '</p>',
];
}
if ($this->step == 3) {
$form['message-step'] = [
'#markup' => '<p class="complete">' . $this->t('- Complete -') . '</p>',
];
$form['message-title'] = [
'#markup' => '<h2>' . $this->t('Thank you') . '</h2>',
];
}
if ($this->step == 1) {
$form['buttons']['forward'] = array(
'#type' => 'submit',
'#value' => t('Next'),
'#prefix' => '<div class="step1-button">',
'#suffix' => '</div>',
'#ajax' => array(
// We pass in the wrapper we created at the start of the form
'wrapper' => 'ajax_form_multistep_form',
// We pass a callback function we will use later to render the form for the user
'callback' => '::ajax_form_multistep_form_ajax_callback',
'event' => 'click',
),
);
}
if ($this->step == 2) {
$form['buttons']['forward'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#ajax' => array(
// We pass in the wrapper we created at the start of the form
'wrapper' => 'ajax_form_multistep_form',
// We pass a callback function we will use later to render the form for the user
'callback' => '::ajax_form_multistep_form_ajax_callback',
'event' => 'click',
),
);
}
$form['#attached']['library'][] = 'drupalbook/multistep_form';
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
return parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->step == 2) {
$values = $form_state->getValues();
$email = $values['email'];
// Save data or send email here.
}
$this->step++;
$form_state->setRebuild();
}
public function ajax_form_multistep_form_ajax_callback(array &$form, FormStateInterface $form_state) {
return $form;
}
}
अगला, हम समझेंगे कि इन पंक्तियों में से प्रत्येक क्या करती है। इस बीच, आइए हम अपने फॉर्म के लिए एक रूट बनाते हैं:
/modules/custom/drupalbook/drupalbook.routing.yml:
drupalbook.multistep_form:
path: '/multistep-form'
defaults:
_form: '\Drupal\drupalbook\Form\MultiStepForm'
_title: 'Subscribe to newsletter'
requirements:
_permission: 'access content'
यह /multistep-form पर एक फॉर्म बनाएगा:
इस फॉर्म को एक मोडल पॉप-अप विंडो में खोलने के लिए, आपको एक ब्लॉक या केवल टेक्स्ट जोड़ने की आवश्यकता है जिसमें एक विशिष्ट use-ajax क्लास और data-dialog-type="modal" एट्रिब्यूट हो, जिसमें फॉर्म पेज के लिए लिंक हो। आपको निम्नलिखित लाइब्रेरीज़ को भी सक्षम करना होगा: core/drupal.dialog.ajax, core/jquery.form। यदि आप इसे ब्लॉक के माध्यम से करते हैं, तो आपको पॉपअप में फॉर्म खोलने के लिए निम्नलिखित कोड मिलेगा:modules/custom/drupalbook/src/Plugin/Block/SubscribeFormButton.php
<?php
namespace Drupal\drupalbook\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a button subscribe to newsletter.
*
* @Block(
* id = "drupalbook_subsribe_form_button",
* admin_label = @Translation("Subscribe to Newsletter"),
* )
*/
class SubscribeFormButton extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$text = '<a href="/multistep-form" class="use-ajax" data-dialog-type="modal">Subscribe</a>';
return [
'#markup' => $text,
'#attached' => array(
'library' => array(
'core/drupal.dialog.ajax',
'core/jquery.form',
),
),
];
}
}
पॉपअप में फॉर्म इस तरह दिखाई देगा:
मैंने drupalbook.libraries.yml फाइल से एक कस्टम लाइब्रेरी भी जोड़ी है:
modules/custom/drupalbook/drupalbook.libraries.yml
multistep_form:
css:
css/multistep_form.css: {}
js:
scripts/multistep_form.js: {}
dependencies:
- core/jquery
- core/jquery.once
फॉर्म को स्टाइल करने और उसमें आवश्यक jQuery प्लगइन्स जोड़ने की आवश्यकता है। इसलिए, आपको और भी फाइलें बनानी होंगी:
modules/custom/drupalbook/css/multistep_form.css
modules/custom/drupalbook/scripts/multistep_form.js
कोड के उदाहरण आप github पर देख सकते हैं:
https://github.com/levmyshkin/drupalbook8