2

I needed get field's value and pass it as contextual filter to "entity reference view" just described as views and contextual filters with entity reference field.

But $Node = menu_get_object() is not retuning node object.

The scenario

Nodes Items of custom content type

Title : Sony LCD
Tags : electronic

Title : The godfather
Tags :Books
.
.
Items(n)

Node of custom content type Review. It's node id is 10

Title : Make a Review
Filter Tags : Taxonomy terms reference (e.x electronic, books, movies etc)

Attach a entityform to above node using entity reference and also back referenced with enityreference_current

My Review Item : EnityReference field - Entity reference view - Autocomplete widget
Review : text area

When node/10 is viewed, user will have to fill "my review item",but should be filter based on "Filter tags" that was already set.

Code inside VIEWS PHP under Contextual Filter

$node = menu_get_object ();
$ Related_ids = array ();
    $ Related = field_get_items ('node', $node, 'field_filter_tags') // Get the term reference field
    if ($related && is_array ($related) && sizeof ($related)> 0) {
      for ($i = 0; $i <count ($related); $i++) {
        $ Related_ids [] = $related [$i]['target_id'];
      }
    }
    return implode (',', $related_ids);//to perform AND 

Update : I also tried with $node= node_load(arg(1)); , this time $node variable set but not type of node. Then I dpm(arg(1)); it shows string autocomplete(yes just autocomplete not string entered in field).

8
  • menu_get_object gets a loaded object from a router item. If you call it when non-node is displayed, it simply cannot return node. Where exactly do you call it? And what exactly does it return?
    – Mołot
    Commented Aug 30, 2013 at 10:05
  • @Mołot entityform is attached to node like mentioned in this thread.Entity Form submission is done via node(in above case node/10).That means node object is displaying, am I correct?. In php code I am checking if(isset($node)) return 1; else return 0; and it returning 0.
    – kiranking
    Commented Aug 30, 2013 at 10:24
  • That should mean it. But if the attached part is rendered separately, that connection might be lost inside. Try to dpr($node); in your code and tell us what really is there, OK?
    – Mołot
    Commented Aug 30, 2013 at 10:27
  • dpr($node); is not showing anything but, notice shown Notice: Undefined variable: node in eval() (line 8 of C:\server\www\mysite.local\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(53) : eval()'d code).Another thing menu_get_object() is called without parameter as default is 'node'.Is it possible to use entity_metada_wrapper inside view's php code section to get node?
    – kiranking
    Commented Aug 30, 2013 at 10:49
  • If dpr(menu_get_object()) also fails, we have a problem. What you say about entity_metada_wrapper sounds reasonable, but I can't tell now, sorry.
    – Mołot
    Commented Aug 30, 2013 at 10:57

1 Answer 1

1

The entityreference autocomplete paths are defined in entityreference_menu() as:

  • entityreference/autocomplete/single/%/%/%
  • entityreference/autocomplete/tags/%/%/%

The wildcard arguments are field name, entity type and bundle respectively. The entityreference_autocomplete_callback() function also accepts an optional entity id and string parameter (the last being the search string).

So while you're expecting the path to be node/[nid], it'll actually be one of the above - which explains why arg(1) is giving you the string autocomplete.

I haven't tried it but you should be able to use arg(6) to get the node ID in your current context.

3
  • I checked with arg(6) but failed to get node. I also checked from arg(1)_ to arg(10) and dpm that. The last entity that is fetched was "st" beyond that every args returns empty.I didn't get what exactly st here.arg 1 autocomplete | arg 2 single | arg 3 field_select_books | arg 4 entityform | arg 5 add_book | arg 6 NULL | arg 7 st
    – kiranking
    Commented Sep 1, 2013 at 9:18
  • When autocomplete field is moved from entityform to its parent node then arg(6) is returning node as you told. ReviewNode title,tags,{entityform [aucomplete entity reference view]} - doesn't work ReviewNode title,tags,[aucomplete entity reference view],{entityform} worked. From this what I am getting is, it is not further backtracing beyond entityform to get its parent element :(.
    – kiranking
    Commented Sep 1, 2013 at 9:30
  • I think the problem is with entityform when it is attached node.Some issues @ D.O drupal.org/node/2043287#comment-7750131 , drupal.org/node/2244987 , drupal.org/node/2078573
    – kiranking
    Commented Jun 1, 2014 at 20:36

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.