我需要删除Drupal 8中相同类型的所有节点(节点超过7k).
这对Drupal 7来说不是问题(DB query + node_delete或node_delete_multiple会解决我的问题).但是,D8略有不同:)
请,建议,我该怎么做.提前致谢!
col*_*lan 20
应该使用实体查询而不是直接对数据库执行操作:
$result = \Drupal::entityQuery('node')
->condition('type', 'my_content_type_name')
->execute();
entity_delete_multiple('node', $result);
Run Code Online (Sandbox Code Playgroud)
像其他答案那样设置范围应该不会太困难.
有关更多信息,请参阅EntityFieldQuery已被重写.
嗯,答案就在于表面:
$types = array('my_content_type_name');
$nids_query = db_select('node', 'n')
->fields('n', array('nid'))
->condition('n.type', $types, 'IN')
->range(0, 500)
->execute();
$nids = $nids_query->fetchCol();
entity_delete_multiple('node', $nids);
Run Code Online (Sandbox Code Playgroud)
我建议你使用"范围"和某种"批处理"(或者只是多次重新运行代码),因为它是一个非常胖的操作(每个操作500个节点可以用于256MB).
要执行此代码,您可以编写自定义模块或使用devel模块:https://www.drupal.org/project/devel
安装完成后,转到yoursite_address/devel/php并在那里执行php代码.
小智 6
Drupal 8具有按内容类型获取节点的功能,因此我会使用
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties(array('type' => 'your_content_type'));
foreach ($nodes as $node) {
$node->delete();
}
Run Code Online (Sandbox Code Playgroud)
从Drupal 8.0.x开始不推荐使用entity_delete_multiple,它将在Drupal 9.0.0之前删除。使用实体存储的delete()方法删除多个实体:
// query all entities you want for example taxonomy term from tags vocabulary
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', 'tags');
$tids = $query->execute();
$storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage_handler->loadMultiple($tids);
$storage_handler->delete($entities);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13501 次 |
最近记录: |