Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 - Add filter for entity tags #20573

Merged
merged 2 commits into from
Jun 15, 2021

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Jun 11, 2021

Overview

Adds a SearchKit/APIv4 filter for tags similar to the "groups" filter recently added to SearchKit.

Before

No filter to search for Contacts/Activities/Cases/etc. by tag.

After

Filter is added to all taggable entities. Finds records with or without particular tag(s). Searching on a parent tag will find records with child tags.

Technical Details

I updated the tag_used_for option group to fix a couple issues with it:

  • The labels were not translated even though they are displayed on a form
  • Option names didn't correspond to entity names exactly (e.g. "Activities" doesn't match the canonical name "Activity", and "Attachements" [sic] doesn't match any entity name and is misspelled).

@civibot
Copy link

civibot bot commented Jun 11, 2021

(Standard links)

@civibot civibot bot added the master label Jun 11, 2021
@seamuslee001 seamuslee001 self-assigned this Jun 11, 2021
@eileenmcnaughton
Copy link
Contributor

Test Result (9 failures / +9)
api_v3_ActivityTest.testActivityGetTagFilter
api_v3_ActivityTest.testJoinOnTags
CRM_Contact_BAO_ActivitySearchTest.testSearchByBareTags
CRM_Contact_BAO_ActivitySearchTest.testSearchByTagset
CRM_Contact_Form_IndividualTest.testOpeningNewIndividualForm
CRM_Contact_Form_IndividualTest.testOpeningNewIndividualFormWithCustomField
CRM_Core_BAO_RecurringEntityTest.testRecurringEntityGenerationWithCustomDataAndTags
CRM_Core_FieldOptionsTest.testOptionValues
api.v4.Action.FkJoinTest.testBridgeJoinTags

@seamuslee001
Copy link
Contributor

Upgrade and code makes sense but clearly there is some issues around the used_for criteria field

@colemanw
Copy link
Member Author

I added legacy handling to _civicrm_api3_api_match_pseudoconstant_value() which fixed the tests.

@seamuslee001
Copy link
Contributor

@colemanw I tried this out on the PR test site but in both the APIv4 Explorer and in search kit I wasn't seeing the tags filter am I missing something?

tags-filter-notshowing

@colemanw
Copy link
Member Author

I see the problem. Regen.sh needs to be run so the install sql picks up the option value change.
Are you able to do that for this PR @seamuslee001 ?

@colemanw
Copy link
Member Author

I was able to do it myself thanks to @demeritcowboy's brilliant new github action https://github.com/demeritcowboy/civicrm-regen

@seamuslee001
Copy link
Contributor

@colemanw I tried on the test site and I created a test child tag that was a child of volunteer and then tried with tags:name or tags = Test Child Tag and it got the following error

Jun 15 15:37:04  [debug] AJAX Error ({error_id}): failed with exception
Array
(
    [error_id] => AkZo-8qFN-IIln
    [exception] => API_Exception: "Illegal operator for tags"
#0 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(481): Civi\Api4\Query\Api4SelectQuery->createSQLClause("`a`.`id`", "=", "Test Child Tag", (Array:27), 0)
#1 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(398): Civi\Api4\Query\Api4SelectQuery->composeClause((Array:3), "WHERE", 0)
#2 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(280): Civi\Api4\Query\Api4SelectQuery->treeWalkClauses((Array:3), "WHERE")
#3 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(134): Civi\Api4\Query\Api4SelectQuery->buildWhereClause()
#4 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(149): Civi\Api4\Query\Api4SelectQuery->getSql()
#5 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Generic/DAOGetAction.php(110): Civi\Api4\Query\Api4SelectQuery->run()
#6 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Generic/DAOGetAction.php(98): Civi\Api4\Generic\DAOGetAction->getObjects(Object(Civi\Api4\Generic\Result))
#7 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Provider/ActionObjectProvider.php(68): Civi\Api4\Generic\DAOGetAction->_run(Object(Civi\Api4\Generic\Result))
#8 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/API/Kernel.php(149): Civi\Api4\Provider\ActionObjectProvider->invoke(Object(Civi\Api4\Generic\DAOGetAction))
#9 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Generic/AbstractAction.php(239): Civi\API\Kernel->runRequest(Object(Civi\Api4\Generic\DAOGetAction))
#10 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/api/api.php(85): Civi\Api4\Generic\AbstractAction->execute()
#11 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Api4/Page/AJAX.php(139): civicrm_api4("Contact", "get", (Array:4), "")
#12 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Api4/Page/AJAX.php(80): CRM_Api4_Page_AJAX->execute("Contact", "get", (Array:4), "")
#13 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Core/Invoke.php(313): CRM_Api4_Page_AJAX->run((Array:5), NULL)
#14 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Core/Invoke.php(69): CRM_Core_Invoke::runItem((Array:12))
#15 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Core/Invoke.php(36): CRM_Core_Invoke::_invoke((Array:5))
#16 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/drupal/civicrm.module(458): CRM_Core_Invoke::invoke((Array:5))
#17 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/includes/menu.inc(527): civicrm_invoke("ajax", "api4", "Contact", "get")
#18 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/index.php(21): menu_execute_active_handler()
#19 {main}

)

