3

I am copying the content from my old non-CMS (Weebly) website to my new Drupal website by manually filling in node forms and comment forms. I would like to recreate all the original authors and posting times on the new website. As a first step, I have created a user account for all the people who contributed to the old website.

Adding nodes works perfectly: as an administrator, I can post new nodes as another user and change the posting time before publishing.

Authoring information for node

Now, I would also like to add new comments, and change the author and date before posting. I know that one can administrate a comment after posting it, as shown in the screenshot below.

Administrating a comment

However, it is vital that the desired changes are made before posting comments, since publishing triggers multiple Rules, related to User Points, Flags etc. Otherwise, the wrong users are rewarded User Points, for example.

How can I do that?

  • @Pierre.Vriens: Since I am copying the comments from my old website, I already know the author and the posting time. I would like to keep this information on the new website. Of course, once the new website is published, users can post new comments themselves, so there will be no need to change the author or date anymore. – Jeroen Jun 25 '16 at 8:46
  • 1
    "I already know the author and the posting time": ok, "you" know, but what do you have in your NEW Drupal site available so that some "user-in-old-cms" corresponds to some "other-userid-in-drupal-with-uid-something"? Also, that "copy" you're doing, is that using "Migrate", "Feeds", something else? – Pierre.Vriens Jun 25 '16 at 8:55
  • You could create a custom module that uses hook_comment_presave which will allow you to change the author and time before the comment gets saved. – No Sssweat Jun 26 '16 at 7:15
1

This is a small hack and it will show you some errors, but it works. In Drupal the comment form on the node is the exact same form as you'll see on the admin page you put in a screenshot. The visibility of fields is controlled by one variable $is_admin defined on line 1883 of modules/comment/comment.module:

$is_admin = (!empty($comment->cid) && user_access('administer comments'));

Basically this checks if the comment has a comment id (cid) and if the user may administer comments.

By removing the id check (so the condition also validates for new comments which not yet have a cid), the administration information is also shown for new comments (the user still requires the 'administer comments' permissions so regular users will not see it).

Thus line 1883 becomes:

$is_admin = (user_access('administer comments'));

You will get some warnings (notices) as these fields are not built for empty data (under normal circumstances they always have a value), but the submission works flawless. Just don't forget to change this back afterwards ;)

comment form

  • Maybe this solution will not be accepted by everyone ("never hack core"), but it seems to works flawless indeed. And since it is only a temporal little trick that solves my problem very easily, I decided to check it as the solution. For future readers: please comment underneath if you are certain that this approach causes problems. – Jeroen Jun 26 '16 at 23:13
  • I would normally advice against this as well, advising you to write some database importer and use the migrate module. But this is so much easier ;) – Neograph734 Jun 27 '16 at 6:10
1

Author

For changing the author (not the date), you might be able to use the Masquerade module for this. Some details from its project page:

The Masquerade module allows site administrators (or anyone with enough permissions) to switch users and surf the site as that user (no password required). That person can switch back to their own user account at any time.

This is helpful for site developers when trying to determine what a client, logged in as themselves, might see when logged into the site.

The masquerade module is designed as a tool for site designers and site administrators. While masquerading, a field is set on the $user object, and a menu item appears allowing the user to switch back. Database log (watchdog) entries are made any time a user masquerades or stops masquerading as another user.

So just switch to the appropriate user before filling in your forms, that should do what you're trying to achieve.

PS: works for any currently supported release of Drupal.

Date

Not sure (yet) about how to do this, but I'd try to use the Rules module for this. Here is a prototype of a rule that you may want to look at and experiment with:

{ "rules_update_comment_post_date" : {
    "LABEL" : "Update comment post date",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : { "comment_presave" : [] },
    "DO" : [
      { "data_set" : { "data" : [ "comment:created" ], "value" : "-1 day" } }
    ]
  }
}

This rule should set the actual comment date to "current date - 1 day". Just as a sample. Adapt the value to fit your needs (before posting each comment, sorry ...), or try to think of an enhanced version of it to somehow retrieve the comment date as part of this rule.

  • Your approach certainly works, so I gave you an upvote. I accepted @Neograph734's answer nevertheless for the reason I gave in a comment on his answer. – Jeroen Jun 26 '16 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.