public function ManageSortFieldsForm::buildForm in Search API Sorts Widget 1.x
Throws
\Drupal\Component\Plugin\Exception\PluginException
Overrides FormInterface::buildForm
File
- src/
Form/ ManageSortFieldsForm.php, line 137
Class
- ManageSortFieldsForm
- Provides a form for managing sort fields for a search api display.
Namespace
Drupal\search_api_sorts_widget\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;
$settings = $this
->getSettings();
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 widgets for %label', [
'%label' => $this->display
->label(),
]);
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 widgets.', [
'%language' => $this->languageManager
->getDefaultLanguage()
->getName(),
]),
],
],
];
}
$form['widget'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Widget settings'),
];
$form['widget']['status'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Active'),
'#default_value' => $settings
->get('status'),
);
$form['widget']['autosubmit'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Autosubmit'),
'#description' => $this
->t('Automatically submit the form once an element is changed.'),
'#default_value' => $settings
->get('autosubmit'),
);
$form['widget']['autosubmit_hide'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Hide submit button'),
'#description' => $this
->t('Hide submit button if javascript is enabled.'),
'#default_value' => $settings
->get('autosubmit_hide'),
);
$header = [
$this
->t('Weight'),
$this
->t('Field'),
$this
->t('Label: Ascending'),
$this
->t('Label: Descending'),
];
$sorts_widget_are_translatable = $this->moduleHandler
->moduleExists('config_translation') && $this->languageManager
->isMultilingual();
if ($sorts_widget_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();
$sorts = $settings
->get('sorts');
uasort($sorts, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
foreach ($fields as $key => $sort) {
$field = $fields[$key] ?? NULL;
if (empty($field) || !$field['status']) {
continue;
}
$form['sorts'][$key]['#attributes']['class'][] = 'draggable';
$form['sorts'][$key]['weight'] = [
'#type' => 'weight',
'#default_value' => $sorts[$key]['weight'] ?? 0,
'#delta' => 100,
'#attributes' => [
'class' => [
'search-api-sort-order-weight',
],
],
];
$form['sorts'][$key]['field'] = [
'#markup' => Html::escape($field['field']),
];
$form['sorts'][$key]['label_asc'] = array(
'#type' => 'textfield',
'#default_value' => $sorts[$key]['label_asc'] ?? '',
);
$form['sorts'][$key]['label_desc'] = array(
'#type' => 'textfield',
'#default_value' => $sorts[$key]['label_desc'] ?? '',
);
if ($sorts_widget_are_translatable) {
$form['sorts'][$key]['translate'] = [
'#type' => 'link',
'#title' => $this
->t('Translate'),
'#url' => Url::fromRoute('entity.search_api_sorts_widget.config_translation_overview', [
'search_api_sorts_widget' => $this
->getEscapedConfigId($this->display
->getPluginId()) . '_' . $key,
]),
];
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save settings'),
];
return $form;
}