I am interested in altering a views query similar to: https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php..., but I'm unable to access the $query->where property as it's protected. Would it be possible to match the core "Sql" class, and make the "where" property public so this can be altered, or is there a reason for not allowing this outside of the class?

Comments

bkildow created an issue. See original summary.

drunken monkey’s picture

Component: General code » Views integration
Status: Active » Needs review
StatusFileSize
new1.1 KB

It would be possible, yes, but we don't do it due to general best practices for OOP programming. Views is, sadly, a very bad example for that.
However, it is true that not being able to alter the $where property is a glaring hole in our API. The proper way to fix it, though, is to provide a getter. (Even cleaner would probably be a getter and a setter, but this should be simpler to use, and we don't need to be unnecessarily strict.)

Thanks for the suggestion, and please try the attached patch!

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Looks good!

  • drunken monkey committed 031fbd2 on 8.x-1.x
    Issue #2799497 by drunken monkey: Added a getter for the Views query's "...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for reviewing!
Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

guaneagler’s picture

StatusFileSize
new454 bytes

Sorry, I really want to change the query condition in hook_views_query_alter(), and the protected $where condition prevent me to do this. Just path for those have the same request.

sonnykt’s picture

@guaneagler you can use the getter:

$where = &$query->getWhere();

and just modify the $where object.