Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OEL-1525: Uninitialized config keys in SearchBlock #130

Merged
merged 3 commits into from
May 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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