4

I've built a custom field into the view which displays the menu name a node is assigned to, but I want to filter on the menu name. How can you filter on this computed field? The only examples I have found are for updating the SQL query to filter on existing values in the table.

2
  • but I want to filter on the menu name. How can you filter on this computed field? when you say computed field, are you referring to the computed field module? Also, I don't get how this "computed field" contains a menu name and not a number...
    – No Sssweat
    Feb 20, 2017 at 10:47
  • Not using the computed field module. I've written a custom field that outputs the name of the menu that the node is assigned to. This is what I want to filter on Feb 22, 2017 at 12:20

1 Answer 1

0

It is to my knowledge not possible to add a filter to a view for a computed field. A possible workaround would be to use hook_views_pre_render() to remove rows before Drupal renders the view.

Example code:

/**
 * Implements hook_views_pre_render().
 */
function mymodule_views_pre_render(ViewExecutable $view) {
  if ($view->id() === 'my_view' && $view->current_display === 'page_1') {
    foreach ($view->result as $key => $value) {
      $menu_name = $value->_entity->get('menu_name')->value;
      if ($menu_name !== 'My Menu') {
        // Remove row from the view
        unset($view->result[$key]);
      }
    }
  }
}

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.