1

I'm trying to figure out how to make a processor for facets in Drupal 8.3. I've got this, but I have a couple issues. The first is a fatal error I get after activating the module, and navigating to a page with the facet on it:

Fatal error: Cannot declare class Drupal\facets_change_labels\Plugin\facets\processor\transform_labels, because the name is already in use in Drupal/modules/custom/facet_change_labels/src/Plugin/facets/processor/TransformLabels.php on line 22

It doesn't make sense, because this is the only place in the entire site that transform_labels or transformLabels is used. As an experiment, I changed the function to a completely random name, and the issue persisted.

What am I missing, or not understanding?

The code:

<?php

namespace Drupal\facets_change_labels\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 for transforming labels.
 *
 * @FacetsProcessor(
 *   id = "transform_labels",
 *   label = @Translation("Transform labels"),
 *   description = @Translation("Transform labels"),
 *   stages = {
 *     "build" = 35
 *   }
 * )
 */
class transform_labels extends ProcessorPluginBase implements BuildProcessorInterface {

  /**
   * {@inheritdoc}
   */
  public function build(FacetInterface $facet, array $results) {
    $config = $this->getConfiguration();

    /** @var \Drupal\facets\Result\Result $result */
    foreach ($results as $result) {
      if ($result->getRawValue() == 'shape') {
        $result->setDisplayValue("leaf shape");
      }
      elseif ($result->getRawValue() == 'type') {
        $result->setDisplayValue("leaf type");
      }
      elseif ($result->getRawValue() == 'color') {
        $result->setDisplayValue("leaf color");
      }
    }

    return $results;
  }
}
1

For PS4 class autodiscovery to work, your file name needs to be the same as your class name. From reading the error message, it appears that your file name is TransformLabels.php, but you've named your class transform_labels. You need to change your class name to TransformLabels:

class TransformLabels extends ProcessorPluginBase implements BuildProcessorInterface {

One other question I have is, did you create the facets processor plugin type? Or was it a 3rd party?

  • No, I didn't create it. The original script can be found here: drupal.org/project/facets/issues/2941661 . I'm using it as a starting point to learn from. BTW, your answer above did solve the error... unfortunately, the processor didn't seem to do anything, though. Guess I still got some learning to do! – turpentyne May 19 '18 at 4:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.