Definisanje bloka u Drupal modulu
Deo V iz Praktičnog vodiča za kreiranje osnovnih Drupal 8 modula
Od .info do testova, samo osnove
Setite se, na početku ovog časa sam rekao da ćemo definisati blok sa formom? Pa, sada je pravo vreme da to razjasnimo.
/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 blok forma */ class LoremIpsumBlockForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'loremipsum_block_form'; }
Ovaj fajl je veoma sličan fajlu za podešavanja, osim što proširuje klasu FormBase.
Takođe ima metod buildForm() otprilike ovako:
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Koliko pasusa? // $options = new array(); $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?'), ]; // Koliko fraza? $form['phrases'] = [ '#type' => 'textfield', '#title' => $this->t('Phrases'), '#default_value' => '20', '#description' => $this->t('Maximum per paragraph'), ]; // Potvrdi. $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Generate'), ]; return $form; }
Ovaj metod se koristi da stavi formu u blok, pomoću koje korisnici podešavaju koliko lažnog teksta žele da generišu.
Ne zaboravite ni metode validateForm() i 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'), ]); } }
A sada za sam blok.
<?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; /** * Pruža Lorem ipsum blok sa kojim možete generisati lažni tekst bilo gde. * * @Block( * id = "loremipsum_block", * admin_label = @Translation("Lorem ipsum block"), * ) */ class LoremIpsumBlock extends BlockBase { /** * {@inheritdoc} */ public function build() { // Vraća formu iz Form/LoremIpsumBlockForm.php. return \Drupal::formBuilder()->getForm('Drupal\loremipsum\Form\LoremIpsumBlockForm'); }
Klasa LoremIpsumBlock proširuje BlockBase i kao takva ima četiri metode koje treba implementirati: build(), blockAccess(), blockForm() i blockSubmit(). Prvi samo prikazuje formu definisanu u našem prethodnom koraku.
Zatim se bavimo kontrolom pristupa:
/** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { return AccessResult::allowedIfHasPermission($account, 'generate lorem ipsum'); }
Definišite našu blok formu za administratorski ekran bloka:
/** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { $form = parent::blockForm($form, $form_state); $config = $this->getConfiguration(); return $form; }
Handler za slanje forme:
/** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->setConfigurationValue('loremipsum_block_settings', $form_state->getValue('loremipsum_block_settings')); } }
Sada naš blok treba da radi.
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.