Problem/Motivation

When using an address field and exposing the "country" element of it via views exposed filter + better exposed filters, "tagify" is offered as a widget, but it does not work.

Steps to reproduce

- Install "address", "better exposed filters" and "tagify".
- Create an address field (type Address) "field_address" on any content type
- Create a view for that content type and select "field_address:country_code" as filter and mark it as exposed
- Use "better exposed filters" and see the settings for that field. Tagify is offered as an option, choose it.
- Save
filter

The filter does NOT use tagify (neither admin theme nor frontend theme).
admin theme

Proposed resolution

Either don't offer it as option or even better, make it work for that field.

Issue fork tagify-3587368

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

fjgarlin created an issue. See original summary.

fjgarlin’s picture

I've tried via form alter but that didn't work.

function bluecheese_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id === 'views_exposed_form') {
    $form['offices']['#type'] = 'select_tagify';
  }
}

I've also tried via custom JS, and while that rendered the element, it didn't fully connect it to the actual input:

      const selectEl = context.querySelector('select[name="officessss[]"]');
      if (selectEl && !selectEl.classList.contains('tagify-initialized')) {
        new Tagify(selectEl, {
          mode: 'multi', // or 'select' depending on your need
          enforceWhitelist: true,
          whitelist: [...selectEl.options].map(o => ({ value: o.value, label: o.text }))
        });
        selectEl.classList.add('tagify-initialized');
      }

In this case, the tagify field works (though it renders the values instead of the labels) but it does NOT update the underlying field.

fjgarlin’s picture

This code made it work (with help from claude), for reference:

(function ($, Drupal, once) {
  Drupal.behaviors.tagifyExposedFilter = {
    attach: function (context, settings) {
      once('tagify-exposed', 'select[name="offices[]"]', context)
        .forEach(function (selectEl) {
          const whitelist = [...selectEl.options]
            .filter(o => o.value !== '')
            .map(o => ({ value: o.text, id: o.value }));

          // Grab any already-selected options
          const preselected = [...selectEl.options]
            .filter(o => o.selected && o.value !== '')
            .map(o => ({ value: o.text, id: o.value }));

          const tagifyInput = document.createElement('input');
          tagifyInput.type = 'text';
          selectEl.insertAdjacentElement('afterend', tagifyInput);
          selectEl.style.display = 'none';

          const tagify = new Tagify(tagifyInput, {
            enforceWhitelist: true,
            whitelist: whitelist,
            dropdown: {
              searchKeys: ['value']
            }
          });

          // Pre-populate Tagify with already selected values
          if (preselected.length > 0) {
            tagify.addTags(preselected);
          }

          tagify.on('change', function () {
            const selectedIds = tagify.value.map(tag => tag.id);
            [...selectEl.options].forEach(option => {
              option.selected = selectedIds.includes(option.value);
            });
          });
        });
    }
  };
})(jQuery, Drupal, once);

I don't think this is a proper patch or fix so I'm not attempting to create an MR for it, just putting here as reference.

Ideally, the hook form alter would have been great, or calling any sort of api function at that level that will do all the wiring.

gxleano made their first commit to this issue’s fork.

gxleano’s picture

Status: Active » Needs review
fjgarlin’s picture

Status: Needs review » Reviewed & tested by the community

The code looks good and covers what was missing (as far as I can see). I tested the MR and it works perfectly, I no longer need to add the above JS code.

Marking this RTBC.

gxleano’s picture

Category: Bug report » Feature request
fjgarlin’s picture

Status: Reviewed & tested by the community » Needs work

Tagify is offered now where it wasn't before but it's NOT offered where it was before.

The "tagify" option does not appear now for normal exposed vocabulary fields set as dropdown.

fjgarlin’s picture

It does appear when set as "autocomplete", but not as "dropdown".

However, I am not sure if it's due to this issue or if it's a separate non-related issue. I'll let you debug just in case. I'm happy to create a separate issue if needed.

If this is not brought by this issue, this one can be back to RTBC.

fjgarlin’s picture

Status: Needs work » Reviewed & tested by the community

Not related to this.

gxleano’s picture

The BEF widget just needed some work.

I will add the next approach:

1. When you are trying to add a filter using an entity reference field and choosing the Selection type "Autocomplete", we will provide a Exposed filter widget called Tagify - This is already working by design.
2. When you are trying to add a filter using an entity reference field and choosing the Selection type "DropDown", we will provide a Exposed filter widget called Tagify Select - This will be added as new feature.
3. For the rest of the filter type that can use a select filter, like Published, Address and other fields, we will add a Exposed filter widget called Tagify Select - This will be added as new feature.