0

I am trying to write a custom filter for views. The goal is to filter by alias, but that's not important now. It seems like I am missing a step to get view to recognize my filter hander after adding my filter in hook_views_data_alter as in the following:

/**
 * Implements hook_views_data_alter().
 */
function alias_views_filter_views_data_alter(array &$data) {
  $data['node']['alias'] = [
    'title' => t('alias filter'),
    'filter' => [
      'title' => t('alias filter'),
      'help' => t('Provides a custom filter nodes by their alias.'),
      'field' => 'nid',
      'id' => 'entity_alias',
    ],
  ];
}

I am able to get the filter to show in a view filter list. Now, my filter handler defined in /docroot/modules/custom/alias_views_filter/Plugin/views/filter/Alias.php but it does not seem to be picked up by views and I always get Broken/missing handler whenever I try to add the filter. Here is my handler definition:

<?php

namespace Drupal\alias_views_filter\Plugin\views\filter;

use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\StringFilter;
use Drupal\views\ViewExecutable;
use Drupal\Core\Form\FormStateInterface;


/**
 * Filters by alias.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("entity_alias")
 */
class Alias extends StringFilter {



  /**
   * The current display.
   *
   * @var string
   *   The current display of the view.
   */
  protected $currentDisplay;

  /**
   * @var views_plugin_query_default
   */
  public $query;

  /**
   * Constructs a new StringFilter object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    // this does seem to get evaluated at all ?
    die('Death in handler');
  }

  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['fields'] = ['default' => []];

    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->valueTitle = t('Filter by alias');
  }

  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $this->view->initStyle();
  }

  function query() {
    $this->ensureMyTable();
    $field = $this->tableAlias."nid"; //table_alias.".nid";
    $nids = $this->getNidsByAliasString($this->value);
    if (!empty($nids)) {
      $this->query->addWhere('AND', $this->options['group'], $field, $nids, 'IN');
    }
  }

  function getNidsByAliasString($aliasString) {
    $query = db_select('url_alias');
    $query->condition('alias', '%'.$aliasString.'%', 'LIKE');
    $result = $query
      ->fields('url_alias')
      ->execute();

    $nids = array();
    foreach ($result as $data) {
      array_push($nids, end(explode("/",$data->source)));
    }
    return $nids;
  }

}

Am missing a step or naming the filter incorrectly?

Checking the logs, I am getting these errors:

Notice: Undefined index: field in Drupal\views\Plugin\ViewsHandlerManager->getHandler() 
Notice: Undefined index: table in Drupal\views\Plugin\ViewsHandlerManager->getHandler() 
Notice: Undefined index: id in Drupal\views\Plugin\ViewsHandlerManager->getHandler() 
0

Specify the data table instead of the base table, just replace node with node_field_data in your hook and run drush cr.

/**
 * Implements hook_views_data_alter().
 */
function alias_views_filter_views_data_alter(array &$data) {
  $data['node_field_data']['alias'] = [
    'title' => t('alias filter'),
    'filter' => [
      'title' => t('alias filter'),
      'help' => t('Provides a custom filter nodes by their alias.'),
      'field' => 'nid',
      'id' => 'entity_alias',
    ],
  ];
}
  • I tried with both but it does not seem that's the issue. In fact initially wrote my question with node_field_data then edited it. – awm Oct 17 at 20:48

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.