The BlockForm is "embedding" the block plugin's configuration form under 'settings' key in the form() method via:
$form['settings'] = $entity->getPlugin()->buildConfigurationForm(array(), $form_state);
This is correct. But in the validateForm() method it completely ignores errors set by the block plugin. It will only take care of the values. The code is:
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
// The Block Entity form puts all block plugin form elements in the
// settings form element, so just pass that to the block for validation.
$settings = (new FormState())->setValues($form_state->getValue('settings'));
// Call the plugin validate handler.
$this->entity->getPlugin()->validateConfigurationForm($form, $settings);
// Update the original form values.
$form_state->setValue('settings', $settings->getValues());
$this->validateVisibility($form, $form_state);
}
whereas it should be:
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
// The Block Entity form puts all block plugin form elements in the
// settings form element, so just pass that to the block for validation.
$settings = (new FormState())->setValues($form_state->getValue('settings'));
// Call the plugin validate handler.
$this->entity->getPlugin()->validateConfigurationForm($form['settings'], $settings);
// Update the original form values.
$form_state->setValue('settings', $settings->getValues());
$errors = $settings->getErrors();
if (!empty($errors)) {
foreach ($errors AS $name => $message) {
$form_state->setErrorByName($name, $message);
}
}
$this->validateVisibility($form, $form_state);
}
So currently there is no way to properly in/validate settings form for block plugins. And I even think this bug is present in any implementation of this sort(ie. embedding forms within different forms, especially when it comes to plugins, this is not unusual).
Comments
Comment #1
Anonymous (not verified) commentedivanjaros created an issue. See original summary.
Comment #2
berdirLikely a duplicate of #2537732: PluginFormInterface must have access to the complete $form_state (introduce SubFormState for embedded forms)?
Comment #3
Anonymous (not verified) commentedMight be, sound like if that implementation will get into core this piece of code will get rewritten anyway so it would fix it. So I will close this one and add comment about validation into that story.