Skip to content

Commit

Permalink
Merge pull request #130 from openeuropa/OEL-1525
Browse files Browse the repository at this point in the history
OEL-1525: Uninitialized config keys in SearchBlock
  • Loading branch information
donquixote authored May 10, 2022
2 parents 267bc37 + abb0d87 commit 735f07c
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions modules/oe_whitelabel_search/src/Plugin/Block/SearchBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ public static function create(ContainerInterface $container, array $configuratio
);
}

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
// Most of these placeholder values must be overwritten in the block
// creation form.
return [
'form' => [
'action' => '',
],
'input' => [
'name' => '',
'label' => '',
'classes' => '',
'placeholder' => $this->t('Search'),
],
'button' => [
'classes' => '',
],
'view_options' => [
'enable_autocomplete' => FALSE,
'id' => '',
'display' => '',
],
];
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -130,7 +157,7 @@ public function blockForm($form, FormStateInterface $form_state): array {
'#type' => 'textfield',
'#title' => $this->t('Input placeholder text'),
'#description' => $this->t('The placeholder that will be shown inside the input field.'),
'#default_value' => $config['input']['placeholder'] ?? $this->t('Search'),
'#default_value' => $config['input']['placeholder'],
];
$form['button'] = [
'#type' => 'details',
Expand All @@ -148,7 +175,7 @@ public function blockForm($form, FormStateInterface $form_state): array {
$form['enable_autocomplete'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable autocomplete'),
'#default_value' => $config['view_options']['enable_autocomplete'] ?? 0,
'#default_value' => $config['view_options']['enable_autocomplete'],
];

if (!$this->moduleHandler->moduleExists('views') || !$this->moduleHandler->moduleExists('search_api_autocomplete')) {
Expand All @@ -162,22 +189,28 @@ public function blockForm($form, FormStateInterface $form_state): array {
'#type' => 'textfield',
'#title' => $this->t('View id'),
'#description' => $this->t('The view id will be the machine name for the view.'),
'#default_value' => $config['view_options']['id'] ?? '',
'#default_value' => $config['view_options']['id'],
'#states' => [
'visible' => [
':input[name="settings[enable_autocomplete]"]' => ['checked' => TRUE],
],
'required' => [
':input[name="settings[enable_autocomplete]"]' => ['checked' => TRUE],
],
],
];
$form['view_display'] = [
'#type' => 'textfield',
'#title' => $this->t('View display'),
'#description' => $this->t('The view display will be the machine name of the views display.'),
'#default_value' => $config['view_options']['display'] ?? '',
'#default_value' => $config['view_options']['display'],
'#states' => [
'visible' => [
':input[name="settings[enable_autocomplete]"]' => ['checked' => TRUE],
],
'required' => [
':input[name="settings[enable_autocomplete]"]' => ['checked' => TRUE],
],
],
];

Expand Down Expand Up @@ -240,6 +273,7 @@ public function blockValidate($form, FormStateInterface $form_state): void {

if (!$view) {
$form_state->setErrorByName('view_id', $this->t('View id was not found.'));
return;
}

if (!$view->getDisplay($values['view_display'])) {
Expand Down

0 comments on commit 735f07c

Please sign in to comment.