MappedFieldProperty.php in Search API Field Map 8
File
src/Plugin/search_api/processor/Property/MappedFieldProperty.php
View source
<?php
namespace Drupal\search_api_field_map\Plugin\search_api\processor\Property;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\FieldInterface;
use Drupal\search_api\Processor\ConfigurablePropertyBase;
use Drupal\search_api\Processor\ConfigurablePropertyInterface;
use Drupal\search_api\Utility\Utility;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Entity\ContentEntityBase;
class MappedFieldProperty extends ConfigurablePropertyBase {
use StringTranslationTrait;
public function defaultConfiguration() {
return [
'type' => 'union',
'fields' => [],
];
}
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['field_data'] = [
'#type' => 'item',
'#title' => $this
->t('Mapped data'),
'#description' => $this
->t('Set the data to be sent to the index for each bundle in the data sources set in your index configuration. Use static values or choose tokens using the picker below.'),
];
foreach ($index
->getDatasources() as $datasource_id => $datasource) {
$bundles = $datasource
->getBundles();
$entity_type = $datasource
->getEntityTypeId();
$entity_types[] = $entity_type;
foreach ($bundles as $bundle_id => $bundle_label) {
$form['field_data'][$entity_type][$bundle_id] = [
'#type' => 'textfield',
'#title' => $this
->t('Field data for %datasource » %bundle', [
'%datasource' => $datasource
->label(),
'%bundle' => $bundle_label,
]),
'#element_validate' => array(
'token_element_validate',
),
'#token_types' => array(
$entity_type,
),
];
if (isset($configuration['field_data'][$entity_type][$bundle_id])) {
$form['field_data'][$entity_type][$bundle_id]['#default_value'] = $configuration['field_data'][$entity_type][$bundle_id];
}
}
}
$form['token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => $entity_types,
];
return $form;
}
public function submitConfigurationForm(FieldInterface $field, array &$form, FormStateInterface $form_state) {
$values = [
'field_data' => array_filter($form_state
->getValue('field_data')),
];
$field
->setConfiguration($values);
}
}