BlockFieldTestValidationBlock.php in Block field 8
File
tests/modules/block_field_test/src/Plugin/Block/BlockFieldTestValidationBlock.php
View source
<?php
namespace Drupal\block_field_test\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
class BlockFieldTestValidationBlock extends BlockBase {
public function defaultConfiguration() {
return [
'content' => '',
];
}
public function blockForm($form, FormStateInterface $form_state) {
$form['content'] = [
'#type' => 'textfield',
'#title' => $this
->t('Content'),
'#default_value' => $this->configuration['content'],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['content'] = $form_state
->getValue('content');
}
public function blockValidate($form, FormStateInterface $form_state) {
if ($form_state
->getValue('content') == 'error by name') {
$form_state
->setErrorByName('content', 'Come ere boi!');
}
if ($form_state
->getValue('content') == 'error by element') {
$form_state
->setError($form['content'], 'Gimmie them toez!');
}
}
public function build() {
return [
'#type' => 'markup',
'#markup' => $this->configuration['content'],
];
}
}