public function ServerForm::buildEntityForm in Search API 8
Builds the form for the basic server properties.
Parameters
array $form: The current form array.
\Drupal\Core\Form\FormStateInterface $form_state: The current form state.
\Drupal\search_api\ServerInterface $server: The server that is being created or edited.
1 call to ServerForm::buildEntityForm()
- ServerForm::form in src/
Form/ ServerForm.php - Gets the actual form array to be built.
File
- src/
Form/ ServerForm.php, line 105
Class
- ServerForm
- Provides a form for creating and editing search servers.
Namespace
Drupal\search_api\FormCode
public function buildEntityForm(array &$form, FormStateInterface $form_state, ServerInterface $server) {
$form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Server name'),
'#description' => $this
->t('Enter the displayed name for the server.'),
'#default_value' => $server
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $server
->isNew() ? NULL : $server
->id(),
'#maxlength' => 50,
'#required' => TRUE,
'#machine_name' => [
'exists' => '\\Drupal\\search_api\\Entity\\Server::load',
'source' => [
'name',
],
],
'#disabled' => !$server
->isNew(),
];
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enabled'),
'#description' => $this
->t('Only enabled servers can index items or execute searches.'),
'#default_value' => $server
->status(),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#description' => $this
->t('Enter a description for the server.'),
'#default_value' => $server
->getDescription(),
];
$backends = $this->backendPluginManager
->getDefinitions();
$backend_options = [];
$descriptions = [];
foreach ($backends as $backend_id => $definition) {
$config = $backend_id === $server
->getBackendId() ? $server
->getBackendConfig() : [];
$config['#server'] = $server;
try {
/** @var \Drupal\search_api\Backend\BackendInterface $backend */
$backend = $this->backendPluginManager
->createInstance($backend_id, $config);
} catch (PluginException $e) {
continue;
}
if ($backend
->isHidden()) {
continue;
}
$backend_options[$backend_id] = Utility::escapeHtml($backend
->label());
$descriptions[$backend_id]['#description'] = Utility::escapeHtml($backend
->getDescription());
}
asort($backend_options, SORT_NATURAL | SORT_FLAG_CASE);
if ($backend_options) {
if (count($backend_options) == 1) {
$server
->set('backend', key($backend_options));
}
$form['backend'] = [
'#type' => 'radios',
'#title' => $this
->t('Backend'),
'#description' => $this
->t('Choose a backend to use for this server.'),
'#options' => $backend_options,
'#default_value' => $server
->getBackendId(),
'#required' => TRUE,
'#disabled' => !$server
->isNew(),
'#ajax' => [
'callback' => [
get_class($this),
'buildAjaxBackendConfigForm',
],
'wrapper' => 'search-api-backend-config-form',
'method' => 'replace',
'effect' => 'fade',
],
];
$form['backend'] += $descriptions;
}
else {
$url = 'https://www.drupal.org/docs/8/modules/search-api/getting-started/server-backends-and-features';
$args[':url'] = Url::fromUri($url)
->toString();
$error = $this
->t('There are no backend plugins available for the Search API. Please install a <a href=":url">module that provides a backend plugin</a> to proceed.', $args);
$this->messenger
->addError($error);
$form = [];
}
}