0

I'm creating an online store using Ubercart. I have Views and Panels installed. I'm using the Page Manager and Panels to configure each of my pages. I've modified the default view to accept taxonomy terms as arguments.

My question is simple, "How do you pass arguments from Panels to a View?"

I followed the direction's at this URL, but, in the process, actually discovered a much better way (I posted the solution in the article, but I'm also going to post it here):

[a link] http://itscommonsensestupid.blogspot.com/2009/04/passing-arguments-from-panel-to-views.html)

I will post the best solution to this question in a couple of hours.

5
  • 1
    Hello, and welcome to DA! :) I mean to be helpful, but my comment is going to be negative. I'm sorry to welcome a new user like that. With that said: Except that steps 7 and 8 don't make sense to me, the blog post is right about how to do it. The solution you have commented with however, is deeply flawed. Your use of arg(1) needlessly hard codes the argument order as well as completely bypasses the validation that the panel argument is valid for the view. It relies on eval(), which you should never do, and just plain doesn't work with any relationship.
    – Letharion
    Commented May 18, 2012 at 20:59
  • I didn't write the tutorial on the Website.
    – dqfan2012
    Commented May 18, 2012 at 21:01
  • I realize that, but based upon 1) you saying you posted on that blog post, 2) the post date, and 3) the use of the ubercart tag here and mention of using ubercart in the comment, I assume that what is currently the last comment there, is your solution.
    – Letharion
    Commented May 18, 2012 at 21:04
  • That is correct. When my Shopping Cart page is using one level of Taxonomy /Shop/Terms The hard coding that you're talking about isn't a horrible solution.
    – dqfan2012
    Commented May 18, 2012 at 21:19
  • I certainly could understand your point if my Shopping Cart were more complicated.
    – dqfan2012
    Commented May 18, 2012 at 21:20

1 Answer 1

1

Example scenario:

There is a panel containing View Content Pane.

Panel URL: /panel_url

Example with argument: /panel_url/term_name

With BEF: /panel_url?category=123

How to set up (having existing panel and added Content Pane):

  1. Add Contextual filter to the View. E.g.: Views contextual filters taxonomy name
  2. Change settings in 'View -> Pane settings -> Argument input' to load the argument 'From panel argument' (e.g. First).

Done.


In addition, if you're using Better Exposed Filters, you could do the following improvements.

To set the current active link based on the argument (if you're using select_as_links):

/**
 * Implements hook_preprocess_[themeFunctionName]().
 */
function foo_preprocess_select_as_links(&$variables) {
  $options = isset($variables['element']['#options']) ? array_change_key_case(array_flip($variables['element']['#options']), CASE_LOWER) : array();
  $selected = end((arg()));
  if (array_key_exists($selected, $options)) {
    $variables['element']['#value'] = $options[$selected];
  }
}

To set the current active link based on the argument (if you're using select):

/**
 * Implements hook_preprocess_[themeFunctionName]().
 */
function foo_preprocess_select(&$variables) {
  switch (arg(0)) {
    case 'panel_url':
      if (@$variables['element']['#name'] == 'DROPDOWN_NAME') { // name of your select list
        $options = isset($variables['element']['#options']) ? array_change_key_case(array_flip($variables['element']['#options']), CASE_LOWER) : array();
        $selected = end((arg()));
        if (array_key_exists($selected, $options)) {
          if ($variables['element']['#value'] <> $options[$selected]) {
            $variables['element']['#value'] = $options[$selected];
          } else {
            // drupal_goto('panel_url');
          }
        }
      }
    break;
  }
}

To make the links SEO friendly, you can try something like:

/**
 * Implements hook_url_inbound_alter().
 */
function foo_url_outbound_alter(&$path, &$options, $original_path){
  if (arg(0) == 'panel_url') {
    // Convert URL like: /panel_url?category=29 into /panel_url/term_name (SEO friendly)
    $path_info = parse_url($path);
    switch (@$path_info['path']) {
      case 'panel_url/term_name':
      case 'panel_url/term_name2':
        $path = 'panel_url/all'; // Convert loopy urls into base url
    }
    if (strpos($path, '?category=') !== FALSE) { // Convert term id into human name
      list(, $tid) = explode('=', current(explode('&', $path_info['query']))); // get tid from URL
      $term_name = strtolower(taxonomy_term_load((int)$tid)->name);
      $path = '/panel_url' . '/' . $term_name; // See also: $path_info['path']
    }
  }
}

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.