public function AggregatedFieldProperty::buildConfigurationForm in Search API 8
Constructs a configuration form for a field based on this property.
Parameters
\Drupal\search_api\Item\FieldInterface $field: The field for which the configuration form is constructed.
array $form: An associative array containing the initial structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the complete form.
Return value
array The form structure.
Overrides ConfigurablePropertyInterface::buildConfigurationForm
File
- src/
Plugin/ search_api/ processor/ Property/ AggregatedFieldProperty.php, line 35
Class
- AggregatedFieldProperty
- Defines an "aggregated field" property.
Namespace
Drupal\search_api\Plugin\search_api\processor\PropertyCode
public function buildConfigurationForm(FieldInterface $field, array $form, FormStateInterface $form_state) {
$index = $field
->getIndex();
$configuration = $field
->getConfiguration();
$form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
$form['#tree'] = TRUE;
$form['type'] = [
'#type' => 'radios',
'#title' => $this
->t('Aggregation type'),
'#description' => $this
->t('Apart from the @union type, all types will result in just a single value.', [
'@union' => $this
->t('Union'),
]),
'#options' => $this
->getTypes(),
'#default_value' => $configuration['type'],
'#required' => TRUE,
];
foreach ($this
->getTypes('description') as $type => $description) {
$form['type'][$type]['#description'] = $description;
}
$form['fields'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Contained fields'),
'#options' => [],
'#attributes' => [
'class' => [
'search-api-checkboxes-list',
],
],
'#default_value' => $configuration['fields'],
'#required' => TRUE,
];
$datasource_labels = $this
->getDatasourceLabelPrefixes($index);
$properties = $this
->getAvailableProperties($index);
$field_options = [];
foreach ($properties as $combined_id => $property) {
list($datasource_id, $name) = Utility::splitCombinedId($combined_id);
// Do not include the "aggregated field" property.
if (!$datasource_id && $name == 'aggregated_field') {
continue;
}
$label = $datasource_labels[$datasource_id] . $property
->getLabel();
$field_options[$combined_id] = Utility::escapeHtml($label);
if ($property instanceof ConfigurablePropertyInterface) {
$description = $property
->getFieldDescription($field);
}
else {
$description = $property
->getDescription();
}
$form['fields'][$combined_id] = [
'#attributes' => [
'title' => $this
->t('Machine name: @name', [
'@name' => $name,
]),
],
'#description' => $description,
];
}
// Set the field options in a way that sorts them first by whether they are
// selected (to quickly see which one are included) and second by their
// labels.
asort($field_options, SORT_NATURAL);
$selected = array_flip($configuration['fields']);
$form['fields']['#options'] = array_intersect_key($field_options, $selected);
$form['fields']['#options'] += array_diff_key($field_options, $selected);
// Make sure we do not remove nested fields (which can be added via config
// but won't be present in the UI).
$missing_properties = array_diff($configuration['fields'], array_keys($properties));
if ($missing_properties) {
foreach ($missing_properties as $combined_id) {
list(, $property_path) = Utility::splitCombinedId($combined_id);
if (strpos($property_path, ':')) {
$form['fields'][$combined_id] = [
'#type' => 'value',
'#value' => $combined_id,
];
}
}
}
return $form;
}