public static function BlockFieldWidget::validate in Block field 8
Form element validation handler.
File
- src/
Plugin/ Field/ FieldWidget/ BlockFieldWidget.php, line 225
Class
- BlockFieldWidget
- Plugin implementation of the 'block_field' widget.
Namespace
Drupal\block_field\Plugin\Field\FieldWidgetCode
public static function validate($element, FormStateInterface $form_state, $form) {
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$values = $form_state
->getValues();
$plugin_id = NestedArray::getValue($values, $element['plugin_id']['#parents']);
if (!empty($plugin_id) && $block_manager
->hasDefinition($plugin_id)) {
// Clean up configuration settings.
$settings = NestedArray::getValue($values, $element['settings']['#parents']);
// Convert label display to FALSE instead of 0. This allow the label to be
// hidden.
if ($settings['label_display'] === 0) {
$settings['label_display'] = FALSE;
}
// Execute block validate configuration.
$block_instance = $block_manager
->createInstance($plugin_id, $settings);
$settings = (new FormState())
->setValues($settings);
$block_instance
->validateConfigurationForm($element['settings'], $settings);
// Pass along errors from the block validation.
foreach ($settings
->getErrors() as $key => $error) {
$parents = implode('][', $element['settings']['#parents']);
// If the block form used setError() then the parents will already be
// part of the key since we are passing along the element in the context
// of the whole form. If the block form used setErrorByName we need to
// add the parents in.
if (strpos($key, $parents) === FALSE) {
$key = sprintf('%s][%s', $parents, $key);
}
$form_state
->setErrorByName($key, $error);
}
NestedArray::setValue($values, $element['settings']['#parents'], $settings
->getValues());
$form_state
->setValues($values);
}
else {
// Clear all configuration settings.
NestedArray::setValue($values, $element['settings']['#parents'], []);
}
}