Relation https://janezurevc.name/ en Relation Migrate integration and *reference upgrade path https://janezurevc.name/relation-migrate-integration-and-reference-upgrade-path <span>Relation Migrate integration and *reference upgrade path</span> <span><span lang="" about="https://janezurevc.name/users/slashrsm" typeof="schema:Person" property="schema:name" datatype="" xml:lang="">slashrsm</span></span> <span>Mon, 06.08.2012 - 23:35</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>There was<a href="https://drupal.org/node/1606404#comment-6305944"> my patch</a> <a href="https://drupalcode.org/project/relation.git/commit/79571cc">committed</a> to <a href="https://drupal.org/project/relation">Relation module</a> (which is great - if you have not tried it yet definitely do) today, that adds <a href="https://drupal.org/project/migrate">Migrate</a> support to Relation and uses it to provide upgrade path from Term/<a href="https://drupal.org/project/references">User/Node</a>/<a href="https://drupal.org/project/entityreference">Entity</a> reference fields to Relation. I hope that this feature will be useful to a lot of people. In order to use new functionality you'd need to enable <em>Relation migrate</em>, which comes together with Relation.</p> <p><a href="https://janezurevc.name/sites/default/files/enable_relation_migrate.png"><br /><img src="https://janezurevc.name/sites/default/files/enable_relation_migrate.png" data-entity-type="file" data-entity-uuid="d38843c3-d3a0-4dd5-b705-5f3ef54f74c6" /></a></p> <h2>Migrate integration</h2> <p>Module implements class <a href="https://drupalcode.org/project/relation.git/blob/refs/heads/7.x-1.x:/relation_migrate/relation_migrate.migration.inc#l8"><em>MigrateDestinationRelation</em></a>, which is a destination plugin for Migrate. You can use this class in your custom migrate scripts in a very similar way as you'd use <a href="https://drupalcode.org/project/migrate.git/blob/refs/heads/7.x-2.x:/plugins/destinations/node.inc#l14"><em>MigrateDestinationNode</em></a>, which is most likely used when migrating nodes. The only difference will be in mapping, which should be done in<em> </em><em>prepare()</em> or <em>prepareRow().</em> This is required because of the complexity of <em>relation_endpoint</em> field, which requires a lot of information in order to properly build a relation. Here is an example of typical mapping definition:</p> <pre> public function prepare(stdClass $relation, stdClass $source_row) { $relation->endpoints[LANGUAGE_NONE] = array( array('entity_type' => $source_row->source_type, 'entity_id' => $source_row->source_id), array('entity_type' => $source_row->destination_type, 'entity_id' => $source_row->destination_id), ); } </pre><p>This example is taken from <a href="https://drupalcode.org/project/relation.git/blob/refs/heads/7.x-1.x:/relation_migrate/relation_migrate.migration.inc#l49">actual implementation</a> of *reference upgrade path in <em>Relation migrate</em>.</p> <h2>Term/User/Node/Entity reference upgrade path</h2> <p><a href="https://janezurevc.name/sites/default/files/relation_type.png"><img alt="" data-entity-type="file" data-entity-uuid="bbdfafac-fd54-43a2-8905-5e26f77d9c04" src="https://janezurevc.name/sites/default/files/styles/large/public/relation_type.png" /></a></p> <p>A typical request by people thinking about using Relation on their existing sites is upgrade path from *reference modules. Migrate integration now allows us to do this. You shouldn't spend more than a few clicks to have this done. I will demonstrate this feature on a standard Drupal 7 installation. I have generated 50 nodes that are tagged with taxonomy terms. This is done via <em>field_tags</em> field, which is obviously a <em>Taxonomy reference.</em> I will first create new Relation type and configure it to allow relations from nodes to terms.</p> <p><a href="https://janezurevc.name/sites/default/files/relation_configuration.png"><img alt="" data-entity-type="file" data-entity-uuid="32ab0464-4653-48ee-9b00-fa54f608ecbd" src="https://janezurevc.name/sites/default/files/relation_configuration.png" /></a></p> <p>Now I have to configure Relation migrate. In order to do that I have to navigate to configuration page (found under <em>Structure -> Relation types -> Migration</em>). You'll find configuration for every type of reference field here. Currently we support Term (core), Node (References), User (References) and Entity reference. As I already mentioned I use standard installation for this demo, but other supported fields should appear here if there are any. Now I just have to enable upgrade path for <em>field_tags</em> and select relation type that should be used for this upgrade.</p> <p>If I navigate to <em>Content -> Migrate</em> (Migrate UI must be enabled) i find four migration classes, each for every supported reference field type. I can see that I have some unmigrated term references, which are obviously tags on my nodes. I select this class and start the migiration process.</p> <p><a href="https://janezurevc.name/sites/default/files/migration_classes.png"><img alt="" data-entity-type="file" data-entity-uuid="47896efa-e2aa-4714-a209-9df818ac998d" src="https://janezurevc.name/sites/default/files/styles/migration_classes.png" /></a></p> <p>When migrate script completes it's job I end up with a lot of new relation, which are tags on my nodes.</p> <p><a href="https://janezurevc.name/sites/default/files/relations.png"><img alt="" data-entity-type="file" data-entity-uuid="d764e31e-edcf-4a30-9803-3ddf42e1a4ea" src="https://janezurevc.name/sites/default/files/relations.png" /></a></p> <p>Described functionality is currently available in 7.x-1.x-dev release of Relation module. It is relatively new and as such not very well tested. Please report back in the issue queue if you find any bugs or encounter any problems. I'd be glad to help. Hope you will enjoy Relation module and it's Migrate integration.</p> </div> Mon, 06 Aug 2012 21:35:19 +0000 slashrsm 43 at https://janezurevc.name