1

I have a view that shows all of a particular user's content (content the user has created on the site). This works fine, but I need to link the results (title field) to the pages where the content is displayed - not to the original node of the created content.

Since the content types may be different, I guess I need to set the URL's programmatically.

For example one content type is "projects" projects are displayed at site.com/projects/!arg

Another content type is "photos" photos are displayed at site.com/photos/!arg.

How do I write a conditional that will set the URL's correctly?

  • What mechanism are you using to display nodes at site.com/projects/!arg? Is that a view? A taxonomy page? – Coleman Nov 6 '14 at 20:15
  • It is a panel page with views. – Finn Nov 6 '14 at 20:16
  • And what does !arg represent? – Coleman Nov 6 '14 at 20:17
  • !arg is the nid. And actually there are two arguments. I only showed one for simplicity sake. I am also passing the uid as an argument for use in another panel. – Finn Nov 6 '14 at 20:17
1

I don't recommend to use views php module,

Menu item with callback:

The idea here is to override all the links of the titles in your view with one link which pass an argument to custom menu callback, then in the callback, you write your PHP code which does the routing.

to implement this solution you need to do the following:

1- Override the link in the view

Make link to your own path

2- Create callback using hook_menu():


/**
 * Implements hook_menu().
 */
function mymodule_menu() {

  $items['my-routing/%node'] = array(
    'title' => 'Custom callback',
    'page callback' => 'my_custom_callback',
    'page arguments' => array(1),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

3- Create your custom callback to do the routing:



function my_custom_callback($node) {
  switch ($node->type) {
    case 'product' :
      drupal_goto('project/' . $node->nid);
      break;
    case 'post' :
      drupal_goto('blog/' . $node->nid);
      break;
  }
}
0

You will need to add several fields to the view and then "exclude from display" so you can use them as tokens: Node type, node id, and node user id.

Then you can change the title field of the node to "output as link" and use the above tokens (views will tell you what they are but assuming they are [node_type] [nid] and [uid]) you would construct the link like: [node_type]/[nid]/[uid]

That works unless you need extra logic to determine the path (if tokens don't match up directly to url arguments). In that case you need the Views Php module, which will allow you to inject php code into the field.

  • the only problem is that I was again simplifying for clarity's sake. The actual name of one of the content types is "add a project". So it doesn't really correspond exactly with the URL. – Finn Nov 6 '14 at 20:39
  • I will need to incorporate the views php module. I only have a little experience with php (and programming) so I am not sure how to find the variables I will need (content type for example) or how to specify the resulting URL to be used as a link. – Finn Nov 6 '14 at 21:16
  • This related answer may help you. – Coleman Nov 6 '14 at 22:10
-1

Here is how I got this to work. The code may not be the most elegant due to inexperience, but it does work.

I added node type, node id and author user id fields to the view and set them to exclude from view. Then using a global php field (provided by the "views php" module) I used the following code:

if ($row->type == "single_house_room_photo") {
  print  '<a href="/rooms-houses/' . $row->title . '/' . $row->nid . '/' . $row->uid . '">  ' . $row->title . ' </a>';
} elseif ($row->type == "add_a_product") {
 print  '<a href="/products/' . $row->title . '/' . $row->nid . '">  ' . $row->title . ' </a>';
} else {
 print '<a href="/rooms-houses/' . $row->title . '/' . $row->nid . '/' . $row->uid . '">  ' . $row->title . ' </a>';
}

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.