Is there a way to check if certain conditions on a facet are met and only then display a certain custom widget? If the specified conditions are not met, the widget becomes unavailable?

This would be similar to how the processors in SearchAPI work. Example:

  /**
   * Can only be enabled for an index that indexes the content bundle entity.
   *
   * {@inheritdoc}
   */
  public static function supportsIndex(IndexInterface $index) {
    foreach ($index->getDatasources() as $datasource) {
      if ($datasource->getEntityTypeId() == 'node') {
        return TRUE;
      }
    }
    return FALSE;
  }

Is there any way to do something similar with Facet Widgets and Processors?

Comments

dakku created an issue. See original summary.

borisson_’s picture

We have something almost similar with \Drupal\facets\Widget\WidgetPluginInterface::isPropertyRequired, but it's not the exact same thing.

dakku’s picture

Hi Borison,
I checked isPropertyRequired, I agree it is similar but what I am looking for is something like:

- user chooses a specific field for Facet:
Glossary Index field: This field contains the Glossary (A, B, C) etc value for a specific node.

- If the user chose the glossary field, Glossary widget should become available (depending on the field criteria)

- processors should follow similar logic.

I am trying to refactor my https://www.drupal.org/project/search_api_glossary module to make it work with latest version of facets, You can check my code here:
https://github.com/dakkusingh/drupalglossaryazsearch/blob/8.x-3.x/src/Pl...
and here
https://github.com/dakkusingh/drupalglossaryazsearch/blob/8.x-3.x/src/Gl...

Any thoughts on how best to implement a field dependency check for Widgets and Processors?

dakku’s picture

I see one quick option, not sure if this is in the correct direction:

I implement a custom Facet Widget Form validator

- get facet $facet->getFieldIdentifier();
-- check if the field chosen is glossary field
-- check if glossary widget is enabled
-- if field is not glossary and widget is glossary
-- throw a validate error, user can either change the widget or change the field.

borisson_’s picture

Issue tags: +beta blocker

I agree that should provide a method to do this, that makes sense to do.

I suggest something like this:

WidgetInterface {
  public function allowedDataType(string $dataType):bool {}
}
dakku’s picture

Hi Borison,
Thank you for your reply.

I am happy to give it a try to implement what you mentioned above, however I have no idea how the allowedDataType method would then change visibility of the widget upstream.

Can you give me some pointers and I can try to code it and create a patch for review?

borisson_’s picture

Not sure how to do it yet either. My suggestion in #5 can be forgotten for now.

borisson_’s picture

Status: Active » Needs review
StatusFileSize
new2.91 KB

How about this implementation?

borisson_’s picture

Issue tags: +Vienna2017

Tagging with vienna's tag.

Status: Needs review » Needs work

The last submitted patch, 8: only_display_facet-2877691-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

borisson_’s picture

Status: Needs work » Needs review
StatusFileSize
new2.91 KB
new3.93 KB

Should fix the page + also adds docs.

dakku’s picture

Thank you sir Borisson. Sounds like Vienna was quite productive session. I will try it out and feedback.

borisson_’s picture

StatusFileSize
new4.51 KB
new8.06 KB

Now with tests!

strykaizer’s picture

Status: Needs review » Reviewed & tested by the community

Looks good

borisson_’s picture

Status: Reviewed & tested by the community » Fixed

  • borisson_ committed edb04f5 on 8.x-1.x
    Issue #2877691 by borisson_: Only display Facet Widget if certain...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.