2

I want to add an additional form element in the configuration form for core/modules/views/src/Plugin/Block/ViewsExposedFilterBlock.php by adding

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $form['my_new_form_element'] = [...];

  return $form;
}

If I hack core and add that code in the file, it does what I want. But of course I don't want to hack core. So I'm trying to figure out how to make it so that my custom module's block class is used instead of ViewsExposedFilterBlock.php.

So how do I alert Views to use my class, ExposedFilterImagesBlock.php?

Information that I've found that might help you help me:

ViewsExposedFilterBlock.php has deriver = "Drupal\views\Plugin\Derivative\ViewsExposedFilterBlock" in its annotation. So I'm assuming the deriver plays an important role...just one that is unclear to me.

Thanks in advance for any help you can provide!

1
  • 2
    You don't alert Views. All Views are scanned for configured exposed blocks and a block plugin definition is derived for each one found. You can try to replace the plugin class in hook_block_alter() and check if the derived blocks also have your custom class, see drupal.stackexchange.com/questions/264349/…
    – 4uk4
    Jun 26, 2019 at 20:35

1 Answer 1

3

Thanks to @4k4 (here) and @nikathone and @dorf (via Slack), I was able to arrive at this, placed in exposed_filter_images.module (all caps should be replaced with a machine name):

function exposed_filter_images_block_alter(&$plugins) {
  if (isset($plugins['views_exposed_filter_block:VIEW_NAME-DISPLAY_NAME'])) {
    $plugins['views_exposed_filter_block:VIEW_NAME-DISPLAY_NAME']['class'] = \Drupal\exposed_filter_images\Plugin\Block\ExposedFilterImagesBlock::class;
  }
} 

Your Answer

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

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