Flag content migration with Migrate module

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

The following example is pending the patch found at https://www.drupal.org/node/2503815


class MyCustomFlagMigration extends DrupalMigration {
  public function __construct($arguments) {
    parent::__construct($arguments);

    // requires the flag name of the source flag in query() below
    $this->sourceFlag = 'bookmarks';
    // requires the fid of the destination flag
    $this->destFlag = flag_get_flag('bookmarks');

    $this->description = t('My awesome bookmark flag migration.');

    $this->source = new MigrateSourceSQL($this->query());
    $this->destination = new MigrateDestinationFlag($this->destFlag->fid);

    // source schema from Flag-7.x-2.x
    $this->map = new MigrateSQLMap($this->machineName,
      array(
        'fcid' => array(
          'type' => 'int',
          'not null' => TRUE,
          'description' => 'Source Flag CID',
        )
      ),
      MigrateDestinationFlag::getKeySchema()
    );

    // map fields including example source migrations Article and User
    $this->addFieldMapping('fid', $this->destFlag->fid);
    $this->addFieldMapping('entity_id', 'content_id')->sourceMigration('Article');
    $this->addFieldMapping('uid', 'uid')->sourceMigration('User');
    $this->addFieldMapping('entity_type', 'content_type');
    $this->addSimpleMappings(array('timestamp'));

  }

  public function query() {
    // query 'flag_content' for a Flag-7.x-2.x table schema
    $query = Database::getConnection('default', $this->sourceConnection)
      ->select('flag_content', 'fc')
      ->fields('fc', array(
        'fcid',
        'fid',
        'content_type',
        'content_id',
        'uid',
        'timestamp'
      ));
    
    // find flags matching this flag machine name
    $query->leftJoin('flags', 'f', 'f.fid=fc.fid');
    $query->condition('f.name', $this->sourceFlag, '=');

    return $query;
  }
}

Help improve this page

Page status: Not set

You can: