You are here

protected function DynamicEntityReferenceWidget::createAutoCompletePaths in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget\DynamicEntityReferenceWidget::createAutoCompletePaths()

Creates auto complete path for all the given target types.

Parameters

string[] $target_types: All the referenceable target types.

Return value

array Auto complete paths for all the referenceable target types.

1 call to DynamicEntityReferenceWidget::createAutoCompletePaths()
DynamicEntityReferenceWidget::formElement in src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php, line 253

Class

DynamicEntityReferenceWidget
Plugin implementation of the 'dynamic_entity_reference autocomplete' widget.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget

Code

protected function createAutoCompletePaths(array $target_types) {
  $auto_complete_paths = [];
  $settings = $this
    ->getFieldSettings();
  foreach ($target_types as $target_type) {

    // Store the selection settings in the key/value store and pass a hashed
    // key in the route parameters.
    $selection_settings = $settings[$target_type]['handler_settings'] ?: [];
    $selection_settings += [
      'match_operator' => $this
        ->getSetting('match_operator'),
      'match_limit' => $this
        ->getSetting('match_limit'),
    ];
    $data = serialize($selection_settings) . $target_type . $settings[$target_type]['handler'];
    $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
    $key_value_storage = \Drupal::keyValue('entity_autocomplete');
    if (!$key_value_storage
      ->has($selection_settings_key)) {
      $key_value_storage
        ->set($selection_settings_key, $selection_settings);
    }
    $auto_complete_paths[$target_type] = Url::fromRoute('system.entity_autocomplete', [
      'target_type' => $target_type,
      'selection_handler' => $settings[$target_type]['handler'],
      'selection_settings_key' => $selection_settings_key,
    ])
      ->toString();
  }
  return $auto_complete_paths;
}