web-dev-qa-db-ja.com

プログラムでノードを有機グループに割り当てる

プログラムでノードを有機グループに割り当てるにはどうすればよいですか?

私は以下を試してみましたが、どれも機能しませんでした($ target_groupが$ nodeを割り当てたいグループIDであると仮定した場合):

$node->og_groups = array($target_group => $target_group);
node_save($node);

$group = node_load($target_group);
$node->og_groups[0] = $target_group;
$node->og_groups_names[0] = $group->title;

これを行う適切な方法は何ですか?

6
Luca

Drupal 7と仮定すると、APIでog_group関数を使用したいとします

/**
 * Set an association (e.g. subscribe) an entity to a group.
 *
 * @param $gid
 *   The group ID.
 * @param $values
 *   Array with the information to pass along, until it is processed in
 *   og_entity_presave(). Keys are:
 *   - "entity type": Optional; The entity type (e.g. "node" or "user").
 *     Defaults to 'user'
 *   - "entity" :Optional; The entity to set the association. Defaults to the
 *     current user if the $entity_type property is set to 'user'.
 *   - "state": Optional; The state of the association. Can be:
 *     - OG_STATE_ACTIVE
 *     - OG_STATE_PENDING
 *     - OG_STATE_BLOCKED
 *   - "save": Optional; TRUE if fields value should be saved. Defaults to TRUE.
 *   - "force reload": Optional; Determine if og_load_entity() should be used on
 *     the passed entity. This can be used when you pass an entity that has been
 *     saved yet to the database, but you want to assign it groups. Defaults to TRUE.
 *     // TODO: Adds docs on rest of keys.
 *
 * @return
 *   The entity with the fields updated.
 */
function og_group($gid, $values = array()) { ...

あなたのコードでは、これは次のようになります:

$values = array('entity type' => 'node', 'entity' => $node, 'state' => OG_STATE_ACTIVE);
og_group($target_group, $values);
node_save($node);
2
schnippy

Drupal 6の場合、ノードオブジェクトに値を割り当てるだけです。

$gnode = node_load($gid);
$node = node_laod($nid);

$node->og_initial_groups[0][$gid];
$node->og_groups[0] = $gid;
$node->og_public = 0;
$node->og_groups_names[0] = $gnode->title;

これが当てはまる場合、オーディエンスの関係が正しく保存されない場合があります。ノードを保存しながら、og_ancestryに行を挿入できます。

if($node = node_submit($node)) {
  $node = node_save($node);
  $sql = "INSERT INTO {og_ancestry} (nid, group_nid) VALUES (%d, %d)";
  db_query($sql, $node->nid, $gnode->nid);
}

完了です。ノードオブジェクトをチェックインします。

1
Jeet