1

Let me explain my scenario at first.

I have two content types in my site.

First is products and second is producer.

Each product has only one producer and each producer has one or more products.

Now in every product page, I want to show the producer information. I made a view block for this issue and it works well.

but in the product page, I also have a plan to show all products of the producer of current product in another block.

In order to show the product nodes of a specific producer I need the node ID of producer content type.

I tried to use Contextual filters --- Provide default value "When the filter value is NOT available" via Content ID from URL but this function returns node ID of current product not producer id. (because we are in a specific page of single product)

Now I am searching to find a solution for this issue.

thanks a lot for your guidance.

1

Say, the term reference field in the product content type is called "field_ref_producer". You need to have this block of code, and a view that accepts a contextual filter for "term reference producer ID" ready.Let this view be called "other_products_producer"

$node = menu_get_object();
$output = '';
if(is_object($node)) {
  $producer_id = $node->field_ref_producer['und'][0]['tid'];
  $output = views_embed_view('other_products_producer', 'default',$producer_id);
}
echo $output;

Basic idea is, grab the node object from URL, get the producer tid from it and then embed a view passing the producer nid as contextual filter to the view.

Update:

Code to put in views PHP (default value of contextual filter):

$node = menu_get_object();
if(is_object($node)) {
  $producer_id = $node->field_ref_producer['und'][0]['tid'];
  return $producer_id;
}

Please replace the name of the field to what you have in your content type.

  • Hello Please tell me should I use your mentioned block code in default value of "contextual filter" as a PHP code? – Mehrdad201 Oct 7 '13 at 7:16
  • I don't normally suggest using PHP code in views. but in this case, I think it will be much easier. I am updating the code that you should put in there. – Neo Oct 7 '13 at 7:23
  • Updated the code that you can use. hope that helps. – Neo Oct 7 '13 at 7:31
  • Thanks a lot for your solution. It works well but I did a tiny modification on it that in next post I will write it because in comment format is not available for Drupal codes. – Mehrdad201 Oct 7 '13 at 8:58
1

If the entity reference field is in the producer content type, this solution might work:

Add another field in your view - "the entity reference field" (i.e.field_ref_producer if your entity reference field is called field_ref_producer).

Use the relationship : Entity Reference: Referenced Entity

Use the contextual filter Content:nid Provide default value "When the filter value is NOT available" via Content ID from URL. (This function returns node ID of current product not producer id.) "

When you render the view, you will get a list of the products from the producer.

What this does is, it essentially (w.r.t. to the above case) uses the product id to locate the producer id (using the entity reference field) and then display the products ( i.e. all referenced entities) associated with the producer id.

0

Here is modified codes from Neo.

$node = menu_get_object();
if(is_object($node)) {
    if(isset($node->field_producer['und'][0]['nid']))
    {
      $producer_id = $node->field_producer['und'][0]['nid'];
      return $producer_id;
    }
}

I added isset function for the nodes that don't have specific referenced field producer. (for example in pages or articles)

Thanks a lot Neo for you help.

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.