I have placed the following function in my .module file to get the information of current view. But it returns null

public function buildForm(array $form, FormStateInterface $form_state) {
    $record = array();
    $view = views_get_current_view();

I have added use Drupal\views\Views; in the top.

What could be the reason to return null. Am i missing anything?

  • 1
    can your provide more background as to how your module comes to call this function? Do you have a hook that calls it; and if so, which one? views_get_current_view() as you are calling it now has no context to indicate what contextual filters or arguments to pass, or what specific page or entity would include the view. Commented Dec 22, 2017 at 23:32

1 Answer 1


views_get_current_view() seems to be from the early days of procedural code and I don't know how this can be used in connection with form building in D8.

You can get the current view from the current route, if this is what you are looking for:

use Drupal\views\Entity\View;

  $view_id = \Drupal::routeMatch()->getParameter('view_id');
  if ($view_id) {
    $view = View::load($view_id);

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.