Hello everybody.
I'm using Feeds 7.x-2.0-alpha4 and
I have a site where some users have permission to import nodes.
So if a user imports some nodes, I need his/her username to appear automatically as the node author.
The only option now is the text box "Select the author of the nodes to be created - leave empty to assign "anonymous"."
Can anyone help?

Thanks a lot.

Comments

paispita’s picture

Assigned: paispita » Unassigned
paispita’s picture

Is that a difficult thing to do?

In my humble opinion this should actually be the default behavior, if not for feeds, at least for node import..

Richard_1618’s picture

I agree, but here is a link you may find interesting http://drupal.org/node/652180 I'm still having a little difficulty in applying these patches but it looks like a more hopeful direction that anything else I've seen so far.

Richard_1618’s picture

Title: Node author - username » Patches don't seem to work for D7

Any help would be seriously appreciated in resolving this issue of assigning the imported nodes to the author / user that imported them.

paispita’s picture

Title: Patches don't seem to work for D7 » Automatically assign the userneme that imported nodes to the node author
paispita’s picture

So by using
global $user;
$user->name
one can have access to the logged in user name.
Can anybody point out where I should use the above snippet?

paispita’s picture

Answer:
In sites\all\modules\feeds\plugins\feedsnodeproccesor.inc, on line 31 one must replace the whole function with this:
protected function newEntity(FeedsSource $source) {
global $user;
$node = new stdClass();
$node->type = $this->config['content_type'];
$node->changed = REQUEST_TIME;
$node->created = REQUEST_TIME;
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
// Populate properties that are set by node_object_prepare().
$node->log = 'Created by FeedsNodeProcessor';
$node->uid = $user->uid; //Changed functionality - the author of the node is the name of the user who imported it.
//$node->uid = $this->config['author']; //original
return $node;
}

paispita’s picture

Status: Active » Closed (fixed)
Adam Wood’s picture

Status: Closed (fixed) » Needs work

Apologies in advance if this patch isn't suppose to do this!

I'm trying to allow users to add their blog via RSS, and assign their username to the nodes created. This patch has worked well for the initial import, however when new items are imported periodically, via cron, then they are reassigned to 'anonymous'.

Is there a way to set this so that the user who created the import is assigned as the author of all nodes created, not just the initial import?

Also, should this be committed to the next release as a standard option? As I'd imagine that people would need to use this functionality quite often.

Thanks

Adam Wood’s picture

Status: Needs work » Closed (works as designed)

After spending days looking at this, it turns out you can do this by default using the mappings for the import. The patch above isn't necessary to achieve this.

  • Create an import or override the default import type
  • Create a content type, e.g. Node import
  • Under 'Basic Settings' attach the import to the content type that you just created
  • Under mapping, add a mapping: Source = 'Feed Node: User ID' Target= 'User ID'
  • Save and create the import from node/add/node-import (or whatever the content type name was).

That's it. The initial import and (in our case) future RSS imports will all be assigned the ID of the user who created that import node.

Hope this helps anyone else on this. This should really be in the documentation...

davemaxg’s picture

Thanks Adam!

blogook’s picture

But what if one chooses to use stand alone form?

When I use the above mentioned method, 2 nodes are created .. one that is fetched by the feed and one that is created ..

blogook’s picture

my problem is solved

https://drupal.org/node/1796008

Magicthought’s picture

Well said Adam Wood, it made my day!

WorldFallz’s picture

Component: Feeds Import » Code
Issue summary: View changes
Status: Closed (works as designed) » Active

Ok, i give up. I've been searching for the 'correct' way to do this for several hours across a couple of days now. And every single thread I've found assumes the use case of using a feed node. I have about a dozen of these and I don't want to create 12 content types and all those nodes just to be able to set the author of an import. that's just crazy, lol.

Using the STANDALONE form there is no "Feed Node: User ID" to use. And the author field allows selecting any user, but not the user doing the import ($global $user;).

Has anyone figured out how to set the user nodes imported using the standalone form to the user doing the import? I don't care if it's code. This comes close, but I can't believe I have to do an additional node_load on all imported nodes.

I'm thinking one of the feeds hooks should do it, but I've been unsuccessful thus far.

I can't believe this is that unique a use case. Either I'm missing something blatantly obvious, or I'm just doing something weird, lol.

Any info/insight would be greatly appreciated.

WorldFallz’s picture

Status: Active » Closed (works as designed)

I'm an idiot, lol. Didn't read the whole post.

Here's how to do it without hacks and without additional node_loads:

/**
 * Implements hook_feeds_preserve().
 */
function MYMODULE_feeds_presave(FeedsSource $source, $entity, $item) {
  if ($entity->type == 'machine_name') {
    global $user;
    $entity->uid = $user->uid;
  }
}
nithinkolekar’s picture

@blogook how #1796008: two nodes are created worked for this issue? could you please share your configuration/setup?

gregceni’s picture

Thanks Adam & WorldFallz, I'm close but they're still going in as "Anonymous (not verified)". I have a .csv of 4 new content items of a content type called 'course_listings'. I'm *not* attaching the Feed to this content type because I want it standalone.

Does one add that code snippet to the feeds.module or the feeds.api.php? And do I still need to add the Source=Feed Node: User ID and Target=User ID? I've tried all those combinations but my nodes are still going in as Anonymous.

And the "MYMODULE" part doesn't apply (or does it?) because this is not a custom module. I realise this can get clobbered.

// feeds.api.php

function hook_feeds_presave(FeedsSource $source, $entity, $item) {
  if ($entity->feeds_item->entity_type == 'node') {
    // Skip saving this entity.
    $entity->feeds_item->skip = TRUE;
  }
  if ($entity->type == 'course_listings') {
    global $user;
    $entity->uid = $user->uid;
  }
}
WorldFallz’s picture

You need to create a custom module for this and replace the "MYMODULE" portion of the function call with the name of your module.

gregceni’s picture

WorldFallz, awesome. Thank you. I made a new module (new course.info and course.module files in /sites/all/modules/custom/) with only that "*_feeds_presave" function in it, changed the names of the 'hook' and 'entity_type', tested it in masquerade mode and voila! Thank you!

Qadees’s picture

@gregceni, Please what file extension did you give to the module you created in custom directory, and what is suppose to be the 'hook' name.
I know this may awkward after long time asking for it again. I created the file and dump in custom module directory but it didn't work.
Any help please.