0

We have a custom module,mymodule, where we create a custom facet processor for a facet we created (myfacet). When we enable the module and cleared the cache we are not seeing the new processor display.

Question:

  • How to get custom facet processor to display

Setup

  • Add Article's Category (taxonomy term) to be indexed for search
  • Created a View (myview) with Page display.
  • Created facet where facet source = myview and field = Category >> Taxonomy Term >> Name
  • Name = myfacet

What we tried:

  • When we navigate to /admin/config/search/facets/myfacet/edit, the processor is not showing.
  • Disabling module, enabling, and clearing cache, but nothing.

Here is the code we tried for mymodule, but it is not showing. It was following Facetapi-processor-blogpost, but it is not showing as an option.

mymodule/src/Plugin/facets/processor/MyModuleProcessor.php

<?php
namespace Drupal\mymodule\Plugin\facets\processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
/**
 * Provides a processor that hides results start with a configurable character.
 *
 * @FacetsProcessor(
 *   id = "mymodule_replace",
 *   label = @Translation("Hide start with some letter"),
 *   description = @Translation("Hide all results that start with a configurable character"),
 *   stages = {
 *     "build" = 40
 *   }
 * )
 */
class MyModuleProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
    $processors = $facet->getProcessors();
    $config = isset($processors[$this->getPluginId()]) ? $processors[$this->getPluginId()] : null;
    $build['character'] = [
      '#title' => $this->t('Character to hide'),
      '#type' => 'textfield',
      '#default_value' => !is_null($config) ? $config->getConfiguration()['character'] : '',
      '#description' => $this->t("All results that start with this character will be hidden."),
    ];
    return $build;
  }
  /**
   * {@inheritdoc}
   */
  public function build(FacetInterface $facet, array $results) {
    $processors = $facet->getProcessors();
    $config = $processors[$this->getPluginId()];
    $char = $config->getConfiguration()['character'];
    /** @var \Drupal\facets\Result\ResultInterface $result */
    foreach ($results as $id => $result) {
      if (strpos(strtolower($result->getDisplayValue()), $char) === 0) {
        unset($results[$id]);
      }
    }
    return $results;
  }
}

mymodule/config/schema/mymodule.schema.yml

plugin.plugin_configuration.facets_processor.mymodule_replace:
  type: mapping
  label: 'Settings for the hide-starts-with processor'
  mapping:
    character:
      type: string
      label: Character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.