You are here

public function MappedFieldProperty::buildConfigurationForm in Search API Field Map 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/search_api/processor/Property/MappedFieldProperty.php \Drupal\search_api_field_map\Plugin\search_api\processor\Property\MappedFieldProperty::buildConfigurationForm()
  2. 8.2 src/Plugin/search_api/processor/Property/MappedFieldProperty.php \Drupal\search_api_field_map\Plugin\search_api\processor\Property\MappedFieldProperty::buildConfigurationForm()
  3. 4.x src/Plugin/search_api/processor/Property/MappedFieldProperty.php \Drupal\search_api_field_map\Plugin\search_api\processor\Property\MappedFieldProperty::buildConfigurationForm()

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/MappedFieldProperty.php, line 39

Class

MappedFieldProperty
Defines an "mapped field" property.

Namespace

Drupal\search_api_field_map\Plugin\search_api\processor\Property

Code

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();

    // Make an array of all the entity types we're working with to pass to token_help.
    $entity_types[] = $entity_type;
    foreach ($bundles as $bundle_id => $bundle_label) {

      // Create a config field for each bundle in our enabled datasources.
      $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,
        ),
        '#maxlength' => 512,
      ];

      // Set the default value if something already exists in our config.
      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];
      }
    }
  }

  // Build the token picker.
  $form['token_help'] = [
    '#theme' => 'token_tree_link',
    '#token_types' => $entity_types,
  ];
  return $form;
}