Была ли у кого-нибудь удачная попытка миграции с Drupal 7 на Drupal 9 модуля Ogranic Groups?
Не можем решить такую проблему: контент из Ogranic Groups переносится, но не привязывается к группам. Просто как энтити мигрирует. + Так же не получается привязать пользователей к группам.
Комментарии
Я решил это таким образом:
<?php declare(strict_types=1);
namespace
Drupal\example_migrate\Plugin\migrate\destination;use
Drupal\migrate\Plugin\migrate\destination\EntityContentBase;use Drupal\migrate\Row;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\group\Entity\Group as GroupEntity; /**
* Group destination.
*
* @MigrateDestination(
* id = "example_migrate:group"
* )
*/
class Group extends EntityContentBase {
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = []) {
$entity_ids = parent::import($row, $old_destination_id_values);
/** @var \Drupal\group\Entity\GroupInterface $group */
$group = $this->storage->load(reset($entity_ids));
$group_members = $row->getDestinationProperty('group_members');
if ($group_members) {
$this->addGroupMembers($group, $group_members);
}
$group_content = $row->getDestinationProperty('group_content');
if ($group_content) {
$this->addGroupContent($group, $group_content);
if (
$group->bundle() === 'country') {$this->addGroupSubgroup($group, $group_content);
}
}
return
$entity_ids;}
* Adds group members to the group.
*/
protected function addGroupMembers($group, $group_members) {
foreach ($group_members as $values) {
$account = User::load($values['etid']);
if ($account) {
/** @var \Drupal\group\GroupMembership $membership */
$membership = $group->getMember($account);
if ($membership) {
// User exists in the group, it can be an author.
/** @var \Drupal\group\Entity\GroupRelationshipInterface $relationship */
$relationship = $membership->getGroupRelationship();
$relationship->set('created', $values['created']);
$relationship->setChangedTime($values['created']);
$relationship->save();
}
else {
// User doesn't exist - add to the group.
$group->addMember($account, [
'created' => $values['created'],
'changed' => $values['created'],
]);
}
}
}
}
* Adds related content to the group.
*/
protected function addGroupContent($group, $group_content) {
$nids = array_keys($group_content);
$nodes = Node::loadMultiple($nids);
foreach (
$nodes as $nid => $node) {// Plugin for content in the group.
$plugin_id = 'group_node:' . $node->bundle();
if ($group->getGroupType()->hasPlugin($plugin_id)) {
// Add content if only correct relation exists.
$group->addRelationship($node, $plugin_id, [
'created' => $group_content[$nid]['created'],
'changed' => $group_content[$nid]['created'],
]);
}
}
}
* Adds subgroups to the group.
*/
protected function addGroupSubgroup($group, $group_content) {
$nids = array_keys($group_content);
$groups = GroupEntity::loadMultiple($nids);
foreach (
$groups as $nid => $subgroup) {$plugin_id = 'ggroup:' . $subgroup->bundle();
if ($group->getGroupType()->hasPlugin($plugin_id)) {
// Add content if only correct relation exists.
$group->addRelationship($subgroup, $plugin_id, [
'created' => $group_content[$nid]['created'],
'changed' => $group_content[$nid]['created'],
]);
}
}
}
}
?>
Надеюсь идея понятна - в Group destination plugin мы ссылки создаем сами.
Ой, это решение для переезда Organic Groups -> Group (https://www.drupal.org/project/group). Но я думаю что логика там будет точно такая же.