My idea are instead of update the views name to the custom code one by one. I like to apply to same content type, but I don't know the syntax.

 function myModule_views_query_alter(&$view, &$query)

   if(($view->name == '?????????'){


I would get the node/content type "myArticle" for node table as following code

$content_type = $wrapper->type->value();

but how can I update $view->name to $views->node->type ? I try print_r the $view but not fully understand the syntax to access the field. Not sure if I went in the wrong direction. Please advise.

  • 1
    Can you please elaborate upon the requirement. I am not able to get what exactly do you wish to achieve Dec 20, 2017 at 10:21
  • 1. i created custom content type for Group doc. called dept_article 2. then add node custom field department (term "department ID") to the dept_article. 3. then create user role Dept mgr, staff, mgt 4. then add user custom field rom admin > people again called field department (term "department ID") now i want to use hook views query alter. if content type = dept_artile and $role = then $query=>add_where().... i think alter the query ref to role is easier for beginner, compare to create a more complex module.
    – user76445
    Dec 20, 2017 at 15:18

1 Answer 1


If You only want to display data for specific roles to specific persons then you can use Global php in views to filter your results, Just take "User: Roles (Roles)" in your field as excluded and use its token in Global PHP field.example

if([roles] == "admin"){print [your_field_token_name];}
elseif([roles] == "your_roles"){print [your_field_token_name];}

You have to download the module of viewsphp to use global php.

  • hello Ranjan, thanks for you reply, i never try token programming, need to study first, by the way, is there any way to do without additional Module, thanks, have a nice weekend and Max's Christmas..!
    – user76445
    Dec 22, 2017 at 14:59
  • Hello, Using a tokens are not a bigger task, actually views already makes a tokens for their use also and the module "viewsphp" make you enable to write PHP in views and if you want to use hook_views_query_alter that's also easy you can use details of add_where syntax here: api.drupal.org/api/views/… may this helps you...add_where($group, $field, $value = NULL, $operator = NULL) and let me know if any other help, thanks and Marry christmas ...!
    – Ranjan
    Dec 23, 2017 at 16:26

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.