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 Architect at Mediacurrent. Throughout his web development career, Chris has gained a unique perspective on what it takes to deliver a successful project through his experiences in freelancing, agency, and also strictly product development/support work.

During his college years at the New Jersey Institute of Technology, Chris created his first production Drupal website for the school’s mechanical engineering department and worked on a Drupal site for a major nurses' association in Pennsylvania. He then began his career at Northpoint Digital and Ernst & Young where he gained a wide range of experience with Drupal including e-commerce, migrations, front-end, back-end, headless Drupal, templating, site architecture, mentoring, support, client interaction, devops, and repository and deployment management. Prior to Mediacurrent, Chris worked as a software developer on feature request and support for a custom PHP-based site, Vultr (VPS hosting company e.g. Digital Ocean, Amazon AWS, etc.), devops, and system administration.

Outside of work, Chris can be found obsessing over cars and keyboards in New Jersey, the state he calls home. He is an audiophile who keeps a server rack running in his living room and still mines Bitcoins 24/7.

Learn more about Chris >

Related Insights