Environment Details
PHP - 5.2.3
Apache - 2.2.3
Drupal - drupal-6.x-dev
OS - Linux (Fedora Core 6)
Steps to reproduce
1 - click to create a new content type http://www.testsite8.com/node/add/story
2 - Title: Story 2
3 - Select a term of a vocabulary that can be applied to this content type
4 - Body: Body of Story 2
5 - Click on the preview button
Notice - the preview box shows the term selected (in my case "Apache" - bottom right corner of the preview box)
but the create story form does not show the term "Apache" as the default selected one.
i dug around the code a bit and the problem lies in the following code in taxonomy.module
function taxonomy_form_alter(&$form, $form_state, $form_id) {
line - 741
// Extract terms belonging to the vocabulary in question.
$default_terms = array();
foreach ($terms as $term) {
if ($term->vid == $vocabulary->vid) {
$default_terms[$term->tid] = $term;
}
in the above foreach loop i got the following value for $terms
Aug 01 21:39:15 [info] $terms = Array
(
[1] => 2
)
hence it breaks down completely since it's expecting an object but gets in integer.
}
Comment | File | Size | Author |
---|---|---|---|
#23 | taxo_preview_10.patch | 4.89 KB | desbeers |
#22 | taxo_preview_09.patch | 4.88 KB | joshk |
#19 | taxo_preview_08.patch | 5.05 KB | desbeers |
#15 | taxo_preview_06.patch | 5.04 KB | Crell |
#14 | taxo_preview_05.patch | 5.03 KB | desbeers |
Comments
Comment #1
yasheshb CreditAttribution: yasheshb commentedthe patch below fixes the problem and it also fixes the problem of seeing php notices as mentioned in the issue
http://drupal.org/node/160039#comment-281374
thx.
yashesh bhatia
Comment #2
mooffie CreditAttribution: mooffie commentedI didn't know somebody was writing a patch. I wrote my own. I also wrote a lenthy explanation, but I guess that it's better to wait for the FAPI guys to first clear all the obvious bugs...
There are two kinds of $node: the pristine one, and the $_POST one (aka "fixed up"). The taxonomy module breaks because it doesn't handle the "fixed up" kind. But I suggest we wait with our patches till the FAPI guys decide on a common approach for this problem. IMHO, it's sad to have two kinds of $node.
Comment #3
desbeers CreditAttribution: desbeers commentedRelated issues:
http://drupal.org/node/167333 (Error when previewing a forum topic)
http://drupal.org/node/166882 (Preview not displaying correct date or taxonomy)
Preview is currently only working if ‘multiple select’ is disabled. The comment ‘On preview, we get tids’ in function node_link() is incorrect when dealing with multiple select of free tagging. Because then they are arrays of tids.
The main problem is that term should be objects but after preview a node they become arrays. Also, free tagging is not working now because there is no way the typed values can be filled in the form after the preview.
The patch from #1 fixes only a small part of the problem. Attached a patch that fixes hopefully all:
Preview works correctly now; except that free-tagged term will not be links. This is on purpose because there might be ‘unsaved’ terms added. I think it’s overkill to check for them as it will become more or less a copy of the taxonomy_node_safe function.
The problem also exist when editing a node; not just with creating new ones. Without ‘preview’ (directly save) everything works as expected and all the term (selected and free) are saved into the database.
Comment #4
moshe weitzman CreditAttribution: moshe weitzman commentedi tested and this fixes various problems as described. might be nice to get a fapi expert to chime in but i think this is committable as is.
Comment #5
dvessel CreditAttribution: dvessel commentedI always found it annoying that the array structure switches out completely from previews to normal views. Could this be fixed by keeping everything consistent no matter how it's being viewed instead of manipulating it back and forth.
Comment #6
desbeers CreditAttribution: desbeers commenteddvessel,
In my opinion it's worse that you are loosing content after a preview than that it's annoying that we have to go 'back and fort'. Yes it's annoying but I see no other way to fix this problem....
Review the code please, make a better proposal or else agree with it. I spend a lot, lot, lot of time to fix this issue and would be very happy if somebody sets this RTBC or guide me in a better direction.
I never submit something without preview and I told everybody who I made a site for to -always preview-, so this is critical and has to be solved. Loosing content is no-go.
Comment #7
desbeers CreditAttribution: desbeers commentedUpdated the patch as by Chx suggestions on Irc:
Comment #8
desbeers CreditAttribution: desbeers commentedUpdated the patch again:
Comment #11
Gábor HojtsyI am not sure I like the approach of this specific preview handling either. Anyway, first started to understand what happens and found some whitespace and comment issues, which made it harder to understand:
- "the other terms who are objects" should be "which are objects" I guess
- $links['taxonomy_term_'. $typed_term] line and friends are indented too much
- "Typed string can be changes by the user, so we just insert the form-field." I don't understand, maybe you "use the form input"?
- taxonomy_terms_to_objects()'s phpdoc is misplaced
- this function does not really convert to objects, but objects and strings depending on free tagging options, docs are misleading
- "but not as object but as string." sounds wrong... could be "as strings instead of objects"
Comment #12
desbeers CreditAttribution: desbeers commentedThanks for the review Gábor!
About the approach I took.... yes, not too nice but I really see no better way to solve this problem.
I updated the patch:
- Updated the incorrect English as best as I could.
- Corrected the indented lines.
- Updated the docs to be more clear.
- Renamed 'taxonomy_terms_to_objects' to 'taxonomy_preview_terms' to be less misleading.
Comment #13
Gábor HojtsyComment #14
desbeers CreditAttribution: desbeers commentedRerolled because the patch had an offset.
Comment #15
Crell CreditAttribution: Crell commentedI can confirm that #14 fixes the bug.
Stylewise, the following issues:
1) taxonomy_preview_terms(), there's an extra line between the docblock and the function that shouldn't be there.
2) Said docblock has rather clumsy language. "objects with exception for 'free tagging' because new tags" feels very clunky.
3) "A 'Single select' field returns numeric." - Also clumsy language.
4) "If previewing; the terms must be converted to objects first." - That should be a comma, not semi-colon.
The attached patch changes no functionality but tries to clean the docs up a bit.
Comment #16
chx CreditAttribution: chx commentedA fine patch with polished comments FTW
Comment #17
desbeers CreditAttribution: desbeers commentedCell,
You don't like good oll' Dutchifide English.... Don't forget we ruled the world in the 18th century!
Thanks for the corrections and review, let's forget the past, I'm just joking :-)
Comment #18
desbeers CreditAttribution: desbeers commentedCross-posted... CTBC :-)
Comment #19
desbeers CreditAttribution: desbeers commentedSmall change in function taxonomy_link() to use 'taxonomy_preview_term_' instead of 'taxonomy_term_' for previewing 'free-tagging-term'.
This will avoid an altering of the (non-functional) term during preview like forum.module is doing for example and so avoiding notices.
Comment #20
mikey_p CreditAttribution: mikey_p commentedApplies with offset, works as advertised.
I tested this with single, multiple and free tagging vocabs and all could be previewed and then saved.
Comment #21
joshk CreditAttribution: joshk commentedThis misses one case: if oyu have a multiple-select taxonomy, and you have the first element selected, this term will not be held on preview. For instance I have a multiple-select with terms foo, bar, baz and lur. "bar" is at the top of that list, and it won't "stick" on a preview.
Turns out this is a problem in how taxonomy_preview_terms() functions:
Basically if you pass through more than one vocabulary's worth of terms, and there is a key overlap between a term id and a later-passed vocabulary ID, the term will be lost because of
unset($node->taxonomy[$key]);
One solution is to actually build a separate taxonomy array and stick it onto the node at the end. Beyond that, some more fundamental shifts in data-structure seem necessary to prevent vid/tid key collisions from creating lost data here. I can also see an instance where a term that's processed first could wipe out an entire vocabulary.
Comment #22
joshk CreditAttribution: joshk commentedOk, here's a patch which tweaks the new function to build and return a new $taxonomy array specifically, rather than working on and altering a whole $node object. This prevents any data-loss.
Comment #23
desbeers CreditAttribution: desbeers commentedNice joshk; better indeed. But now taxonomy_link didn't output anything because it was still expecting a whole node from taxonomy_preview_terms. Corrected in attached patch.
Comment #24
catchTried this with fixed, freetagging and forum vocabularies. Looks great.
Comment #25
Gábor HojtsyThanks. Committed the patch with some wrapping and white space issues fixed in taxonomy_preview_terms(). Also committed a follow up patch when I noticed there were some other overly long lines in the patch and in the original code around the patch. Thanks again.
Comment #26
(not verified) CreditAttribution: commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #27
rd08 CreditAttribution: rd08 commentedI'm using Drupal 6.4; the above patch is applied to taxonomy.module, but I have the same problem. Checkboxes for a taxonomy field lose their values during preview. Any suggestions?