I've beening reading support issues on how to impliment the modal windows and still haven't been able to do so for the comment form. I should note by comment form I mean the reply comment form.
So here's what I have...
First I altered the reply link
function fp_comment_link_alter(&$links, $node, $comment = NULL) {
// Load the Ctools modal library and add the modal javascript.
ctools_include('modal');
ctools_modal_add_js();
$links['comment_reply']['title'] = t('Reply');
$links['comment_reply']['weight'] = 10;
// Alter the reply link so we can use the Ctools modal dialog window.
$links['comment_reply']['href'] = "comment/reply/$comment->nid/$comment->cid/nojs/go";
$links['comment_reply']['attributes'] = array('class' => 'ctools-use-modal');
}
Followed by creating a menu
function fp_comment_menu() {
$items['comment/reply/%node/%ctools_js'] = array(
'page callback' => 'fp_comment_comment_reply',
'page arguments' => array(2, 3),
'access arguments' => array('post comments'),
);
return $items;
}
Then I made the page callback
function fp_comment_comment_reply($js = FALSE) {
if ($js) {
ctools_include('modal');
ctools_include('ajax');
$form_state = array(
'ajax' => TRUE,
'title' => t('Reply to Comment'),
);
$output = ctools_modal_form_wrapper('comment_form', $form_state);
if (empty($output)) {
$output[] = ctools_modal_command_display('Thank you for you comment.');
}
ctools_ajax_render($output);
} else {
return drupal_get_form('comment_form');
}
}
When the form loads I get an error...
Unable to complete operation. Fatal error in C:\websites\freedomspress\modules\comment\comment.module on line 1254: Unsupported operand types
So I tried to fix that... Although I had no clue what I was doing. I think I took out the + sign on line 1245. Which made that error go away but another error about missing arguements appeared. So I quickly stopped and came here for help.
Thanks.
Comments
Comment #1
walker2238 commentedDialog seems to do the same thing from what I have read. I'll try that out.
Comment #2
minff commentedI'm trying to do the exact same thing as the previous poster half a year ago. Only on D7, not D6.
Here's the function I have:
This code outputs a comment form, but not the one of the particular node type. That is, it outputs 2 fields (name and topic), while it should output name and comment textarea (and no topic field). When posted, a 404 error is shown (because form action=comment/reply/ for some reason).
Any help or suggestions are welcome!
Comment #3
minff commentedOk, I got that one myself: I hadn't given the page info the right way. Thus, instead of
'#edit' => $edit, one needs'comment' => (object)$edit.Comment #4
minff commentedYet, problems persist. Although the form is ok now, when submitting, HTML of the success page is shown in JS alert. No Ajax is performed from that point to change the output of the modal window. The comment, however, is added.
Also, what is interesting, in dblog the following error of comment_form() is shown:
Warning: Missing argument 3 for comment_form() in function comment_form() (row 1835 in /modules/comment/comment.module).That is, the comment form is not called properly by ctools_modal_form_wrapper(). Am I still missing some arguments that need to go into $form_state array when dealing with comments?
Comment #5
merlinofchaos commentedIt should no longer check for empty $output. Instead, check for $form_state['submitted'] to see if the form was successfully submitted and processed.
Comment #6
merlinofchaos commentedAlso, hm. It's possible you may have to adjust the $form['#action'] if the original form wants to submit somewhere else. That may be one of the difficulties of trying to override the comment form here.
Comment #7
minff commentedAs the $form['#action'] was indeed directing elsewhere (i.e. comment/reply/NID), then I swapped it for a custom page/function using hook_form_alter(). But the problem is in getting the form info in order to process the saving part. ajax_form_callback() gives me "Invalid form POST data" error, meaning that the form isn't cached when generated, or the build_id is wrong for some reason. Or does Ctools have an equivalent for ajax_form_callback()? (as it doesn't use the '#ajax' parameter in that form)
Or would there be a way to still use the same form action but snatch the info for Ajax before the page reloads?
PS. The warning given in #4 still persists: comment_form() is not getting its third argument ($comment) for some reason.
Comment #8
minff commentedOk, here's a working code with some "buts":
It's obviously not the best piece of code ever written, but I'm still working on it. (And at least it works.) Suggestions on how to do it more gracefully are much appreciated.
Todo: handling messages and errors, closing the modal and scrolling to the newly added comment in the main window.
What is weird, though, the warning referred to in #4 persists.
Comment #9
lord_of_freaks commentedsuscribing
Comment #10
merlinofchaos commentedAhh, to get rid of that warning you might need to set...I think it's $form_state['build_args'] in D7. Check the documentation for drupal_get_form().
Comment #12
ranvir.prasad commentedWell, I also struggled a lot and finally managed to accomplish by doing the following:
Comment #13
Ironflood commentedi'm trying to create the same functionality, under D7.
Minff and merlin's comments helped a lot, but i still can't figure out the issue with the AJAX warning that i believe has been described at #4.
I followed Minff code suggestion.
The comment is successfully posted, however I get a:
"Warning: Parameter 1 to dialog_comment_comment_view() expected to be a reference, value given in module_invoke_all() (line 819 of ...\includes\module.inc)."
Did anyone solved the issues mentionned in minff's posts? Does anyone has a solution or walkthrough on this?
Thx!
Comment #14
Ironflood commentedEdit: it's on the latest version of ctools
Comment #15
merlinofchaos commentedPlease reserve 'needs review' for patches to the module.
Comment #16
minff commentedWhat does the function that gives the error (i.e. dialog_comment_comment_view()) do? (It's not the same error described in #4.) Have you done the hook_menu() implementation correctly? It should look something like that:
Unfortunately the project I needed this for is now on hold (and down), so I don't have a working example anywhere. But as much as I remember it did not give any warnings or errors any more. The only problem I encountered was closing the modal, refreshing comments using Ajax and scrolling to the newest one. But otherwise everything worked.
Comment #17
Ironflood commentedThanks Minff for your answer, i correctly made the menu implementation yes.
The error I still cannot get rid is an AJAX error. The DB log shows nothing. The comment is perfectly added but it seems I'm still facing AJAX error on form submission. AJAX HTTP 200
I tried for weeks to make it work but without success, would it be a module conflict?
Anyone has a walkthrough on posting a comment through a Ctools modal on D7?
Comment #18
couturier commentedI'm working on this myself currently with the help of another programmer. If we find any solutions, we'll let you know. In the meantime, has anyone had further success with this?
Comment #19
Ironflood commentedI couldn't get rid of the AJAX errors, I must have missed something.
The easy solution was finally to create a form manually displayed in modal, and then to create a new comment programmatically by passing the data gathered by the form.
Comment #20
couturier commentedI and a programmer from my local DUG have been working on this, and like you, Ironflood, we are getting AJAX errors with the codes suggested above. Would you be willing to share the code you used to get the modal to work, including the modal link itself? I've scoured CTools documentation and cannot find anything to help other than this thread.
One idea, since our node content is just a small paragraph and photo displayed with many other nodes in a Views table, is to target the modal link to the whole node, then any existing comments plus the comment reply form would appear together. The programmer who is helping me in his spare time has come up with a way to add an option to Views to create a modal link. He will contribute this when it is ready, but there are still bugs to work out and he has to find spare time to continue work on it, so it could be a few more months.
Comment #21
barakgalili commentedthis line before ctools_modal_form_wrapper will fix the error
$form_state['build_info']['args'][] = (object)$edit;
Comment #22
waaadim commentedhello, let's do it quick :)
i found this example ---> http://zroger.com/2009/12/ajax-modal-windows/ but i can not get the form to load in the modal window.
my code looks like this:
please feel free to comment
Comment #23
couturier commentedThis issue is for bringing up the comment form (or a node with the comment form below it) in the modal window. By the way, Mimmoo, I tried the code in #21 and did not see a difference. I would still love to see Ironflood's code from #19, including the format for the link to activate the modal.
Waaadim, if you are interested in bringing up a plain form in the modal window, I think that is much easier. I recommend installing the Modal Forms module and working based on those examples or reading documentation in the CTools Ajax examples and modal.inc and other CTools includes files for help.
Comment #24
Ironflood commentedCouturier, crafting the proper link to the modal is quite straight forward from CTools AJAX example.
I'll list you what needs to be done if you were going in the same direction: create a new comment programmatically by passing the data gathered by the form.
Note: This is a part of my code, this is far from being the best piece of code ever written!
You can put the following in a block for example, this part crafts the link:
(The $nid allows us later on the know which node the comment should be attached to)
Now use drupal hook menu in MYMODULE.module to define the callback:
Finally, write the callback that exposes a custom form
Hope this draft helps you!
Comment #25
couturier commentedIronflood, I wanted to be sure to get back with you and say THANKS so much for taking time to document your work for us here. I am sure that it will be of help to programmers who are referencing this issue for help with modals. For myself--a non-programmer--I did give your code a try, but with my limited knowledge of php was not able to implement it successfully to my site. In light of my other time obligations this year, I have decided that it is best to shelve this project rather than taking the weeks that I would need to acquire adequate coding skills for this task.
Another programmer who offered to help me was not able to achieve the desired outcome quickly on a test site we set up (I wanted comment form modals to pop up from Views tables). He had to move on to other work but told me that he may re-visit the concept in the future for one of his own sites, in which case he would let me know and give me the code. My hope now is that if I wait long enough, modals may be simplified with an API into a future release of CTools. One comment from merlinofchaos on a related thread summed up what I feel is the complexity of working with coding for modals.
Comment #26
shivams commentedWell, there's a module for that.
http://drupal.org/project/modal_forms
Saved me a lot of hassle. Autmagically converted the links to load modal forms.
Comment #27
couturier commentedThe Modal Forms project is different and much more simple than putting the Comment Reply Form in a modal. One is static, the other dynamic.
Comment #28
keopxComment #29
v8powerage commentedWhere should I put code from https://www.drupal.org/node/828794#comment-4318936 #12?
Comment #30
plazik commentedWorking example for "product" node type:
mymodule.module
js/mymodule-reviews-add-review.js
Comment #31
mustanggb commented