The idea is to add a CCK Field Formatter for issue IDs. It should behave exactly like the input filter feature that project_issue already has.
Patch is attached :)
| Comment | File | Size | Author |
|---|---|---|---|
| #43 | 1175704-43-bzr-format-wiget-formatter-noderef.patch | 11.44 KB | jhodgdon |
| #41 | 1175704-41-noderef-widget-formatter.patch | 11.37 KB | jhodgdon |
| #37 | 1175704-37.issue-noderef-widget.patch | 11.53 KB | dww |
| #35 | 1175704-35-bzr-format.patch | 11.54 KB | jhodgdon |
| #33 | 1175704-33.patch | 11.54 KB | jhodgdon |
Comments
Comment #1
dwwInstead of trying to treat a simple int field as if it were a node reference to an issue, why not just do this:
#243543: nid text field widget?
Allow the issue node reference to just take an int nid as input (which is how it's stored, anyway) and then you get all the power of node references.
Comment #2
jhodgdonFormatting it like #1175704: Add CCK field widget and formatter for issue references seems better to me than formatting it like http://drupal.org/node/648218 -- right?
Comment #3
cweagansThe problem is that node references do not perform the same job as this patch. Node references allow you to reference a node, but this patch allows you to display the issue ID, the issue title, and the background color of that span is based on the status of the issue.
dww, not sure if you were online last night when this was discussed - jhodgdon is working on #648218: Make API changes in Drupal core be nodes and it would be cool to use this field formatter for displaying the issues that caused an API change.
Comment #4
dwwWell, if it was a real node reference, it'd appear like Add CCK field formatter for issue numbers, not like http://drupal.org/node/1175704. So yeah, I guess this patch could still be useful as a way to format node references that point to issue nodes to add the #xxx and the color-coding, strike-through, etc. But, I'd still strongly encourage #648218: Make API changes in Drupal core be nodes to stores these fields as real node refs, not plain ints. IMHO, the difference between this:
Add CCK field formatter for issue numbers
and:
#1175704: Add CCK field widget and formatter for issue references
is just icing on the cake, not the cake itself.
Comment #5
cweagansI wonder if there would be some way to use the Node Reference NID module from http://drupal.org/project/cck_extras to store the data as a node reference and then use this formatter for display.
I'll play with that a bit tonight.
Comment #6
jhodgdonGood points... So the features we want here are:
a) [required] Widget: enter the NID (rather than entering the node title in an auto-complete or select). So this could be a numeric field or a noderef field...
b) [highly desirable] Field: Have it be a noderef field, so you can use views relationships etc.
c) [highly desirable] Formatter: display the NID and the node title:
#648218: Make API changes in Drupal core be nodes
d) [desirable] Formatter: display the coloring/strikethrough for issue formatting that we're all used to: #648218: Make API changes in Drupal core be nodes
Comment #7
mikey_p commentedI was just going to say the same, I'd also like to see what kind of widget we could add that would limit this to issue nodes with possible autocomplete (for issues only, would still support nid).
Note that I'm doubly interested in this, since it's something that could be reused by the related issues stuff (even though that's not using CCK).
Comment #8
jhodgdonI'm not as concerned about autocomplete for the issue nodes. I think devs can copy/paste or type in the issue number (and correct it if the preview shows the wrong issue). We already do that when typing [ # 12345 ] in comments.
As far as limiting to particular content types, noderef already does this in its existing widgets, so if that can be adopted, good. (at least it would validate the nid)
Comment #9
cweagansHere's an updated patch. I don't think it's super important to use a node reference field - how often are you really going to need to do that?
This patch does exactly what #648218: Make API changes in Drupal core be nodes needs.
Comment #10
jhodgdonAgreed - I don't think we need it to be a noderef for this project. I don't see anything we can't do with an integer field. I'll give this a try and report back shortly, thanks!
Comment #11
mikey_p commentedI'd really rather see this as a node reference field, you're going to end up validating this data at some point and it sure would make alot more sense to validate it on input rather than on output (it'll confuse alot less people that way).
Comment #12
mikey_p commentedHere's a first pass at a widget and autocomplete for issues. The autocomplete is very fluid, and takes issue numbers OR titles and uses a more pleasing syntax than the default CCK. The formatter is adapted to work with nodereference instead of integer fields as well.
The autocomplete also includes a generic callback in addition to the one used by the nodereference widget, that could be used elsewhere (I intended it for use with the related issues module, and I could move that callback to that module if necessary).
I'll try to setup a demo of how this works, and I'd love a detailed code review.
Comment #13
jhodgdonThis is great!
I did find one bug. I tried to link to issue #1136130: Regression: Reinstate WATCHDOG_* constants and document why they are necessary., which, at least on my test box, has title:
Don't duplicate watchdog level constants
I typed in "Don't dupli..." and it found the issue. So far so good.
But when I went to save the API change node (which has this autocomplete noderef field on it), I got an error:
Issues: title mismatch. Please check your selection.
And in the field, the issue title was showing up as:
So it looks like there is an entity double-decoding or double-encoding problem. Other than that, it seems to be working fine...
Comment #14
mikey_p commentedAhh, I was reusing the value for the key, which was already run through check_plain. That doesn't work since the key will be used exactly as is. This ever so slightly reworks the logic in the autocomplete function to be slightly more agnostic.
Comment #15
jhodgdonThat fixed the problem I identified in #13 - seems to be working fine on my dev site!
Comment #16
jhodgdonThis is working very well for us on my demo site. Can we get it committed and deployed to d.o?
Comment #17
mikey_p commentedHere's a revised patch that moves the generic callback out (and into pi_related module) and cleans up some code comments.
I'd like to here from another project* maintainer before committing this.
Comment #18
jhodgdonDoes this patch need testing too, or is it functionally the same?
Comment #19
mikey_p commentedIt'd be good to test this patch, but it mostly just removes some functionality (that wasn't being used by this feature, only in pi_related).
Comment #20
jhodgdonI just deployed the new patch from #17 on http://jhodgdon-drupal.redesign.devdrupal.org/, and it is working fine for auto-complete widget and formatter. The bug I identified in #13 is also still fixed.
As far as I'm concerned, this patch is ready to go (but I haven't reviewed the code).
Comment #21
mikey_p commentedJust found another bug where it could return more than the designated number of items. Silly me.
Comment #22
jhodgdonIs it easy to fix? Is there something I can do to help? I'd really love to get the API changes thing deployed, but it's waiting on this issue.
Comment #23
mikey_p commentedThis should be better, and has better comments.
Comment #24
jhodgdonThe new patch is up and working fine on my test site. Still haven't reviewed the code, just verified that the widget and formatter appear to be working fine.
Comment #25
dwwx-post of some sort?
Comment #26
jhodgdonsorry, I definitely did not mean to change the status!
Comment #27
rfaySubscribe.
Comment #28
webchickSorry, I don't really know CCK well enough to give a really proper review, but here's some stuff I found.
<pedantic stupid crap>The standard is for comments to wrap at 80 chars, and if more than that's required, it's on a separate line.
</pedantic stupid crap>Is it worth it to sanitize $nid here?
Just as a minor style quibble, you might want to put db_placeholders($values) inside the query ala http://api.drupal.org/api/drupal/modules--block--block.module/function/b... so the next person to review this doesn't go OMG! SQL INJECTION?!
<pedantic nonsense>there's an extra space before =
</pedantic nonsense><boring pedantic nonsense>Comments should end in a period. :P
</boring pedantic nonsense>This function could use a few more comments to explain what's going on. A lot of that instantiation stuff, for example, is unclear, and wherever there's a regex, a comment is always a good idea. ;)
Unless you know something I don't, that looks like an improper use of t(). t() should only go against static strings, because otherwise there's no way to extract translations.
Should these be wrapped in t()?
Neither of these actually check to see if $project_issue_node was successfully returned. Might not be a bad sanity check to add.
Same PHPDoc as the function above, but this one shows assigned to information after it.
Powered by Dreditor.
Comment #29
jhodgdonHere's a redo of the above patch, to address webchick's comments in #28. I haven't tested it yet... will make time to do that tomorrow...
Changes from the previous patch:
- Fixed up the comments throughout (docblocks and in-code).
- Sanitized input and/or added comments as to why it's not needed.
- Added some comments and some additional validation in project_issue_nodereference_autocomplete_validate().
- Wrapped two literal strings in t().
- Added check to see if node was loaded in theme_project_issue_formatter_issue_id() and the following function.
One thing I didn't address was "Unless you know something I don't, that looks like an improper use of t()" regarding this string not being static:
It looks fine to me... so I didn't make a change. There are no variables there that I can see, just a % placeholder?
Comment #30
webchickSorry, that comment wasn't in relation to that part of the string, but in relation to this:
http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/t/7 recommends against t($variable) unless you know what you're doing. However, I "overheard" mikey_p saying that he basically copy/pasted this out of CCK, so it's likely this is an instance where t($variable) is appropriate.
Comment #31
sun$element is unset unless 'type' is the expected.
Use regular (int) casting here.
In one line:
Any reason for why it needs to come first? (array_unshift)
!empty() excludes "0". Should probably be $value !== ''.
Odd indentation for the sanity check, syntax error?
$n is a bit cryptic.
I don't understand why we're attempting to validate the title. The issue title - even when manipulated - should not have an effect?
Missing db_rewrite_sql().
In the negative case, we output the 'nid' value without any bells and whistles?
I'd either expect at least a check_plain(), or ignore the value altogether, t('n/a').
Comment #32
jhodgdonmikey_p: your turn this time...
Comment #33
jhodgdon#30 - agreed, even though CCK/noderef is doing it, t() should not be called there, so I took that out.
#31 - addressing these issues in a new patch...
One note: In the validation function for the widget, the user could have conceivably entered a node title, a node ID, or else the autocomplete javascript could have supplied #NID: title. We're checking for validation of any of those options. I think the code comments reflect that...
I think I've addressed everything else - thanks for the thorough review sun!
Comment #34
jhodgdonThe patch above is *not* working any more. Looking into it...
Comment #35
jhodgdonSmall problem with the order of args in array_search - it all works fine now on my test site, with this patch (sorry, this came from bzr on my test box, so it has the wrong number of path segments for a git patch, hope that is OK?).
Comment #36
sunAlright, this looks good to go now.
Comment #37
dwwThanks y'all, this is looking really close and *really* useful and cool!
There are a few trivial code style bugs in some of the PHPDoc blocks (which I've fixed in the attached patch).
However, the real reason I'm setting this to 'needs work' is that I don't understand something about project_issue_nodereference_autocomplete_validate() as it sits in this patch:
In the "nid: title" case, it tries a full node_load() (and doesn't test for published)
In the nid-only case, we do a separate query and don't test n.status at all.
In the title-only case, we do a separate query and include
n.status = 1inside the WHEREWhat's going on here? ;)
A) We should be consistent about enforcing that people only link to published issues... or should we? Should we let people that can view the unpublished nodes (e.g. those with 'administer nodes') reference them?
B) Why node_load() in one case and not the others? node_load() is expensive (unless it's already happening and we're hitting the cache). Is there some reason the node is likely to already be loaded if we're in the nid: title case for some reason? Please to explain.
In other news:
C) This could use some simpletests... As with other recent patches, I'm not going to block a commit over it, but I'd really appreciate tests if possible.
D) Just tried this out on a local site. Once you select this widget on the first page when adding a new noderef field to a content type, you land on the 2nd page to configure the field instance and you get to select which node types can be referenced. The widget's validation is only going to allow issue nodes, so it's kinda pointless to ask again and let admins shoot themselves in the foot by allowing other node types to be referenced. However, I don't know if it's possible for the widget to alter the instance settings form or not. Again, not a blocker, but if anyone can shed light, I'd be very happy.
Cheers,
-Derek
Comment #38
dwwp.s. I agree with sun in #21:
E) Can someone explain why we care in the #nid: title case? The noderef only stores the nid anyway. I can see we need to validate the title if that's all we've got, but once we've got the nid we're aiming for, why bother? I could even imagine this being a pain in the ass if someone re-titles the issue while you're filling out a form with one of these references.
Comment #39
jhodgdonRE #37/38 -
a) I believe all the queries have db_rewrite_sql() in them, which should be checking permissions at least, but probably yes we should only show published nodes in all cases.
b) I think we should probably rewrite the code so that it does a similar query in all cases, rather than node_load, because all we need to find for all cases is nid and title.
c) Yeah, tests, always good... Can we do tests with autocomplete? Probably not I'm guessing... so I guess only form submission with the acceptable formats and widget rendering would be testable. Shouldn't be too hard to write...
d) Agreed, it shouldn't allow you to select node types, and we should check into that...
e) Agreed that we probably don't need to validate that the title matches in the #nid: title case.
So we need a new patch. I'll see if I can make time today... mikey_p, let me know in IRC or here if you are doing this instead.
Comment #40
dww@jhodgdon: Thanks for working on this...
re: a) db_rewrite_sql() just allows other modules (e.g. node access ones) to alter the query and inject more JOINs on {node_access_grants} and friends. It doesn't force a check for n.status = 1, which is all I'm concerned about. My talk of permissions was to see if we wanted to conditionally force n.status = 1 depending on the current user, but I agree, that's weird. Let's just always check for n.status = 1.
re: c) I dunno about writing autocomplete tests. ;) I was more thinking of unit tests on project_issue_autocomplete_issues_search(), and perhaps functional tests on the validation itself (ignoring the autocomplete). However, the latter might be a huge hassle since you'd need to define a content type and CCK and all that crap. If it's easier to do all this within our testing framework than I fear, go for it. But, if it's a rat hole, just some unit tests on project_issue_autocomplete_issues_search() would be a good step in the right direction.
Thanks!
-Derek
Comment #41
jhodgdonRE item (d) from the last couple of comments:
This is coming from CCK's hook_field_settings() [from the nodereference field].
The way this addition to project_issue is currently set up, it's providing a special Issue-specific editing widget and output formatter for the nodereference field. So it cannot affect the *field* settings, since it's not providing a field. All it could do is have widget settings, which would be shown in a different part of the settings screen.
So I think we're unfortunately going to have to live with this as it is. Sorry.
I have the other items addressed (except I haven't written tests), and am attaching this patch here so that I can verify that it works on my test site (wget from d.o seems to be the easiest way for me to transfer a patch here) (anyway, someone else might want to look at it).
I'm not sure I have the time/energy to write tests today either, so I'm un-assigning this for now... will report back on whether this patch works (via quick human-driven UI test).
Comment #42
jhodgdonComment #43
jhodgdonThe logic in that last patch didn't quite work in the validation. This one works. Sorry, bzr format again (patch created from d.o test box).
Comment #44
dwwPerfect. I love it:
http://drupal.org/commitlog/commit/1894/a4a7e63f31f4c9211042b7ee2138225f...
Thanks everyone! This totally rocks.
Back to "needs work" on the vain hope that someone will provide a patch to remove the "needs tests" tag. ;)