SearchContentForm.php in Open Social 8.3
File
modules/social_features/social_search/src/Form/SearchContentForm.php
View source
<?php
namespace Drupal\social_search\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class SearchContentForm extends FormBase implements ContainerInjectionInterface {
protected $requestStack;
public function __construct(RequestStack $requestStack) {
$this->requestStack = $requestStack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'));
}
public function getFormId() {
return 'search_content_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['search_input_content'] = [
'#type' => 'textfield',
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Search Content'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$search_all_view = 'search_all';
if (empty($form_state
->getValue('search_input_content'))) {
$search_content_page = Url::fromRoute("view.{$search_all_view}.page_no_value");
}
else {
$search_input = Html::escape($form_state
->getValue('search_input_content'));
$search_input = preg_replace('/[\\/]+/', ' ', $search_input);
$search_content_page = Url::fromRoute("view.{$search_all_view}.page", [
'keys' => $search_input,
]);
}
$redirect_path = $search_content_page
->toString();
$query = UrlHelper::filterQueryParameters($this->requestStack
->getCurrentRequest()->query
->all(), [
'page',
]);
$redirect = Url::fromUserInput($redirect_path, [
'query' => $query,
]);
$form_state
->setRedirectUrl($redirect);
}
}