View source
<?php
class SearchApiFederatedSolrRemap extends SearchApiAbstractAlterCallback {
public function propertyInfo() {
if (is_array($this->options['properties'])) {
return $this->options['properties'];
}
return [];
}
public function alterItems(array &$items) {
foreach ($items as &$item) {
foreach ($this->options['remap'] as $destination => $source) {
if ($source && isset($item->{$source})) {
$item->{$destination} = $item->{$source};
}
}
}
}
public function configurationForm() {
$form['remap'] = [
'#type' => 'fieldset',
'#title' => t('Remap properties'),
];
foreach ($this
->federatedFields() as $k => $field) {
$form['remap'][$k] = [
'#type' => 'select',
'#title' => $field['name'],
'#options' => $this
->indexFieldOptions(),
'#default_value' => isset($this->options['remap'][$k]) ? $this->options['remap'][$k] : '',
];
}
return $form;
}
public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
$properties = [];
$fields = $this->index
->getFields(FALSE);
foreach (array_filter($values['remap']) as $destination => $source) {
$properties[$destination] = [
'label' => t('@field (remapped from @key)', [
'@field' => $fields[$source]['name'],
'@key' => $source,
]),
'description' => $fields[$source]['description'],
'type' => $fields[$source]['type'],
];
}
$values['properties'] = $properties;
return parent::configurationFormSubmit($form, $values, $form_state);
}
protected function federatedFields() {
return [
'federated_title' => [
'name' => t('Federated Title'),
'description' => '',
'type' => 'string',
],
'federated_date' => [
'name' => t('Federated Date'),
'description' => '',
'type' => 'date',
],
'federated_type' => [
'name' => t('Federated Type'),
'description' => '',
'type' => 'string',
],
'federated_terms' => [
'name' => t('Federated Terms'),
'description' => '',
'type' => 'string',
],
'federated_image' => [
'name' => t('Federated Image'),
'description' => '',
'type' => 'uri',
],
'rendered_item' => [
'name' => t('Rendered Item'),
'description' => '',
'type' => 'text',
],
];
}
protected function indexFieldOptions() {
$options = array_diff_key($this->index
->getFields(FALSE), $this
->federatedFields());
array_walk($options, function (&$item, $key) {
$item = t('@name (@machine_name)', [
'@name' => $item['name'],
'@machine_name' => $key,
]);
});
return [
'- ' . t('None') . ' -',
] + $options;
}
}