I'm looking to simply use my own query on a given node_reference field. I'd rather not use Views (not sure if that's even available yet), because the last time I checked, Views doesn't (out of the box, at least) support OR filter conditions (something I need)..

I've looked around in the code and can't seem to find a hook or method to alter the query. As of now, the only way is altering the form with hook_form_alter(), but that seems inefficient.

Possible?

Thanks

Comments

wodenx’s picture

Old thread, I know, but how about hook_query_alter() see this discussion.

wodenx’s picture

Category: support » feature

Actually, as I think about, that solution is problematic because there isn't enough context. It would be great if the $field object (and maybe the $options as well) could be added to the query as metadata - something like:

   $node_type_alias  = $query->addField('n', 'type',  'node_type');
   $query->addTag('node_access');
   $query->addMetaData('id', ' _node_reference_potential_references_standard');
+  $query->addMetaData('field', $field);
+  $query->addMetaData('options', $options);

   if (is_array($field['settings']['referenceable_types'])) {
     $query->condition('n.type', $field['settings']['referenceable_types'], 'IN');

in _node_reference_potential_references_standard(), after line 752 of node_reference.module.

The Views integration is terrific, but sometimes you just want to write a lean query without all the overhead - especially for sites that have no other need for Views.

geerlingguy’s picture

Ditto and subscribe. Would be an awesome feature, and the query alter idea would probably be the most robust way to do it... I think.

One way to hack around this currently would be to set up a view to do most of the grunt-work, then use a query alter on the actual views query to go the extra mile. Of course, you'd still need to use Views to get started (but seriously... who doesn't have Views installed on a Drupal site? ;-)

mstef’s picture

Alright, Views3 does have OR filters, but this could still be a decent feature.

rickvug’s picture

In regards to #2 see the patch at #1256280: addMetaData for query in node_reference_field_prepare_view() which adds more meta data to the query so that it can be properly targeted using hook_query_alter. The patch looks good to me but it would be nice to have more feedback before marking it RTBC.

s_leu’s picture

Status: Needs review » Active
StatusFileSize
new664 bytes

made a patch containing the proposed additions of #2. closing #1256280: addMetaData for query in node_reference_field_prepare_view() as duplicate.

berdir’s picture

Status: Active » Needs review
berdir’s picture

Status: Active » Reviewed & tested by the community

Disclaimer: s_leu and I are working together.

The change is simple enough and allows you to do field specific alterations without having to mess with views.

Edit: Note that the issue that was marked as a duplicate added an id (something that the module already does ) but didn't add the options. That's why we've chosen this approach.

yched’s picture

Status: Reviewed & tested by the community » Fixed
StatusFileSize
new1.71 KB

Sure, why not - just in time for the 7.x-2.0 release :-)

Added the corresponding in user_references, committed the attached patch.
Thanks !

Status: Fixed » Closed (fixed)

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