1

I am trying to get a list view in a panels node template to show content based on a text field of the currently viewed node.

Just to give you more information I am including my setup below:

Content Type A - textfield called 'ref'. Content Type B - textfield called 'a_ref'.

Content Pane View to show list of Content Type B but filtered depending on the node being viewed.

Node template on Content Type A with two regions (Left and Right).

On the Left I am displaying fields of the node being viewed. On the Right I want to show the Content Pane View with a list of Content Type B where 'a_ref' is equal to 'ref'.

I am trying to figure out how to use contextual filters between the node template and content pane but to no avail.

I hope anyone out there can enlighten me on the way it should be done.

Thanks in advance for any help,

Kevin

  • The phrasing of your question makes it way more localized than it needs to be. Can you get any view to list content based on a field value? That's where you need to start. – Letharion Nov 29 '12 at 18:52
  • I'm glad someone actually takes the time to improve their questions :) Reversed vote. – Letharion Nov 30 '12 at 8:07
  • I try my best not to leave an Internet mess in the forums. Thanks. – Kevin Carbonaro Dec 2 '12 at 18:01
  • Still trying to work this one out. I am in talks with our web host programmers (they might just do it for free) and hopefully will have a solution soon. I will document how we did it as soon as it is done, hoping to help any others who have the same or similar question. – Kevin Carbonaro Dec 18 '12 at 12:41
  • Are you aware of how to use contextual filters? If I'm understanding your question right, configuring the View to filter on a field value should be really easy. Like, less than 30 seconds easy. – Letharion Dec 18 '12 at 12:52
1

Solution

  1. Get the view, and the view alone to do what you want. You need a contextual filter on the field you want to match against. Test the view out in the views UI.
  2. Add a content pane display, this requires the views content module to be enabled. In the pane settings, find argument inputs settings (Sorry, no UI in front of me, that setting could be something else). In here you can tell Views what type of data the View should expect for the contextual filter. Pick From context. You'll be presented with a looong list of available options, it's easy to get this wrong, be careful. Scroll down until you under Node find the field you are trying to match against. If you have multiple fields with the same label, there is no easy way to know which is which. Yes, it's crap, you will have to go trial-and-error until this gets fixed. Save the view.
  3. Go into Panels. Ensure that the new view is visible under the View panes category, not just Views. Add the view, tell it which node to get data from, which is usually just one, save.

Troubleshooting

This setup is non-trivial for a lot of users. I find that the common most problem people hit is configuring the view to require a certain type of input. If you can't get this to work, there are two things to try.

  1. The Panels process for identifying which Views are valid, seemingly can get stuck sometimes. Clear caches, and resave both the View and Page.
  2. In the Views arguments configuration, set the argument to be optional. This is not really what you want, but if this causes the View to show up in the Panels UI, then we know the problem is that Panels does not have access to the information that the View says it wants, which at least narrows the problem down a lot.
  • I will try this tomorrow during office hours (shhh don't tell my boss LOL). I think I may have tripped on step 2 with the looong list (tried around ten different options), but your explanation is quite clear on what is what and what should be chosen and am positive that it will help me solve this riddle. Will come back here as soon as I have an update. – Kevin Carbonaro Dec 2 '12 at 17:59
  • Good luck :) I added a short note to point 2 about having multiple fields with the same label. – Letharion Dec 3 '12 at 9:00
  • I think I'm almost there.I just found out that the field that I want to match to has slashes '/' (e.g. MCA/C/12-1306) and Drupal separates the arguments using slashes. Is there a way to avoid this problem without having to replace all slashes with dashes (e.g. MCA-C-12-1306)? – Kevin Carbonaro Dec 4 '12 at 11:24
  • That sounds like a separate question. I saw a very similar one about # in URLs, but I can't find it at the moment. – Letharion Dec 4 '12 at 12:32
  • I have confirmed that '/' do not work with contextual filters and I cannot make changes to the way our document references are generated. Still I did some tests to see how this would work if the slashes were replaced with dashes. I managed to get everything working except when I added everything in Panels (Step 3). I managed to get our web host programmer to do it for free :) but it was done through the PHP filter in a block which I placed in the panel. I should be able to document about it later on. – Kevin Carbonaro Dec 20 '12 at 12:43
1

An alternative approach is possible using simply Views, Blocks and a bit of PHP inside the views configuration page.

Perhaps you could try using the views option 'Views Contextual Filter' and some inline php to set the default value for the contextual filter. The contextual filter allows you to use some PHP to rewrite the value passed to the filter.

I have to admit it does feel a bit like stepping back in time with Drupal and using PHP snippets, but it provides some nice configurability with only 'Views' and 'Blocks'.

My suggestion is to create a View listing 'Content type B' in a block.

Then add a contextual filter on 'a_ref'. In the contextual filter select 'provide a default value' and 'php code'.

The php code will read arg(1), load up the node object and return the value of the field.

A quick way to test out the behavior of the contextual filter with default and PHP is with one link of php containing a static value, eg.

return 'some known for ref';

Then attach the block in the appropriate pages using normal block interface.

In one of my sites, I'm using this to parse the url, extract a numeric userid, and load the user object. I think you may be able to adapt this to load the node object instead and access the field that you require.

// snippet to include in text field in views default value, provide a value, php
if (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 1) {
  // note - >1 not >0 so excludes admin

  if ($related_user = user_load(arg(1))) {
     return $related_user->mail;
  } else {
    return false;
  }
} else {
  return false;
}

Perhaps you could adapt this using node_load() instead of user_load(). A quick route would be to use $node->field_ref['und'][0]['safe_value'] to access first value in this field.

I've not tried out using a multi value field - perhaps something along the following lines would work. This code below needs some further work, but it might get you started.

// snippet to include in text field in views default value, provide a value, php
$nid = arg(1);
if ($arg(0) == 'node' && is_numeric($nid) && $node = node_load($nid)) {

  // optionally add some more validation - $node->type or similar

  $field = field_get_items('node', $node, $field_name);
  $field_name = 'field_ref';

  if (count($field) > 0) {

    $output = array();

    foreach ($field as $delta => $f) {
      $value = field_view_value('node', $node, $field_name, $field[$delta]);
      // fix $value here using regexp or str_replace to replace unwanted slashes
      $output[] = $value;
    }

    return implode(', ', $output);
  } else { 
    return false;
  }
} else {
  return false;
}
  • Thanks, will try this route as the previous one although very helpful (can understand contextual filters much more) I could not make it work. I'm still learning PHP but can understand the logic and sometimes yes - one has to take a step back in time and get dirty to get something functional :) – Kevin Carbonaro Dec 8 '12 at 14:23

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.