I'm building an issue tracker. Got the two content types project and issue. Now, when a user creates a new issue, he has to select the associated project (Node Reference field).

I'd love to offer a link at each project node, which opens the "issue creation form", and where the associated project is already filled in. So that the user does not have to select from a lot of projects.

So, I would offer these 2 workflows:

  1. Create content -> Issue -> select project
  2. open project node -> "Create a new issue for this project"

Comments

arhak’s picture

lets say you made a module named mytracker
I'll write down some functions for adding the functionality you desire

NOTICE: that this code is from an OLD drafted module of my own, it was a tracker made with cck
therefore, this solution might not be the best solution, but it is a working solution (that module is still in use on an intranet issue tracker)

I cut off some extra code, I hope it works, since I won't check for possibly introduced bugs, just let me know if there was any missing function (also I renamed functions to use "mytracker" module name)


function mytracker_preprocess_node(&$vars) {
  if (isset($vars['type'])) {
    $type = $vars['type'];
    if ($type == 'project' || $type == 'issue') {
      $nid = $vars['nid'];
      switch($type) {
        case 'project':
          $project_nid = $nid;
          $add_issue_text = t('Add Issue');
          break;
        case 'issue':
          $project_nid = $vars['field_project'][0]['nid'];
          $add_issue_text = NULL;
          break;
      }

      if (empty($project_nid)) {
        $add_issue_link = '<span class="orphan orphan-issue">('. t('Orphan issue') .')</span>'; 
      } else {
        $add_issue_link = _mytracker_get_link_add_issue_to_project($project_nid, $add_issue_text);
      }     
      $wiki_link_name = '[#'. $nid .']';
      
      $vars['submitted'] = '<b>'. $wiki_link_name .'</b> '. $add_issue_link .'<br/>'. $vars['submitted'];
    }
  }
}

function _mytracker_get_link_add_issue_to_project($nid, $link_text = NULL) {
  if (!_mytracker_is_project($nid)) return _mytracker_get_link_new_issue();
  $node = node_load($nid);

  $link_title = t('Add Issue to project') .' [#'. $nid .'] '. $node->title;
  if (empty($link_text)) {
    $link_text = $link_title;
  }
  $attributes = array('title' => $link_title);
  
  $query_string_args = array('project' => $nid);
  return l($link_text, 'node/add/issue', array('query' => $query_string_args, 'attributes' => $attributes));
}

function _mytracker_get_link_new_issue() {
  $link_title = t('New Issue');
  $link_text = $link_title;
  $attributes = array('title' => $link_title);
  
  return l($link_text, 'node/add/issue', array('attributes' => $attributes));
}

function _mytracker_is_project($nid) {
  $node = node_load($nid);
  return ($node->type == 'project');
}

no2e’s picture

Hi and thanks, arhak.

lets say you made a module named mytracker
I'll write down some functions for adding the functionality you desire

Hm, unfortunately I'm no developer and I don't understand PHP :/

What should I do with the code you provided?

arhak’s picture

I misunderstood, you said I'm building an issue tracker and I assumed you were a developer, now I realize you are building it from the UI

well, you'll need to make a module to tweak what you want
can't promise anything... I'll see later

basically, what I wrote down is almost everything, it just needs an empty module shell to be wrapped into

can you get help from some developer
also you might read any eBook/tutorial about making modules, just create an empty mod and drop that code in it, contact me for troubleshoting if you get stuck

sorry, but right now I'm in the middle of something...

arhak’s picture

I'll probably might help you tomorrow
in advance, answer the following:
- machine name of your project content type (is it "project"?)
- machine name of your issue content type (is it "issue"?)
- does your issues have a nodereference field referencing their parent project?
- machine name of that cck field? (is it "field_project"?)
- can your issues have no parent project?
- can your issues have more than one parent project?

no2e’s picture

I misunderstood

Ah, sorry for that. Yes, I'm building it from the UI.

  • machine name of your project content type: project
  • machine name of your issue content type: issue
  • does your issues have a nodereference field referencing their parent project: yes
    • machine name of that cck field: field_project_associated
  • can your issues have no parent project: no
  • can your issues have more than one parent project: no

I wonder, if there might be a contributed module for that use case? I can't be the only one needing this ... ? I found these modules, but I'm not sure, if they could be used for my case:

arhak’s picture

I'll do a module that will fit your needs
but I won't be able to contribute it to drupal.org since I can't use CVS due to firewall restrictions

would you be able to commit it to drupal.org contributed modules?
otherwise give me an e-mail address to send you the module attached (once it gets ready, I'm about to begin develop it)

arhak’s picture

yes, prepopulate is part of the way to go

WorldFallz’s picture

For just a create content link with the referenced node already filled in, nodereference_url is exactly what you need. No muss, no fuss, no custom code.

If you want to prepopulate more than just the nodereference, then probably prepopulate will do the job.

arhak’s picture

yes, but it's missing the "Add a new issue to this project" link

WorldFallz’s picture

There's an option to add the link along with what text the link should use.

arhak’s picture

yes, it is already implemented

no need of prepopulate module
just with nodereference_url

- once created the field nodereference choose the widget "Reference from URL"
- configure checkboxes "Create link on the teaser view" and "Create link on the full view" as you please
- set the "Link title" to something like "Add new issue to this project"

that's it

no2e’s picture

wow, cool. nodereference_url works like a charm. didn't know that it provides functionality for creating the link. thank you :)

arhak’s picture

yeah, big thanks to WorldFallz
I was about to implement exactly the same existing module

arhak’s picture

I provided a patch #571994: token support for "Link title" and "Link hover title"
to support links like "Add a new Issue to this Project" by means of token support "Add a new [target-type] to this [type]"
also there is token support for [title], [nid], and remaining nodereference's tokens

WorldFallz’s picture

awesome-- excellent idea.