How to "Add a Lesson" link to my Course Content Type so that the [nid] substitution works even after destination redirect ?

Events happening in the community are now at Drupal community events on www.drupal.org.
FergalMohan's picture

Hi all, I'm working on a site that has 2 simple content types, Courses and Lessons. The Lesson have a Node Reference to the Course that they belong to and I have a View that displays the Course with a Table of Lessons (View Attached to the bottom). I have added a "Add Lesson" link in the footer as follows to allow the Course Creator to easily add another lesson:-

<?php
echo l( 'Add Lesson', 'node/add/lesson/[nid]', array('html' => true, 'query' => 'destination=practice/[nid]') );
?>

When viewing the Course and clicking on the "Add Lesson" link it seems to work the first time the lesson is added, courtesy of the nid being available as a field for replacement but once the lesson is added and the redirect to the destination happens the [nod] substitution in link no longer works. I'm still relatively new to Drupal so I'm probably not doing this the "best" way but I'd appreciate any guidance on fixing this or on how to get something like it to work ?
Fergal

Comments

I'd use a combination of

Chris Graham's picture

I'd use a combination of Nodereference and Nodereference By URL.

That second one gives you a widget option on the CCK field to tell it to get the reference by URL and then you can specify that it redirects to the referenced node (course) once the new node is created (lesson).

You can tell it to put an "Add lesson" link in the Drupal links field for the course, or if you want to manually place it somewhere in the template to style it up differently then you can use this function to print the link on your course node:

<?php
/**
* Helper function for themers to easily create a link.
*
* This function should be used in custom themes, rather than making manual
* links because it first checks a user's access before showing the link. If
* the user does not have access to create the node then an empty string will
* be returned.
*
* @param $node
*   The node object that will be referenced.
* @param $field_name
*   The name of the Node Reference field.
* @param $type_name
*   The name of node type that contains the Node Reference field.
* @param $attributes
*   Optional. An array of additional attributes to add to the link.
*/
function nodereference_url_create_link($node, $field_name, $type_name, $attributes = array()) {
 
$output = '';
 
$field = content_fields($field_name, $type_name);
 
$field['widget']['node_link']['full'] = TRUE;
  if (
$link = nodereference_url_build_link($node, $field)) {
   
$options = array();
   
$options['attributes'] = $attributes + (array) $link['attributes'];
    if (
$link['query']) {
     
$options['query'] = $link['query'];
    }
   
$output = l($link['title'], $link['href'], $options);
  }
  return
$output;
}
?>

So, for example, assuming that your Lesson content type has a CCK nodereference field called field_course using the NodereferenceByURL widget, put this in your Course content type somewhere:

<?php
print nodereference_url_create_link($node, 'field_course', 'lesson');
?>

You can then sit back and not worry about destinations etc.

D6 or D7

pepemty's picture

Hello, Chris!
Does this snippet work for D6 or D7 --or either?

Warm regards from sunny México!

:-)
Pepe

Hi Pepe. The first bit of

Chris Graham's picture

Hi Pepe.

The first bit of code was just the function that is provided with Nodereference URL, you only need second snippet with the one line in it.

The function looks like it hasn't changed from D6 to D7, so it should be the same with both.

The only difference with D7 is that you don't have CCK, fields are now in core. You need to download the References module to give you the ability to use the nodereference field widget.

Cheers,
Chris

Thanks, Chris! I've been

pepemty's picture

Thanks, Chris!
I've been trying to solve this one for over a week. Now I can almost put a check mark on this one.

Thanks again!

Warm regards from sunny México!

:-)
Pepe

maybe try using arg(1) to

btopro's picture

maybe try using arg(1) to ensure that it's always the nid that's active like 'node/add/lesson/'. arg(1)

that'll always give you the node id thats in the arguments for the address.

As for node reference adding make sure that you have the prepopulate module installed http://drupal.org/project/prepopulate I have something with galleries and images being added in a similar way in the Collaborative learning environment if you'd like to see hack apart a working example http://drupal.psu.edu/content/134

Nodereference By URL

md2's picture

Yea I'd agree with Chis, a combination of combination of Nodereference and Nodereference By URL is your best bet.

Thanks guys, that helps a

FergalMohan's picture

Thanks guys, that helps a lot.

Ireland

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week