pluginreference-2.0.0/src/Plugin/Field/FieldWidget/PluginReferenceAutocompleteWidget.php
src/Plugin/Field/FieldWidget/PluginReferenceAutocompleteWidget.php
<?php
namespace Drupal\pluginreference\Plugin\Field\FieldWidget;
use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
* Plugin implementation of the 'plugin_reference_autocomplete' widget.
*/
#[FieldWidget(
id: 'plugin_reference_autocomplete',
label: new TranslatableMarkup('Autocomplete'),
field_types: [
'plugin_reference',
],
)]
class PluginReferenceAutocompleteWidget extends WidgetBase {
use PluginReferenceConfigurationFormTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'match_operator' => 'CONTAINS',
'match_limit' => 10,
] + self::configurationDefaultSettings() + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['match_operator'] = [
'#type' => 'radios',
'#title' => t('Autocomplete matching'),
'#default_value' => $this->getSetting('match_operator'),
'#options' => $this->getMatchOperatorOptions(),
'#description' => t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of items.'),
];
$elements['match_limit'] = [
'#type' => 'number',
'#title' => $this->t('Number of results'),
'#default_value' => $this->getSetting('match_limit'),
'#min' => 0,
'#description' => $this->t('The number of suggestions that will be listed. Use <em>0</em> to remove the limit.'),
];
return $elements + $this->configurationSettingsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$operators = $this->getMatchOperatorOptions();
$summary[] = t('Autocomplete matching: @match_operator', ['@match_operator' => $operators[$this->getSetting('match_operator')]]);
$size = $this->getSetting('match_limit') ?: $this->t('unlimited');
$summary[] = $this->t('Autocomplete suggestion list size: @size', ['@size' => $size]);
return $summary + $this->configurationSettingsSummary();
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// Append the match operation to the selection settings.
$selection_settings = $this->getFieldSetting('handler_settings') + [
'match_operator' => $this->getSetting('match_operator'),
'match_limit' => $this->getSetting('match_limit'),
];
$element['plugin_id'] = [
'#type' => 'plugin_autocomplete',
'#target_type' => $this->getFieldSetting('target_type'),
'#selection_handler' => $this->getFieldSetting('handler'),
'#selection_settings' => $selection_settings,
'#validate_reference' => FALSE,
'#default_value' => $items[$delta]->plugin_id,
] + $element;
return $this->singleConfigurationFormElement($items, $delta, $element, $form, $form_state);
}
/**
* {@inheritdoc}
*/
public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) {
return $element['plugin_id'];
}
/**
* Returns the value of a setting for the plugin reference selection handler.
*
* @param string $setting_name
* The setting name.
*
* @return mixed
* The setting value.
*/
protected function getSelectionHandlerSetting($setting_name) {
$settings = $this->getFieldSetting('handler_settings');
return $settings[$setting_name] ?? NULL;
}
/**
* Returns the options for the match operator.
*
* @return array
* List of options.
*/
protected function getMatchOperatorOptions() {
return [
'STARTS_WITH' => t('Starts with'),
'CONTAINS' => t('Contains'),
];
}
}
