Summary

This is a simple but powerful module for Drupal 7 & Views 3.x to convert a view argument's node id into its url path.

There is a similar solution for terms in views core, however there is no in-code solution to handle contextual filters for nid. One possible solution is to use in-line PHP in the contextual filter, but I really don't like in-line PHP and when I created this module, I did so because we use that same logic in several different views. Developing this into a module means there is less code in the world.

Project Page

Views URL Path Arguments

Git Repository

git clone http://git.drupal.org/sandbox/lucashedding/1872408.git views_url_path_arguments

Requirements

Views

Installation

Install as usual. See Installing contributed modules (Drupal 7) for further information.

Configuration

  • Configure contextual filters in Administration » Structure » Views » view:
    • Add a contextual filter i.e. Content: Nid
    • When the filter value is available or a default is provided » Specify validation criteria: Url path alias

Review for this Project

http://ventral.org/pareview/httpgitdrupalorgsandboxlucashedding1872408git

Reviews of Other Projects

More coming soon... BTW, these reviews are really fun! I'm might have to start doing more of them when I get a free moment.

Comments

vladimir-m’s picture

Hello heddn,

Thank you for great module.

1. Please add git link corresponding to non-maintainer like "git clone http://git.drupal.org/sandbox/lucashedding/1872408.git views_url_path_arguments"
2.Please review your module from coder module and see the automated code review http://ventral.org/pareview/httpgitdrupalorgsandboxlucashedding1872408git
3.I suggest you to use in file: views_plugin_argument_validate_url_path.inc at line: 19, 31, function is_object() to check variable is an object.

Cheers!

vladimir-m’s picture

Status: Needs review » Needs work
alexmoreno’s picture

Status: Needs work » Needs review

As a recommendation, do at least three reviews of other modules to accelerate your process of approval.

