Get the current range values from a QueryAlterableInterface As far as I can see this isn't possible outside of QueryAlterableInterface, because the $this-&gt;$range variable is protected, and the range() method can only be used as a setter.

I suppose I could implement my own class that extends QueryAlterableInterface and adds in a method to expose the protected field... but I would have no idea how to get Drupal 7 to use the new class instead of its core one. Not to mention this would be quite over-kill.

Any better ideas?

Context

I am attempting an external (i.e. outside of hacking the Reference module) general code fix for something similar to the following issue:

References module doesn&#39;t specify/read a pager limit when field is using a view to generate potential references

I have come across another instance where this is causing my site a problem, but I can't apply the specific fix found in the Q/A above. Mainly because the code takes a different route through the References module. Rather than my field using a view to generate its "potential" reference values &mdash; as in the previous Q/A, which makes it quite identifiable &mdash; it is instead just a field that relies on a direct query to source its values. This direct query still has the same problem of attempting to list far too many values, to the point of timing out.

My plan is to detect the particular query involved, and see if it has no limit applied, and then set a safe limit &mdash; so that the views UI doesn't attempt (and fail) to generate thousands and thousands of checkboxes. I can probably find a way to detect the specific query, but I don't think I can tell if a limit is set or not.

function hook_query_alter(QueryAlterableInterface $query) {
  if ( $query-&gt;getMetaData(...) == ...) {
    $query-&gt;limit(); /// this does not return the limit value.
    $query-&gt;limit; /// triggers an protected access error.
  }
}




A different way to find this question

I realised that with the way I've posed this question, only those who know the underlying cause will be able to find a possible solution. So just as an aid to those who might searching for this specific problem &mdash; as it is easy to reproduce if you have a good sized database &mdash; here are some key search terms.

Unable to add a filter to a View for a Reference field

When attempting to add a Reference field as a filter for a View, using the Views UI, the Ajax request seems to hang. After max execution time triggers the following error:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /admin/structure/views/ajax/config-item/related_nodes/default/filter/field_node_related_nid
StatusText: parsererror
ResponseText: Fatal error: Maximum execution time of 60 seconds exceeded in /var/www/drupal/includes/common.inc on line 6642
Luckily (or thankfully) the Reference module adds some rather useful information as meta data to the query in question. Making it easy to spot in terms of its id (although you have to watch out that the id is prefixed with a whitespace character). This is the same id for all similarly made queries. It also records the $options array, which means we have a reference to the ['limit'] value without going through the $query object.

I've set the following to work for any field, but have left reference to the $field in use just in case it is useful for others. For my purposes 300 rows is a suitable default to have, rather than just accepting an infinite number or records.

Whilst this doesn't access the limit/range value from the query itself, it does solve the problem in quite a general way.

/**
 * Implements hook_query_alter().
 */
function HOOK_query_alter(QueryAlterableInterface $query) {
  /// trim due to typo in references module (took me a while to spot this)
  $id = trim((string) $query-&gt;getMetaData('id'));
  if ( $id === '_node_reference_potential_references_standard' ) {
    $field = $query-&gt;getMetaData('field');
    $options = $query-&gt;getMetaData('options');
    if ( !empty($options) &amp;&amp; (empty($options['limit']) || !$options['limit']) ) {
      drupal_set_message('Modifying Node References query to ' . 
        'protect against too many values (limited to 300 values)');
      $query-&gt;range(0, 300);
    }
  }
}