Skip to main content
Hero Background Image

Blog Post

Flag Migrations in Drupal 8

February 2, 2017

There is no explicit documentation for the Drupal 8 version of the flag module, and now that the Drupal 8 version has hit alpha, it's a good time to share some information on it.

In Drupal 8, migration destinations are easy to set up for entities. Since flags are entities in Drupal 8, all you need to do is set up a standard entity migration configuration with the fields for creating a flag.

The fields are:

flag_id - The flag's bundle machine name in Drupal

entity_type - The type of entity the flag is referencing

entity_id - The entity ID of the entity the flag is referencing

global - Globalization

uid - The user who made the flag

In an ideal situation, you have the data needed for these, with just a normal field mapping you can set up the configuration like this:

id: my_flag_migration
label: My Flag Migration
migration_group: content
  plugin: my_flag_migration
  plugin: entity:flagging
  flag_id: Id
	plugin: default_value
	default_value: node
	plugin: migration
	migration: my_node_migration
	source: ReferenceId
	plugin: default_value
	default_value: 0
	plugin: migration
	migration: my_user_creation
	source: UserId
   - my_user_migration
   - my_node_migration


In most cases when coming from a custom CMS, the data will have to be manipulated in some way.  In that case you can use your migrate source class in the same way done for any other entity migration.

class MyFlagMigration extends SqlBase {

   * {@inheritdoc}
  public function query() {

	// ... query definition ...

	return $query;

   * {@inheritdoc}
  public function fields() {
	return [
  	'ReferenceId' => $this->t('Reference Id'),
  	'UserId' => $this->t('User Id'),
  	'Id' => $this->('Legacy Flag Id'),

   * {@inheritdoc}
  public function getIds() {
	return [
  	'Id' => [
    	'type' => 'integer',
    	'alias' => 'td',

   * {@inheritdoc}
  public function prepareRow(Row $row) {

   // ... do something to the data here ...
   // $row->setSourceProperty('Id', $this->doSomething($this->getSourceproperty('Id')));

	return parent::prepareRow($row);

  public function doSomething($value) {
	// ... do something here ...


For more information on the Flag module or the Migrate API, check out their pages on  If you have any questions, feel free to comment below.

Flag module
Migrate API

Additional Resources:
Drupal 8: How to Reference a Views’ Block Display From a Field | Blog
Migration With Custom Values in Drupal 8 | Blog
Intranets The Drupal Way | eBook