I will check when I come back home, but the code seems too short to be considered for approval, please confirm because and in a mobile smartphone and I cannot download the code just now. Using drupalcode.org shows really few lines of code, maybe I am wrong (again, I will check in a few days, I'm just traveling).

Thank you for your effort.

heddn’s picture

Thanks for all the great feedback.

Comment: #2

  1. Is completed
  2. The only findings from PAReview show that I am following the views naming style, which causes an exception. I have resolved all other findings. If the consensus is that the class & method naming should be adjusted, I'm glad to oblige.
  3. I've switched to is_object()

Comment: #3

  1. Agreeded, the plan is to write some reviews.
  2. Sure, the number of lines isn't many but the usefulness of the module compels me to suggest it be promoted to a full project. You can find that I have provided many other patches over the past year and have commit rights, but not full project rights for htmlpurifier. There are several examples of good programming documented in the 2.x branch.
heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue tags: +PAreview: review bonus

PAReview: review bonus

heddn’s picture

For #4#2-3, I probably should change this back to if (!$node) because the interface for node_load is to return an object or FALSE. Why add all the extra overhead (though minimal) of is_object() ?

klausi’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -PAreview: review bonus +PAreview: single application approval

manual review:

  1. project page: the use case is not really clear to me - maybe give an example?
  2. Only 100 lines of code: This project is too short to approve you as git vetted user. We are currently discussing how much code we need, but everything with less than 120 lines of code or less than 5 functions cannot be seriously reviewed. However, we can promote this single project manually to a full project for you.

Otherwise looks RTBC to me. Removing review bonus tag, you can add it again if you have done another 3 reviews of other projects.

heddn’s picture

I'm working on adding a new feature to handle blocks i.e. views_plugin_argument_default. And will also beef up the project description with a more clear use case. This will hopefully push things over the 120 line / 5 method limit.

heddn’s picture

New features added and README / project page updated.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue tags: +PAreview: review bonus

Added more lines of code and performed additional reviews.

heddn’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -PAreview: single application approval

I'd like to get this module promoted through the normal processes.

tsphethean’s picture

Automated review looks good except for the naming convention issue discussed earlier.

README and Project page are now both more descriptive and describe the purpose/use case of the module.

Multiple reviews completed by the author with good comments.

The only thing I think which definitely needs looking at is that in views_plugin_argument_default_url_path(),

    if (ctype_digit($arg)) {
      $return = $arg;
    }
    // Is it a path alias?
    elseif ($nid = drupal_lookup_path('source', $arg)) {
      $return = $nid;
    }
    return $return;

could be simplified to:

    if (ctype_digit($arg)) {
      return $arg;
    }
    // Is it a path alias?
    elseif ($nid = drupal_lookup_path('source', $arg)) {
      return $nid;
    }

although even with this, there is a potential scenario where nothing will be returned - does this cause views any problems if drupal_lookup_path returns false for any reason? Alternatively if we're always going to get something relevant back from drupal_lookup_path then could do:

    if (ctype_digit($arg)) {
      return $arg;
    }
    // Is it a path alias?
    else {
      return drupal_lookup_path('source', $arg);
    }

Shouldn't stop approval but worth considering:

In validate_argument() in views_plugin_argument_validate_url_path, there are a couple of node loads. I can't decide if these are necessary, or if there should just be a query against the node table to check if the argument is a valid nid - it doesnt need to load the whole node as it only returns TRUE/FALSE, which for some node types with CCK fields etc could involve expensive queries (admittedly statically cached). You may at least be able to cut out the first node load. Don't think it's a show stopper in any case, but interesting to think about.

tsphethean’s picture

Status: Needs review » Needs work

Duplicate submission. Deleting
(I've done this a lot today. Comment button doesnt seem to be indicating its been clicked very well :-( )

heddn’s picture

Status: Needs work » Needs review

#12

Default argument plugin: The way it is working is correct. If nothing validates, then it uses the views default value of 'all'.

Validate argument plugin: You bring up a good point. I don't really need to perform a node_load(). I've switched it to do a query against the node table and that should be sufficient. I've also reworked the function to return a single value at the bottom of the function. That makes the code a lot more clear in the validate plugin.

tsphethean’s picture

Status: Needs review » Needs work

default argument plugin - understand, the simplified return values look good though and make the code much easier, good stuff.

validate argument plugin - I think you still needed the second node_load (after elseif ($nid = drupal_lookup_path('source', $argument)) {) as now $node->nid and $node->title are undefined and will throw warnings/not set correctly (unless you're dispensing with that info?).

For the query against the node table when nid is numeric, we should use db_select for D7 for compatability against other DB backends, so your syntax would be something like:

$result = db_select('node', 'n')
  ->condition('n.nid', $nid)
  ->limit(1) // as you only care if there's one
  ->execute();

if (count($result) > 0)  {
  // then we had a valid nid
  return TRUE;
}
heddn’s picture

Status: Needs work » Needs review

#15 - Good point about $node->nid & title. That is now fixed.

Explain more about using db_select vs db_query. I've never heard that argument for use of db_select. I almost always use db_query because it is more performant than db_select. Can you point me to some references on when to use one vs the other?

heddn’s picture

Status: Needs review » Needs work
tsphethean’s picture

Here's a good answer on when to use db_query vs db_select: http://drupal.stackexchange.com/questions/1200/given-that-db-select-is-m...

Having said that, it does look like running through that list this use case is pretty ok for using db_query so its up to you. If you stick with db_query though, make sure to enclose the table name in {} i.e.

SELECT COUNT(*) FROM {node} WHERE nid = :nid LIMIT 1

I'd also add a limit, it won't make a massive difference as you're querying an indexed column with unique key on nid, but every little helps IMO.

heddn’s picture

Status: Needs work » Needs review

#18 - nid is a primary key so it is certain to only return a single result or FALSE. I found the article that explains why you should use {} around table names. It's for multi-site installs. See prefixing: http://drupal.org/node/310072. This issue is also now fixed & committed.

tsphethean’s picture

Changes look good to me. Happy to mark this as RTBC and see if anyone disagrees.

Good work, and a good module.

tsphethean’s picture

Status: Needs review » Reviewed & tested by the community
klausi’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -PAreview: review bonus

manual review:

  • views_plugin_argument_validate_url_path: shouldn't the node queries respect node access grants? See http://drupal.org/node/93737 . Not sure that is relevant for your case?

Could be a potential security issue, so I set this back to needs work to hear your opinion. Removing review bonus tag, you can add it again if you have done another 3 reviews of other projects.

heddn’s picture

#22 - Good point, these things should honor node_access. I'm not certain if its a security issue but at the very least, changing to db_select will give a more consistent user experience. At the most, a node title could have been exposed. I couldn't prove that unless I did more testing. Regardless, I've switched to using db_select() for UX reasons.

heddn’s picture

Status: Needs work » Needs review
heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue summary: View changes

Updated issue summary.

heddn’s picture

Issue tags: +PAreview: review bonus

re-added bonus

klausi’s picture

Status: Needs review » Reviewed & tested by the community

Very good, looks RTBC to me now.

patrickd’s picture

Status: Reviewed & tested by the community » Fixed

heyho

I personally don't think that a hook_help() that provides just the same information as the module description ... is really helpful. Maybe you could provide more information? eg. A more detailed example how-to? it took a while until I really understood how to use it.

But there's nothing critical and also your reviews show that you are experienced enough to get approved!

Many thanks for your contribution and reviews!

I updated your account to let you promote this to a full project and also create new projects as either a sandbox or a "full" project.

Here are some recommended readings to help with excellent maintainership:

You can find lots more contributors chatting on IRC in #drupal-contribute. So, come hang out and get involved!

Thanks, also, for your patience with the review process. Anyone is welcome to participate in the review process. Please consider reviewing other projects that are pending review. I encourage you to learn more about that process and join the group of reviewers.

Thanks to the dedicated reviewer(s) as well.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.