class IndexController in Search API 8
Provides route responses for search indexes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\search_api\Controller\IndexController
Expanded class hierarchy of IndexController
File
- src/
Controller/ IndexController.php, line 20
Namespace
Drupal\search_api\ControllerView source
class IndexController extends ControllerBase {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack|null
*/
protected $requestStack;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface|null
*/
protected $messenger;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var static $controller */
$controller = parent::create($container);
$controller
->setRequestStack($container
->get('request_stack'));
$controller
->setMessenger($container
->get('messenger'));
return $controller;
}
/**
* Retrieves the request stack.
*
* @return \Symfony\Component\HttpFoundation\RequestStack
* The request stack.
*/
public function getRequestStack() {
return $this->requestStack ?: \Drupal::service('request_stack');
}
/**
* Retrieves the current request.
*
* @return \Symfony\Component\HttpFoundation\Request|null
* The current request.
*/
public function getRequest() {
return $this
->getRequestStack()
->getCurrentRequest();
}
/**
* Sets the request stack.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The new request stack.
*
* @return $this
*/
public function setRequestStack(RequestStack $request_stack) {
$this->requestStack = $request_stack;
return $this;
}
/**
* Retrieves the messenger.
*
* @return \Drupal\Core\Messenger\MessengerInterface
* The messenger.
*/
public function getMessenger() {
return $this->messenger ?: \Drupal::service('messenger');
}
/**
* Sets the messenger.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The new messenger.
*
* @return $this
*/
public function setMessenger(MessengerInterface $messenger) {
$this->messenger = $messenger;
return $this;
}
/**
* Displays information about a search index.
*
* @param \Drupal\search_api\IndexInterface $search_api_index
* The index to display.
*
* @return array
* An array suitable for drupal_render().
*/
public function page(IndexInterface $search_api_index) {
// Build the search index information.
$render = [
'view' => [
'#theme' => 'search_api_index',
'#index' => $search_api_index,
],
];
// Check if the index is enabled and can be written to.
if ($search_api_index
->status() && !$search_api_index
->isReadOnly()) {
// Attach the index status form.
$render['form'] = $this
->formBuilder()
->getForm('Drupal\\search_api\\Form\\IndexStatusForm', $search_api_index);
}
return $render;
}
/**
* Returns the page title for an index's "View" tab.
*
* @param \Drupal\search_api\IndexInterface $search_api_index
* The index that is displayed.
*
* @return string
* The page title.
*/
public function pageTitle(IndexInterface $search_api_index) {
return new FormattableMarkup('@title', [
'@title' => $search_api_index
->label(),
]);
}
/**
* Enables a search index without a confirmation form.
*
* @param \Drupal\search_api\IndexInterface $search_api_index
* The index to be enabled.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response to send to the browser.
*/
public function indexBypassEnable(IndexInterface $search_api_index) {
// Enable the index.
$search_api_index
->setStatus(TRUE)
->save();
// \Drupal\search_api\Entity\Index::preSave() doesn't allow an index to be
// enabled if its server is not set or disabled.
if ($search_api_index
->status()) {
// Notify the user about the status change.
$this
->getMessenger()
->addStatus($this
->t('The search index %name has been enabled.', [
'%name' => $search_api_index
->label(),
]));
}
else {
// Notify the user that the status change did not succeed.
$this
->getMessenger()
->addWarning($this
->t('The search index %name could not be enabled. Check if its server is set and enabled.', [
'%name' => $search_api_index
->label(),
]));
}
// Redirect to the index's "View" page.
$url = $search_api_index
->toUrl('canonical');
return $this
->redirect($url
->getRouteName(), $url
->getRouteParameters());
}
/**
* Removes a field from a search index.
*
* @param \Drupal\search_api\IndexInterface $search_api_index
* The search index.
* @param string $field_id
* The ID of the field to remove.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response to send to the browser.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* Thrown when the field was not found.
*/
public function removeField(IndexInterface $search_api_index, $field_id) {
$fields = $search_api_index
->getFields();
$success = FALSE;
if (isset($fields[$field_id])) {
try {
$search_api_index
->removeField($field_id);
$search_api_index
->save();
$success = TRUE;
} catch (SearchApiException $e) {
$args['%field'] = $fields[$field_id]
->getLabel();
$this
->getMessenger()
->addError($this
->t('The field %field is locked and cannot be removed.', $args));
}
}
else {
throw new NotFoundHttpException();
}
// If this is an AJAX request, just remove the row in question.
if ($success && $this
->getRequest()->request
->get(AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER)) {
$response = new AjaxResponse();
$response
->addCommand(new RemoveCommand("tr[data-field-row-id='{$field_id}']"));
return $response;
}
// Redirect to the index's "Fields" page.
$url = $search_api_index
->toUrl('fields');
return $this
->redirect($url
->getRouteName(), $url
->getRouteParameters());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
IndexController:: |
protected | property |
The messenger. Overrides MessengerTrait:: |
|
IndexController:: |
protected | property | The request stack. | |
IndexController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
IndexController:: |
public | function | Retrieves the messenger. | |
IndexController:: |
public | function | Retrieves the current request. | |
IndexController:: |
public | function | Retrieves the request stack. | |
IndexController:: |
public | function | Enables a search index without a confirmation form. | |
IndexController:: |
public | function | Displays information about a search index. | |
IndexController:: |
public | function | Returns the page title for an index's "View" tab. | |
IndexController:: |
public | function | Removes a field from a search index. | |
IndexController:: |
public | function |
Sets the messenger. Overrides MessengerTrait:: |
|
IndexController:: |
public | function | Sets the request stack. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |