SearchBlockForm.php in Drupal 8
File
core/modules/search/src/Form/SearchBlockForm.php
View source
<?php
namespace Drupal\search\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\search\SearchPageRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SearchBlockForm extends FormBase {
protected $searchPageRepository;
protected $configFactory;
protected $renderer;
public function __construct(SearchPageRepositoryInterface $search_page_repository, ConfigFactoryInterface $config_factory, RendererInterface $renderer) {
$this->searchPageRepository = $search_page_repository;
$this->configFactory = $config_factory;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('search.search_page_repository'), $container
->get('config.factory'), $container
->get('renderer'));
}
public function getFormId() {
return 'search_block_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_id = NULL) {
if (!$entity_id) {
$entity_id = $this->searchPageRepository
->getDefaultSearchPage();
$this->renderer
->addCacheableDependency($form, $this->configFactory
->get('search.settings'));
}
if (!$entity_id) {
$form['message'] = [
'#markup' => $this
->t('Search is currently disabled'),
];
return $form;
}
$route = 'search.view_' . $entity_id;
$form['#action'] = Url::fromRoute($route)
->toString();
$form['#method'] = 'get';
$form['keys'] = [
'#type' => 'search',
'#title' => $this
->t('Search'),
'#title_display' => 'invisible',
'#size' => 15,
'#default_value' => '',
'#attributes' => [
'title' => $this
->t('Enter the terms you wish to search for.'),
],
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Search'),
'#name' => '',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}