public function ManageSortFieldsForm::buildForm in Search API sorts 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ ManageSortFieldsForm.php, line 108
Class
- ManageSortFieldsForm
- Provides a form for managing sort fields for a search api display.
Namespace
Drupal\search_api_sorts\FormCode
public function buildForm(array $form, FormStateInterface $form_state, IndexInterface $search_api_index = NULL, $search_api_display = NULL) {
$original_search_api_display = $this
->getOriginalConfigId($search_api_display);
$this->display = $this->displayPluginManager
->createInstance($original_search_api_display);
$this->index = $search_api_index;
if ($disabled = empty($this->index
->status())) {
$this
->messenger()
->addWarning($this
->t('Since the index for this display is at the moment disabled, no sorts can be activated.'));
}
$form['#title'] = $this
->t('Manage sort fields for %label', [
'%label' => $this->display
->label(),
]);
$form['description'] = [
'#type' => 'item',
'#title' => $this
->t('Select the available sorts'),
'#description' => $this
->t('<p>Only index single-value strings or numbers can be used as sorts. See the Fields tab to change indexed fields.</p>'),
];
if ($this->languageManager
->getDefaultLanguage()
->getId() !== $this->languageManager
->getCurrentLanguage()
->getId()) {
$form['translation_message'] = [
'#theme' => 'status_messages',
'#message_list' => [
MessengerInterface::TYPE_WARNING => [
$this
->t('You are currently editing the %language version of the search api sorts fields.', [
'%language' => $this->languageManager
->getDefaultLanguage()
->getName(),
]),
],
],
];
}
$header = [
$this
->t('Weight'),
$this
->t('Enabled'),
$this
->t('Default sort'),
$this
->t('Default order'),
$this
->t('Field'),
$this
->t('Type'),
$this
->t('Label'),
];
$sort_fields_are_translatable = $this->moduleHandler
->moduleExists('config_translation') && $this->languageManager
->isMultilingual();
if ($sort_fields_are_translatable) {
$header[] = $this
->t('Translate');
}
$form['sorts'] = [
'#type' => 'table',
'#header' => $header,
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'search-api-sort-order-weight',
],
],
'#empty' => $this
->t('There are currently no fields for which sorts can be displayed.'),
];
$fields = $this
->getSearchApiSortsFieldsValues();
foreach ($fields as $key => $field) {
$form['sorts'][$key]['#attributes']['class'][] = 'draggable';
$form['sorts'][$key]['weight'] = [
'#type' => 'weight',
'#default_value' => $field['weight'],
'#delta' => 100,
'#attributes' => [
'class' => [
'search-api-sort-order-weight',
],
],
];
$form['sorts'][$key]['status'] = [
'#type' => 'checkbox',
'#default_value' => $field['status'],
'#disabled' => $disabled,
];
$form['sorts'][$key]['default_sort'] = [
'#type' => 'radio',
'#return_value' => $key,
'#tree' => FALSE,
'#default_value' => $field['default_sort'] === TRUE ? $key : NULL,
'#states' => [
'enabled' => [
':input[name="sorts[' . $key . '][status]"]' => [
'checked' => TRUE,
],
],
],
];
$form['sorts'][$key]['default_order'] = [
'#type' => 'select',
'#default_value' => $field['default_order'],
'#options' => [
'asc' => $this
->t('Ascending'),
'desc' => $this
->t('Descending'),
],
'#states' => [
'visible' => [
':input[name="sorts[' . $key . '][status]"]' => [
'checked' => TRUE,
],
],
],
];
$form['sorts'][$key]['field'] = [
'#markup' => Html::escape($field['field']),
];
$form['sorts'][$key]['type'] = [
'#markup' => $field['type'],
];
$form['sorts'][$key]['label'] = [
'#type' => 'textfield',
'#maxlength' => max(strlen($field['label']), 80),
'#size' => 30,
'#default_value' => $field['label'],
];
if ($sort_fields_are_translatable && $field['status'] === TRUE) {
$form['sorts'][$key]['translate'] = [
'#type' => 'link',
'#title' => $this
->t('Translate'),
'#url' => Url::fromRoute('entity.search_api_sorts_field.config_translation_overview', [
'search_api_sorts_field' => $this
->getEscapedConfigId($this->display
->getPluginId()) . '_' . $key,
]),
];
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save settings'),
];
return $form;
}