0

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?

  • 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. – Anson W Han Dec 22 '17 at 23:32
0

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, privacy policy and cookie policy

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