Drupal मॉड्यूल में ब्लॉक की परिभाषा
Часть V из Практического руководства по созданию базовых модулей Drupal 8
От .info к тестам, только основы
याद है, इस урока की शुरुआत में मैंने कहा था कि हम форму वाले блока को определим? तो, अब समय है इसे विस्तार से देखने का।
/src/Form/LoremIpsumBlockForm.php
<?php namespace Drupal\loremipsum\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; /** * Lorem Ipsum block form */ class LoremIpsumBlockForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'loremipsum_block_form'; }
यह файл settings файла जैसा ही है, फर्क सिर्फ इतना है कि यह FormBase класс को расширяет करता है।
इसमें buildForm() метод भी है, примерно так:
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // कितने पैराग्राफ? $options = array_combine(range(1, 10), range(1, 10)); $form['paragraphs'] = [ '#type' => 'select', '#title' => $this->t('Paragraphs'), '#options' => $options, '#default_value' => 4, '#description' => $this->t('How many?'), ]; // कितने phrases? $form['phrases'] = [ '#type' => 'textfield', '#title' => $this->t('Phrases'), '#default_value' => '20', '#description' => $this->t('Maximum per paragraph'), ]; // Submit. $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Generate'), ]; return $form; }
यह метод блок के अंदर форму डालने के लिए इस्तेमाल किया जाता है, जिससे пользователи तय कर सकते हैं कि वे कितना dummy text генерировать करना चाहते हैं।
validateForm() और submitForm() методы भी जोड़ना न भूलें:
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $phrases = $form_state->getValue('phrases'); if (!is_numeric($phrases)) { $form_state->setErrorByName('phrases', $this->t('Please use a number.')); } if (floor($phrases) != $phrases) { $form_state->setErrorByName('phrases', $this->t('No decimals, please.')); } if ($phrases < 1) { $form_state->setErrorByName('phrases', $this->t('Please use a number greater than zero.')); } } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_state->setRedirect('loremipsum.generate', [ 'paragraphs' => $form_state->getValue('paragraphs'), 'phrases' => $form_state->getValue('phrases'), ]); } }
और अब сам блок:
<?php namespace Drupal\loremipsum\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; /** * Provides a Lorem ipsum block with which you can generate dummy text anywhere. * * @Block( * id = "loremipsum_block", * admin_label = @Translation("Lorem ipsum block"), * ) */ class LoremIpsumBlock extends BlockBase { /** * {@inheritdoc} */ public function build() { // Return the form @ Form/LoremIpsumBlockForm.php. return \Drupal::formBuilder()->getForm('Drupal\loremipsum\Form\LoremIpsumBlockForm'); }
कласс LoremIpsumBlock, BlockBase को расширяет करता है और इसमें चार методы होने चाहिए: build(), blockAccess(), blockForm() и blockSubmit(). पहला सिर्फ हमारे पिछले шаге определенной формы को रेंडर करता है।
अब access control:
/** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { return AccessResult::allowedIfHasPermission($account, 'generate lorem ipsum'); }
Admin экрана для блока формы определяем:
/** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { $form = parent::blockForm($form, $form_state); $config = $this->getConfiguration(); return $form; }
Submit handler:
/** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->setConfigurationValue('loremipsum_block_settings', $form_state->getValue('loremipsum_block_settings')); } }
अब हमारा блок काम करना चाहिए।