简体   繁体   中英

How to programically add all the nodes of content type article to one group in drupal 8?

I am looking for a solution to add all the nodes of content type article to a group with group id=25, Is there any way to add them once through PHP code, I heard about the function addcontent in groups , but I don't know how to use it, please help me out

Group::addContent(ContentEntityInterface $entity, $plugin_id, $values = [])

How to use the above function?

Is there any way to do one step process?

Group::addContent(ContentEntityInterface $entity, $plugin_id, $values = [])

I need to all the content type of articles to group with gid=25

May be it will help

$group->addContent($entity, 'group_node:article');

plugin_id is actually node type

please try it.

let me try to explain more

use Drupal\\node\\Entity\\Node; use Drupal\\group\\Entity\\Group;

/**
* Implements hook_ENTITY_TYPE_insert().
*/
function yourmodulename_node_insert(Node $node) {
  if ($node->bundle() == 'article') {
    $pluginId = 'group_node:article';
    // Here create some logic to load/chose the group id's you want to 
    // add the content to
    // ... 
    foreach ($group_ids as $gid) {
      $group = Group::load($gid);
      $group->addContent($node, $pluginId);
    }
  }
}

I hope it will be more clear, foreach means you can add to multiple groups so here you can try your own logic that to which group do you want to add, hope it will make sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM