# How to find which side a custom filter is included in from the field\_filter hook?

**URL:** <https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814>\
**Category:** Development\
**Created:** [May 8, 2022, 6:08am UTC](https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814 "2022-05-08T06:08:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [May 8, 2022, 6:08am UTC](https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814/1 "2022-05-08T06:08:34Z")

</div>

The field\_filter hook doesn’t include this kind of info in the arguments passed. Is there another way this can be found?

---

<div class="post-metadata">

**Author:** ![hkr](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@hkr](https://forums.ankiweb.net/u/hkr)\
**Post date:** [May 8, 2022, 2:04pm UTC](https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814/2 "2022-05-08T14:04:43Z")

</div>

The only (ugly) workaround I can come up with:

```python
import re

from anki import cards, hooks, template
from aqt import gui_hooks

q_start = "###question###"
q_end = "###/question###"
a_start = "###answer###"
a_end = "###/answer###"
regex = fr"{q_start}(?P<q_text>.*?){q_end}{a_start}(?P<a_text>.*?){a_end}"

def on_field_filter(
    field_text: str,
    field_name: str,
    filter_name: str,
    ctx: template.TemplateRenderContext,
) -> str:
    if filter_name == "my_filter_name":
        q_text = "foo"
        a_text = "bar"
        field_text = f"{q_start}{q_text}{q_end}{a_start}{a_text}{a_end}"
    return field_text

def on_card_will_show(text: str, card: cards.Card, kind: str) -> str:
    if "Question" in kind:
        text = re.sub(regex, r"\g<q_text>", text, flags=re.DOTALL)
    elif "Answer" in kind:
        text = re.sub(regex, r"\g<a_text>", text, flags=re.DOTALL)
    return text

hooks.field_filter.append(on_field_filter)
gui_hooks.card_will_show.append(on_card_will_show)

```

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [May 9, 2022, 12:28am UTC](https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814/3 "2022-05-09T00:28:00Z")

</div>

Indeed, it’s an ugly, but a viable workaround 🙂 Thanks!

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [May 9, 2022, 9:51am UTC](https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814/4 "2022-05-09T09:51:45Z")

</div>

We could probably expose the side to the field filter, but changing the signature will require a new hook. One alternative would be to add a bool property on ctx.

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [May 9, 2022, 12:07pm UTC](https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814/5 "2022-05-09T12:07:57Z")

</div>

> [@dae](#):
>
> One alternative would be to add a bool property on ctx.

Was going to do something like this. Will send a PR.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 3:15am UTC](https://forums.ankiweb.net/t/how-to-find-which-side-a-custom-filter-is-included-in-from-the-field-filter-hook/19814/6 "2023-05-01T03:15:45Z")

</div>


