Patch to overwrite real node author

emilyf - June 20, 2008 - 18:55
Project:Author Taxonomy
Version:5.x-1.0-beta
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I've written a patch that adds a new checkbox to the settings page. Checking this new box will, upon node submission, overwrite the original/current author of the node with the user who's name corresponds to the selected name in the author taxonomy term. If a match isn't found, it leaves the old author intact.

This is my first patch ever, so let me know if I messed it up!

#1

emilyf - June 20, 2008 - 18:56

sorry, here is the patch...

AttachmentSize
author_taxonomy_overwrite_node_author.patch 2.08 KB

#2

emilyf - June 23, 2008 - 15:49

I adjusted the patch a little. It was replacing author with anonymous if on first submission it didn't find a match. Now it doesn't do that.

AttachmentSize
author_taxonomy_overwrite_node_author_2.patch 2.09 KB

#3

David Strauss - June 23, 2008 - 15:57
Status:needs review» needs work

taxonomy_node_get_terms_by_vocabulary() will load the previously saved taxonomy terms for the node, not the ones set on the current submit.

#4

David Strauss - June 23, 2008 - 16:00

This code will also display warnings in PHP's strict mode because there might not be an item 0 in the authors array.

#5

David Strauss - June 23, 2008 - 16:06

Coding style errors:
* Form element titles should be sentence case.
* t() items should be in double quotes if they contain single quotes (and don't contain double ones).
* Indentation should follow Drupal code style standards.

This line doesn't do anything:
$node->nid = $node->nid;

#6

emilyf - June 23, 2008 - 16:24

Yes, I see that you are correct on this. Do you have a suggestion of how I can get around this issue and get the current term that is going to be submitted?

#7

emilyf - June 27, 2008 - 20:05
Status:needs work» needs review

OK, here is a new version that uses nodeapi better to get taxonomy term before submitting. It also deals with if the multiple select option is turned on, and if you have multiple entries if you're using free tags. However, at present in both those situations it will only look at the first term. My org doesn't even need it to be for multiple select but I included it so it could be more functional with this module. If people are interested and like this patch, then I will add the code to loop through each term until it finds one that matches a user. Alternatively, it could match based on term weight which may be more appropriate. For this I would take input from others. In terms of these comments:

Coding style errors:
* Form element titles should be sentence case.
* t() items should be in double quotes if they contain single quotes (and don't contain double ones).
* Indentation should follow Drupal code style standards.

All three I basically followed how it's already being done in the author taxonomy module. And I think I fixed the PHP strict warning issue.

AttachmentSize
author_taxonomy_overwrite_node_author_3.patch 3.01 KB

#8

David Strauss - June 27, 2008 - 21:02
Status:needs review» needs work

You probably shouldn't re-implement the term parsing from taxonomy.module. I can come up with about three cases off the top of my head where your parser would work differently than the built-in one.

It's much easier and safer to do the following:

      $copy = drupal_clone($node);
      taxonomy_node_save($node->nid, $node->taxonomy);
      $copy->taxonomy = _my_module_get_terms($node->nid);

Where _my_module_get_terms is implemented as:

/**
* Retrieve the terms for the given node ID.
*
* @param $nid int
*  The node ID to get terms for.
*
* @return array
*  The term objects for the given node ID.
*/
function _my_module_get_terms($nid) {
  $result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $nid);
  $tids   = array();
  $terms  = array();
 
  while ($row = db_fetch_array($result)) {
    $tids[] = $row['tid'];
  }
 
  foreach ($tids as $tid) {
    $terms[$tid] = taxonomy_get_term($tid);
  }
 
  return $terms;
}

$copy->taxonomy will then be in the same format as when you node_load();

Also, the code still needs changes to comply with Drupal coding standards. Primarily, replace your tabs with two spaces each. Comments like "The following case added by sw9" are generally not part of patches.

Thanks for the work so far!

#9

emilyf - June 30, 2008 - 13:55
Status:needs work» needs review

Hi David,

Now I see why you are still telling me to get to Drupal standards. In my text editor all the tabs are really 2 spaces, so when I made the patch something got funky on my linux distro. I made this version of the patch on my mac and it's rendering them properly. I *hope* the coding styles is at least remedied.

In terms of your other comments, please forgive me for being so newbie in development. I don't think I understand what's wrong with just accessing $node->taxonomy before it's been submitted. Am I going to cause errors somewhere? If you have a moment, could you maybe further explain this and what it would do should it work differently:

I can come up with about three cases off the top of my head where your parser would work differently than the built-in one.

Thanks, I really appreciate your assistance.

AttachmentSize
author_taxonomy_overwrite_node_author_4.patch 3 KB

#10

Todd Nienkerk - December 16, 2008 - 16:51

The problem with accessing $node->taxonomy before it's submitted is (I think) because the terms have not yet been ordered in terms of weight (or, failing weight, in alphabetical order). This results in the following strange behavior when free tagging is enabled:

  1. Add author terms "Jane, Bill" in that order
  2. Save the node. The overwritten author is "Jane."
  3. Edit the node. Note that the terms are now in alpha order, which is what Drupal defaults to when weights are equivalent. The author terms now read "Bill, Jane" in that order.
  4. Save the node. The overwritten author is now "Bill."

While you haven't actually changed any information, the overwritten author has changed because Drupal reorders the terms when the node is saved.

I'm working on implementing David's solution.

#11

emilyf - December 16, 2008 - 17:37

@Todd, yes David's solution is the better way to go here. I ended up not liking this implementation and wrote my own custom module that assigns a node author based on a cck user ref field. This was cleaner and a better solution based on the assistance I got from David.

#12

Todd Nienkerk - December 16, 2008 - 18:57

Regarding my earlier statement:

While you haven't actually changed any information, the overwritten author has changed because Drupal reorders the terms when the node is saved.

This may not be accurate. It's possible Drupal is reordering the terms when the node is loaded into the edit form, thus reordering the terms on the second save.

#13

Todd Nienkerk - December 16, 2008 - 20:38
Status:needs review» fixed

I've added this feature to the 5.x and 6.x versions. (It's very different than the patch originally submitted, but the result is the same.) Thanks for your input!

#14

System Message - December 30, 2008 - 20:40
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.