Skip to main content

Blog Post

Flag Migrations in Drupal 8

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
source:
  plugin: my_flag_migration
destination:
  plugin: entity:flagging
process:
  flag_id: Id
  entity_type:
	plugin: default_value
	default_value: node
  entity_id:
	plugin: migration
	migration: my_node_migration
	source: ReferenceId
  global:
	plugin: default_value
	default_value: 0
  uid:
	plugin: migration
	migration: my_user_creation
	source: UserId
migration_dependencies:
   - 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 drupal.org.  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

 

Chris Runo

Meet team member, Chris Runo

Chris brings four years of Drupal experience to his role as a Senior Drupal Developer at Mediacurrent. Throughout his web development career, Chris has gained a unique perspective on what...

Learn more about Chris >
Access icon Up arrow icon Drupal 8 icon Facebook icon - white Facebook icon - blue outline Facebook icon - yellow Hollow right arrow icon Hollow right arrow icon - white LinkedIn icon - white LinkedIn icon - hollow LinkedIn icon - blue outline LinkedIn icon - yellow Mediacurrent wordmark Quote icon Twitter icon - white Twitter icon - hollow Twitter icon - blue outline Twitter icon - yellow Youtube icon - white Youtube icon - yellow