We have a Drupal 9 site where we need to filter a view showing nodes where their "US State" field value is contained in the user’s "Assigned States" multi-value field.
Node: "US State" field is a plain text list single value field: "node__field_state_new"
User: "Assigned State(s)" is a multiple value check box field: "field_assigned_state"
We’ve created a custom filter but are stuck on getting it to work correctly. I suspect we may need to create joins in the query but not sure.
MODULE.views.inc:
function MODULE_views_data() { $data['views']['state_admin_filter'] = [ 'title' => t('Is State Admin'), 'filter' => [ 'title' => t('Is State Admin'), 'group' => t('Custom'), 'help' => t('Provides a custom filter to filter state admins.'), 'id' => 'state_admin_filter', ], ]; return $data; }
/src/Plugin/views/filter/StateAdminFilter.php
class StateAdminFilter extends StringFilter { public function query() { $user = DrupaluserEntityUser::load(Drupal::currentUser()->id()); $assigned_states = $user->field_assigned_state->getValue(); $this->query->addWhere('AND', 'node__field_state_new.field_state_new_value', serialize($assigned_states), 'IN'); } public $operator = 'IN'; }