Dynamic searchable_fields

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

V 4.3

$searchable_fields based on permissions

This are my standard searchable fields.

private static $searchable_fields = [
        'Date',
        'Number',
        'ArticleNumber',
        'Article',
        'Season',
    ];   

if a user is in a certain group, I want to remove and add some other fields as searchable_fields. how can I solve this?

It looks like searchable_fields is used in two or more form scaffolding methods, so to avoid having to mess with those I think the easiest thing would be to add some code somewhere to check what group the current user is in and update the config value at run time. You could maybe use on anAfterInit() hook or something like that, then do e.g. YourDataObjectClass::config()->set('searchable_fields', ['what', 'ever'])

what about implementing scaffoldSearchFields ?

public function scaffoldSearchFields($_params = null) {
  $fields = parent::scaffoldSearchField($_params);
  // now update the fieldList
  return $fields;
}

That’s a cool idea, but I think you would need to overload defaultSearchFilters() as well.

Looking at the code I think if you override searchableFields() instead that might work well because this method is used by scaffoldSearchFields() and defaultSearchFilters() to determine which fields to include.

Note also that scaffoldSearchFields might be used in some parts of the CMS but not others (e.g. I don’t think it would be used by ModelAdmin but I could definitely be wrong).

Each DataObject subclass can have multiple search contexts for different cases, e.g. for a limited frontend search and a fully featured backend search. By default, you can use {@link DataObject->getDefaultSearchContext()} which is automatically scaffolded. It uses {@link DataObject::$searchable_fields} to determine which fields to include.

I solved it this way …

I don’t used:

private static $searchable_fields = [

I implemented this function:

public function getDefaultSearchContext() {

    $fields = new FieldList();
    $filters = array();
    $member = Member::currentUser();

    // created the fields, can check permissions & pushed the fields to $filters array

    return new SearchContext(
        Order::class,
        $fields,
        $filters
    );