I have an existing view to which I would like to add some contextual filters if my module gets enabled. In D7 this was possible using hook_views_default_views, but I can't seem to find the equivalent in D8. Is this possible in D8?

  • Yes, but it's not easy to figure out how. It involves adding handler to the viewExecutable object in the view_pre_view hook. This may get you started: atendesigngroup.com/blog/…
    – Jonathan
    Sep 11, 2016 at 23:17

1 Answer 1


Providing a view by using hook_views_default_views() is no longer possible. In D8 views are configuration entities. This means when a module wants to provide a view, it puts a yml file in /config/install.

If you want to change a view in code, load the view, change it and save it.

$view = View::load('test_filter_taxonomy_index_tid');
$display =& $view->getDisplay('default');
$display['display_options']['filters']['tid']['type'] = 'textfield';

A third alternative is to change the view directly in configuration, for example replace it with the content of a yml file:

$config_path = drupal_get_path('module', 'mymodule') . '/config/optional/views.view.myview.yml';
$data = \Symfony\Component\Yaml::parse(file_get_contents($config_path));

The last option might be the easiest for your use case. You can configure the view in ui, export it to a yml file and use this file to overwrite the configuration of an existing view in hook_install().

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.