Definition eines Blocks in einem Drupal-Modul
Teil V aus dem Praktischen Leitfaden zur Erstellung grundlegender Drupal 8 Module
Von .info zu Tests, nur die Grundlagen
Erinnern Sie sich, am Anfang dieser Lektion sagte ich, wir würden einen Block mit einem Formular definieren? Nun, jetzt ist die Zeit gekommen, dies anzugehen.
/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 Formular */ class LoremIpsumBlockForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'loremipsum_block_form'; }
Diese Datei ähnelt sehr der Konfigurationsdatei, außer dass sie die Klasse FormBase erweitert.
Sie hat auch die Methode buildForm(), etwa so:
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Wie viele Absätze? $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?'), ]; // Wie viele Phrasen? $form['phrases'] = [ '#type' => 'textfield', '#title' => $this->t('Phrases'), '#default_value' => '20', '#description' => $this->t('Maximum per paragraph'), ]; // Absenden. $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Generate'), ]; return $form; }
Diese Methode wird verwendet, um das Formular im Block anzuzeigen, mit dem Benutzer einstellen können, wie viel Dummy-Text sie generieren möchten.
Vergessen Sie auch nicht die Methoden validateForm() und 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'), ]); } }
Nun zum Block selbst.
<?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; /** * Stellt einen Lorem Ipsum Block bereit, mit dem Sie an beliebiger Stelle Dummy-Text generieren können. * * @Block( * id = "loremipsum_block", * admin_label = @Translation("Lorem ipsum block"), * ) */ class LoremIpsumBlock extends BlockBase { /** * {@inheritdoc} */ public function build() { // Gibt das Formular aus @ Form/LoremIpsumBlockForm.php zurück. return \Drupal::formBuilder()->getForm('Drupal\loremipsum\Form\LoremIpsumBlockForm'); }
Die Klasse LoremIpsumBlock erweitert BlockBase und hat vier Methoden, die implementiert werden sollten: build(), blockAccess(), blockForm() und blockSubmit(). Die erste Methode zeigt einfach das Formular an, das wir im vorherigen Schritt definiert haben.
Als nächstes kümmern wir uns um die Zugriffskontrolle:
/** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { return AccessResult::allowedIfHasPermission($account, 'generate lorem ipsum'); }
Definieren Sie unser Block-Formular für den Administrationsbildschirm des Blocks:
/** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { $form = parent::blockForm($form, $form_state); $config = $this->getConfiguration(); return $form; }
Der Submit-Handler:
/** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $this->setConfigurationValue('loremipsum_block_settings', $form_state->getValue('loremipsum_block_settings')); } }
Jetzt sollte unser Block funktionsfähig sein.
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.