applab Posted July 29, 2014 Posted July 29, 2014 I have a template which includes a "comments" field using the core Comments field type (FieldtypeComments) Is there a way to post a comment to a page which uses this field, programmatically, from a different page?, ie. not the current $page. something along the lines of: $my_comment = array( 'cite' => $comment_cite, 'text' => $comment_text, 'created' => $comment_date ); $p = $pages->get('my-selector'); $p->comments->add($my_comment); // no such method! tia.
teppo Posted July 30, 2014 Posted July 30, 2014 Something like this should work (assuming that the name of your comments field is 'comments'): $comment = new Comment; $comment->text = "my text"; $comment->cite = "my name"; // etc. $p = $pages->get('/about/'); $p->comments->add($comment); $p->save('comments'); 5
applab Posted July 30, 2014 Author Posted July 30, 2014 thanks Teppo, worked great after 1 amendment: on first attempt I got an error: "Error: Exception: Can't save field from page 1028: /blog/posts/test-post/: Call $page->setOutputFormatting(false) before getting/setting values that will be modified and saved." so, I did as the error suggested end ended up with: $comment = new Comment; $comment->text = "my text"; $comment->cite = "my name"; // etc. $p = $pages->get('/about/'); $p->setOutputFormatting(false); // added $p->comments->add($comment); $p->save('comments'); checking the PW cheatsheet for $page->setOutputFormatting() makes it clear why that's needed. 6
ttbb Posted December 5 Posted December 5 Maybe my question fits in here ... I'm working on a larger migration procedure from Drupal 7 to PW, and my progress makes me optimistic about achieving my goal 🙂 I have some Drupal content types with comments attached, and want to add them programmatically while creating PW pages from the imported content. The comments from Drupal are incoming as a flat array with an item "comment_parent_id". If it's not zero, it's a reply to another comment and shows the ID of the respective comment. I didn't find anything about how such a nested comment is added to a page. Please, can anyone shed some light on how nested comments are working API-wise? Thanks a lot! Thomas
applab Posted December 5 Author Posted December 5 11 years later, where does the time go!! disclaimer: I haven't tried this but glancing at the API docs it looks possible... from this page https://processwire.com/api/ref/fieldtype-comments/add-comment/ we see the addComment() method $bool = $fieldtypeComments->addComment(Page $page, Field $field, Comment $comment, bool $send); this accepts a (fully populated) Comment object as one of it's parameters. If we look at the API docs for the comment class https://processwire.com/api/ref/comment/ it has properties `id` & `parent_id` So (I'm guessing) if you add a comment you can retrieve it's `id` property to use as a `parent_id` on a subsequent comment. HTH, Martin. 1
ttbb Posted December 5 Posted December 5 thanks a lot, @applab 🙂👍 your hints helped a lot. while proceeding and making progress the next questions pop up: do I need an existing (saved) page object to attach my comments to or can I add the comments to the new (unsaved) page object? this would allow me to do the page creation in one run and to avoid a later loop for attaching the comment. and: do I need to use a new comment array before attaching this to the page?
applab Posted December 6 Author Posted December 6 16 hours ago, ttbb said: this would allow me to do the page creation in one run and to avoid a later loop for attaching the comment. As I understand it, in order to save a comment you need to save the page the comments field is attached to, and given that you will need to save a comment in order to retrieve it's id for use as a parent_id in later calls you will be calling $page->save() multiple times for each post, but this won't require a "later loop for attaching the comment", you only need to iterate your posts once. 16 hours ago, ttbb said: and: do I need to use a new comment array before attaching this to the page? you don't need an array to hold the comments , if that's what you mean, you use a reference to the comments field attached to the page $myPage->comments->addComment(Page $page, Field $field, Comment $comment, bool $send); you will need to create a new comment object to be passed in to the addComment() call.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now