1

I try to add a new filter criteria to my views with a custom module.

I manage to display my new filter on the filter criteria list but when I select it and go to "Add and configure filter criteria" I have an error saying:

The handler for this item is broken or missing. The following details are available:

Enabling the appropriate module may solve this issue. Otherwise, check to see if there is a module update available.

Here is my content_search.module :

function content_search_views_data_alter(&$data)
{
    $data['node']['content_search_filter'] = [
        'title' => t('Content search filter'),
        'filter' => [
            'title' => t('Content search filter'),
            'help' => 'Filters nodes on its content',
            'field' => 'nid',
            'id' => 'content_search_filter',
        ]
    ];
}

And here is my ContentSearchFilterPlugin, placed in content_search/src/Plugin/Filter/ContentSearchFilterPlugin.php

namespace Drupal\content_search\Plugin\Filter;

use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\FilterPluginBase;

/**
 * Filters nodes on on their content
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("content_search_filter")
 */
class ContentSearchFilterPlugin extends FilterPluginBase {
  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->valueTitle = t('Content search filter');
  }


  public function query() {
    parent::query();
    // TODO: custom query here
  }

}

What am I missing ?

2 Answers 2

1

I think you have a problem with plugin file organization. It should be :

content_search/src/Plugin/views/filter/ContentSearchFilterPlugin.php

...instead of

content_search/src/Plugin/Filter/ContentSearchFilterPlugin.php

And the namespace for this file should be

namespace Drupal\content_search\Plugin\views\filter;
0

This works for me in Drupal8:

Name of custom module: custom_modulename

Name of table in SQL: name_of_sql_table

Selecting the ID for filtering

function custom_modulename_views_data_alter(array &$data) { 
  $data['name_of_sql_table']['custom_modulename'] = [
    'title' => t('Title shown'),
    'group' => t('abc'),
    'filter' => [
      'title' => t('Filter'),
      'help' => t('Self made'),
      'field' => 'id',
      'id' => 'string'
    ],
  ];
}

Hint: If you want to use options etc. then it is much more complicated. Use a Class for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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