I have a table with a userid and itemid field, I've migrated everything that is referenced on the itemid's, I can also migrate the users using Migrate module, but how do I use migrate extras to migrate the flags?

Comments

emjayess’s picture

+1 we're facing this as well - and searching high and low for examples using the flag support in migrate extras to migrate flag content. any example or direction would help...

thanks in advance!

hbalagtas’s picture

@emjayess I couldn't wait for help anymore so I decided to just manually migrate the flags during the user migration, it might not be the best or fastest way but it worked for me, hope this helps you out.

Basically created a user query that also fetches their favorites

        $userquery = db_select('member', 'u');
        $userquery->fields('u', array());
        $userquery->condition('BrandCode', BRAND_CODE);
        $userquery->condition('Status', 'A');
        $or = db_or()->condition('EmailStatus', 'A')->condition('EmailStatus', 'P');
        $userquery->condition($or);

        $userquery->leftJoin('member_patternlibrary', 'p', 'p.MemberID = u.MemberID');
        $userquery->addExpression("GROUP_CONCAT(p.PatternID)", "PatternList");

Then using the complete function

    public function complete($entity, stdClass $source_row)
    {
        $flag = flag_get_flag('bookmarks');

        // flag for user
        $action = 'flag';
        $flag_name = 'bookmarks';
        $account = user_load($entity->uid);

        // flag patterns
        $flaglist = explode(',',$source_row->PatternList);
        foreach($flaglist as $flagnode)
        {
            $query = new EntityFieldQuery;
            $result = $query
                ->entityCondition('entity_type', 'node')
                ->fieldCondition('field_pattern_id', 'value', $flagnode, '=')
                ->execute();

            if (!empty($result['node']))
            {
                $nodes = entity_load('node', array_keys($result['node']));
                foreach ($nodes as $node)
                {
                    flag($action, $flag_name, $node->nid, $account);
                }
            }
        }
mikeryan’s picture

Status: Active » Fixed

Sorry I didn't respond in time to help. I vaguely recall using the flag support on one project, but can't track down the code. It looks to me, though, like it should just be a matter of mapping to the flag name. If you have flags attached to users, in the Migrate UI if you click on the name of your user migration, the flags should be exposed as destination fields, and you should be able to do this in your constructor:

$this->addFieldMapping('flag_name', 'source_flag_value');

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

luukyb’s picture

Version: 7.x-2.4-rc1 » 7.x-2.5

Hi,

I was looking for some documentation too, but there isn't anywhere.
I tried to do the solution in #3 with using the function complete or post import. But if you have too much data to migrate, your system will soon run out of memory.
Then the only way is to use this module and create a separate class, like if you were migrating nodes or users.

In your class, start to declare your flag

protected  $flag;

Then the destination class you will have to use is MigrateDestinationFlagSimple. In your MigrateSQLMap:

MigrateDestinationFlagSimple::getKeySchema()

And in your destination:

// Get the flag definition.
$this->flag = flag_get_flag('myrecipes');
// Uses Flags as a destination.
$this->destination = new MigrateDestinationFlagSimple($this->flag->fid);

Destination fields are from the table flag_content in your drupal db. Like : fid, content_type, content_id, uid.
Once you have prepared the right data (from your query or PrepareRow), simply map it like:

// Content type.
$this->addFieldMapping('content_type', 'CONTENT_TYPE');

Hope that can help someone :)

Luc

mccrodp’s picture

Hi,

I tried Mike's method, but from within migrate_d2d module, but to no avail and I cannot see anything flag related to the User migration in terms of source/destination fields. I have my User migration all working with migrate_d2d module, so don't want to change to this module. However I separated out my flag content migrate module and I'm getting a 'Fatal error: Class 'MigrateDestinationFlagSimple' not found in flag.inc' error. Am I extending the wrong class or do I specifically need to include / register MigrateDestinationFlagSimple class somewhere?

 class MigrateFlagMigration extends Migration {
  protected $flag, $sourceConnection;
  
  protected $modified_arguments = array(
     'source_connection' => 'legacy',
     'source_version' => 6,
     'source_database' => 'd6_website'
  );
  
  protected $sourceFields = array('fid');
  
  public function __construct() {
     $this->sourceConnection = $this->modified_arguments['source_connection'];
     if (!empty($this->modified_arguments['source_database'])) {
       Database::addConnectionInfo($this->sourceConnection, 'default', $this->modified_arguments['source_database']);
     }
  
	   $this->source = new MigrateSourceSQL($this->query(), $this->sourceFields, NULL);
	   $this->map = new MigrateSQLMap($this->machineName,
      array(
        'fid' => array('type' => 'int',
                       'unsigned' => TRUE,
                       'not null' => TRUE,
                       'description' => 'Source Flag ID',
                      ),
      ),
      MigrateDestinationFlagSimple::getKeySchema()
    );
    
    // Get the flag definition.
		$this->flag = flag_get_flag('register_interest');
		// Uses Flags as a destination.
		$this->destination = new MigrateDestinationFlagSimple($this->flag->fid);
  }

Many thanks.

travelertt’s picture

jcandan’s picture

Issue tags: +This is being ported to the Flag module and is currently a work in progress. See https://www.drupal.org/node/2503815
Related issues: +#2503815: Flag destination class for migrate module

This is being ported to the Flag module and is currently a work in progress. See https://www.drupal.org/node/2503815

jcandan’s picture

Issue tags: -This is being ported to the Flag module and is currently a work in progress. See https://www.drupal.org/node/2503815