Then i tried changing the operator to be IN with field = tags

Jun 15 15:38:23  [debug] AJAX Error ({error_id}): failed with exception
Array
(
    [error_id] => RJbO-uZrp-xHWU
    [exception] => CRM_Core_Exception: "One of parameters  (value: ) is not of the type CommaSeparatedIntegers"
#0 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Service/Spec/Provider/EntityTagFilterSpecProvider.php(78): CRM_Utils_Type::validate("", "CommaSeparatedIntegers")
#1 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(505): Civi\Api4\Service\Spec\Provider\EntityTagFilterSpecProvider::getTagFilterSql((Array:27), "`a`.`id`", "IN", "Test Child Tag", Object(Civi\Api4\Query\Api4SelectQuery), 0)
#2 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(481): Civi\Api4\Query\Api4SelectQuery->createSQLClause("`a`.`id`", "IN", "Test Child Tag", (Array:27), 0)
#3 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(398): Civi\Api4\Query\Api4SelectQuery->composeClause((Array:3), "WHERE", 0)
#4 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(280): Civi\Api4\Query\Api4SelectQuery->treeWalkClauses((Array:3), "WHERE")
#5 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(134): Civi\Api4\Query\Api4SelectQuery->buildWhereClause()
#6 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Query/Api4SelectQuery.php(149): Civi\Api4\Query\Api4SelectQuery->getSql()
#7 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Generic/DAOGetAction.php(110): Civi\Api4\Query\Api4SelectQuery->run()
#8 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Generic/DAOGetAction.php(98): Civi\Api4\Generic\DAOGetAction->getObjects(Object(Civi\Api4\Generic\Result))
#9 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Provider/ActionObjectProvider.php(68): Civi\Api4\Generic\DAOGetAction->_run(Object(Civi\Api4\Generic\Result))
#10 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/API/Kernel.php(149): Civi\Api4\Provider\ActionObjectProvider->invoke(Object(Civi\Api4\Generic\DAOGetAction))
#11 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/Civi/Api4/Generic/AbstractAction.php(239): Civi\API\Kernel->runRequest(Object(Civi\Api4\Generic\DAOGetAction))
#12 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/api/api.php(85): Civi\Api4\Generic\AbstractAction->execute()
#13 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Api4/Page/AJAX.php(139): civicrm_api4("Contact", "get", (Array:4), "")
#14 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Api4/Page/AJAX.php(80): CRM_Api4_Page_AJAX->execute("Contact", "get", (Array:4), "")
#15 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Core/Invoke.php(313): CRM_Api4_Page_AJAX->run((Array:5), NULL)
#16 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Core/Invoke.php(69): CRM_Core_Invoke::runItem((Array:12))
#17 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/CRM/Core/Invoke.php(36): CRM_Core_Invoke::_invoke((Array:5))
#18 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/sites/all/modules/civicrm/drupal/civicrm.module(458): CRM_Core_Invoke::invoke((Array:5))
#19 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/includes/menu.inc(527): civicrm_invoke("ajax", "api4", "Contact", "get")
#20 /home/jenkins/bknix-dfl/build/core-20573-3ssgx/web/index.php(21): menu_execute_active_handler()
#21 {main}

)

However doing IN with field tags:name worked and this was all done in the APIv4 Explorer

@colemanw
Copy link
Member Author

@seamuslee001 the API explorer is more open ended and allows you to screw stuff up. Try SearchKit it restricts the operator and normalizes the input for you.

@seamuslee001
Copy link
Contributor

Ok it works fine in Search Kit, I figure that it probably should be tied up in the Explorer but that can be a follow up

@seamuslee001 seamuslee001 merged commit ac0cecd into civicrm:master Jun 15, 2021
@seamuslee001 seamuslee001 deleted the entityTagFilter branch June 15, 2021 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants