0

I am able to migrate organic groups easily enough, but when I attempt to migrate the group content, I am not able to successfully migrate the "parent" group ID(s) (or group audience if you prefer). I could only find snippets of code, and never a complete example, so hopefully I'm missing something simple.

Here is my complete migration code for the group content (as per multiple sources, but for example and per the OG issue page:

  $arguments = $common_arguments + array(
    'description' => t('Migration of group nodes from Drupal 6'),
    'machine_name' => 'RanchoNodeGroupNews',
    'source_type' => 'groupnews',
    'destination_type' => 'groupnews',
  );
  Migration::registerMigration('RanchoGroupNewsMigration', $arguments['machine_name'], $arguments);

class RanchoGroupNewsMigration extends RanchoNodeMigration {
  public function __construct(array $arguments) {
    parent::__construct($arguments);

    // OG Audience field
    $this->addFieldMapping('og_group_ref', 'og_group_ref')
         ->separator(',')
         ->sourceMigration('RanchoNodeGroup');
    $this->addFieldMapping('is_public', 'is_public');        
    // Set the default owner to rm.com
    $this->addFieldMapping('uid', 'uid')
         ->defaultValue(4);

    $this->addUnmigratedSources(array(
    ),'DNM', FALSE);

    $this->addUnmigratedDestinations(array(
    ),'DNM', FALSE);

  }

  protected function query() {
    // Get the default query 
    $query = parent::query();
    $query->leftJoin('og_ancestry','oga','n.nid = oga.nid');
    $query->addExpression('GROUP_CONCAT(DISTINCT oga.group_nid)', 'og_group_ref');       
    $query->addField('oga', 'group_nid', 'group_nid');
    $query->addField('oga', 'is_public', 'is_public');

    return $query;
  }  

  public function prepareRow($row) {
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    } 
    drush_print_r($row->og_group_ref);    
    return TRUE;
  }

  public function prepare($node, stdClass $row) {
    // Set og field_mode to 'admin' so we can save this group content despite not being a member of the group.
    // See https://www.drupal.org/node/2399997
    if (!empty($node->og_group_ref[LANGUAGE_NONE])) {
      foreach($node->og_group_ref[LANGUAGE_NONE] as $delta => $value) {
        $node->og_group_ref[LANGUAGE_NONE][$delta]['field_mode'] = 'admin';
      }
    }
    drush_print_r($node->og_group_ref);
  }
}

The output I receive from drush_print_r is as follows:

drush mi RanchoNodeGroupNews --idlist=60274 --user=1
56288
Array
(
    [und] => Array
        (
            [0] => Array
                (
                    [target_id] => 56288
                    [field_mode] => admin
                )

        )

)

Processed 1 (1 created, 0 updated, 0 failed, 0 ignored) in 0.2 sec (267/min) - done with 'RanchoNodeGroupNews'         

                          [completed]

Note that nid 56288 is indeed the parent group node, and is the only parent that has been migrated. I am able to edit the group content and set the parent manually. Any idea what's wrong with my code?

  • What does hook_migrate_api() look like for that content type? – Jaypan Sep 25 '16 at 2:09
  • @Jaypan, I updated my question to add the hook_migrate_register_migrations() portion. There's nothing of interest in hook_migrate_api() (using d2d). – Webdrips Sep 25 '16 at 2:43
  • I'll be honest, I don't really see why it wouldn't find all your nodes. How many does it list on the migrate listing page? Do you have any messages after the migration with errors or anything in them? – Jaypan Sep 25 '16 at 2:48
  • No messages so long as I import with the group owner ID or user 1 with the above code that sets the field_mode. I believe I found a solution using complete(), so I'll post that a bit later when I have everything I need. Note: it's finding the correct parent, just not setting the value. – Webdrips Sep 25 '16 at 2:51
0

Seems like a bug, but needed to add a complete() function as follows:

class RanchoGroupNewsMigration extends RanchoNodeMigration {
  public function __construct(array $arguments) {
    parent::__construct($arguments);

    // OG Audience field
    $this->addFieldMapping('og_group_ref', 'og_group_ref')
         ->separator(',')
         ->sourceMigration('RanchoNodeGroup');
    $this->addFieldMapping('group_access','group_content_access');     

    ...

  }

  protected function query() {
    // Get the default query 
    $query = parent::query();
    $query->leftJoin('og_ancestry','oga','n.nid = oga.nid');
    $query->addExpression('GROUP_CONCAT(DISTINCT oga.group_nid)', 'og_group_ref');       
    $query->addField('oga', 'group_nid', 'group_nid');
    $query->leftJoin('og_access_post','oa','oa.nid = n.nid');
    $query->addField('oa','og_public','group_content_access');

    return $query;
  }  

  public function prepareRow($row) {
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    } 
    // Seems the access setting is actually the opposite in D7, so we need to
    // toggle the boolean here.
    $row->group_content_access = !$row->group_content_access;
    return TRUE;
  }

  public function prepare($node, stdClass $row) {
    // Set og field_mode to 'admin' so we can save this group content despite not being a member of the group.
    // See https://www.drupal.org/node/2399997
    if (!empty($node->og_group_ref[LANGUAGE_NONE])) {
      foreach($node->og_group_ref[LANGUAGE_NONE] as $delta => $value) {
        $node->og_group_ref[LANGUAGE_NONE][$delta]['field_mode'] = 'admin';
      }
    }
  }

  public function complete($node, stdClass $row) {    
    $values = array(
      'entity_type' => 'node',
      'entity' => $node->nid,
      'state' => OG_STATE_ACTIVE,
      'membership_type' => OG_MEMBERSHIP_TYPE_DEFAULT,
      'field_name' => 'og_group_ref',
    );
    // Update the node
    $group_id = $row->og_group_ref;
    og_group('node', $group_id, $values);
  }
}

The above works regardless the number of groups the node is assigned to.

Note because this took me quite some time to figure out: the og_access and og_public have the opposite meaning from D6 to D7.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.