GoogleCSESearchBoxForm.php in Google Custom Search Engine 8.3
File
src/Form/GoogleCSESearchBoxForm.php
View source
<?php
namespace Drupal\google_cse\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\google_cse\GoogleCSEServices;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class GoogleCSESearchBoxForm extends FormBase {
protected $googleCSEServices;
protected $requestStack;
public function __construct(GoogleCSEServices $googleCSEServices, RequestStack $requestStack) {
$this->googleCSEServices = $googleCSEServices;
$this->requestStack = $requestStack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('google_cse.services'), $container
->get('request_stack'));
}
public function getFormId() {
return 'google_cse_search_box_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('search.page.google_cse_search');
if ($config
->get('configuration')['results_display'] == 'here') {
$cof = $config
->get('configuration')['cof_here'];
}
else {
$form['#action'] = $this->googleCSEServices
->getProtocol() . $config
->get('configuration')['domain'] . '/cse';
$cof = $config
->get('configuration')['cof_google'];
}
$form['#method'] = 'get';
$form['cx'] = [
'#type' => 'hidden',
'#value' => $config
->get('configuration')['cx'],
];
$form['cof'] = [
'#type' => 'hidden',
'#value' => $cof,
];
$form['keys'] = [
'#type' => 'textfield',
'#default_value' => $this->requestStack
->getCurrentRequest()->query
->has('query') ? $this->requestStack
->getCurrentRequest()->query
->get('query') : '',
];
$form['sa'] = [
'#type' => 'submit',
'#value' => $this
->t('Search'),
];
foreach ($this->googleCSEServices
->advancedSettings() as $parameter => $setting) {
$form[$parameter] = [
'#type' => 'hidden',
'#value' => $setting,
];
}
$form['keys']['#size'] = intval($config
->get('configuration')['results_searchbox_width']);
$form['keys']['#title'] = $this
->t('Enter your keywords');
$this->googleCSEServices
->siteSearchForm($form);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}