Commit 4bd3d46a authored by David Galeano's avatar David Galeano
Browse files

Issue #3300367: Allow "contains" (as opposed to "starts with") filtering to be honored

parent ca48f591
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
field.widget.settings.tagify_entity_reference_autocomplete_widget:
  type: field.widget.settings.tagify
  label: 'Tagify entity reference widget'
  mapping:
    match_operator:
      type: string
      label: 'Autocomplete matching'
    match_limit:
      type: integer
      label: 'Maximum number of autocomplete suggestions.'
+3 −1
Original line number Diff line number Diff line
@@ -20,10 +20,12 @@
          tagify = new Tagify(input, tagify_args),
          controller;

        // avoid creating tag when the entity reference is not existing.
        // Avoid creating tag when the entity reference is not existing.
        tagify.settings.enforceWhitelist = !$(this).hasClass("autocreate");
        tagify.settings.skipInvalid = !!$(this).hasClass("autocreate");
        tagify.settings.maxTags = $(this).hasClass("limited") ? 1 : Infinity;
        // Enables filtering dropdown items values by string containing and not only beginning.
        tagify.settings.dropdown.fuzzySearch = true;

        // Bind "DragSort" to Tagify's main element and tell
        // it that all the items with the below "selector" are "draggable".
+63 −4
Original line number Diff line number Diff line
@@ -115,14 +115,60 @@ class TagifyEntityReferenceAutocompleteWidget extends WidgetBase {
    $this->entityTypeManager = $entityTypeManager;
  }

    /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
        'match_operator' => 'STARTS_WITH',
        'match_limit' => 10,
      ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element['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 entities.'),
    ];
    $element['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 $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];

    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $default_value = $this->defaultValues($items);
    // Append the match operation to the selection settings.
    $selection_settings = $this->getFieldSetting('handler_settings') + [
      'match_operator' => 'CONTAINS',
      'match_limit' => 10,
      'match_operator' => $this->getSetting('match_operator'),
      'match_limit' => $this->getSetting('match_limit'),
    ];
    $target_type = $this->getFieldSetting('target_type');
    $selection_handler = $this->getFieldSetting('handler');
@@ -203,8 +249,8 @@ class TagifyEntityReferenceAutocompleteWidget extends WidgetBase {
    }
    $target_type = $this->getFieldSetting('target_type');
    $selection_settings = $this->getFieldSetting('handler_settings') + [
      'match_operator' => 'CONTAINS',
      'match_limit' => 10,
      'match_operator' => $this->getSetting('match_operator'),
      'match_limit' => $this->getSetting('match_limit'),
      'target_type' => $target_type,
    ];
    $bundle = $this->getAutocreateBundle();
@@ -299,4 +345,17 @@ class TagifyEntityReferenceAutocompleteWidget extends WidgetBase {
    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'),
    ];
  }

}