2017-08-08 40 views
0

我有一个自定义模块,在安装时创建一个内容类型。如果我使用该内容类型创建内容,则在卸载该模块时该内容不会被删除。删除模块中的内容卸载Drupal 8

如何在卸载模块时删除从该内容类型创建的所有内容?

删除hook_uninstall上的模块配置无济于事。

在此先感谢!

回答

0

您必须在您的mymodule.install文件中实施hook_uninstall

在这个钩子,你就可以使用下面的代码删除每个内容:

/** 
* Implements hook_uninstall(). 
*/ 
function mymodule_uninstall() { 
    // Load services. 
    $queryFactory = \Drupal::service('entity.query') 
    $nodeStorage = \Drupal::entityManager()->getStorage('node'); 

    // Query all entity. 
    $query = $queryFactory->get('node') 
    ->condition('type', 'article'); // <-- Change the type here for yours. 
    $nids = $query->execute(); 

    // Delete entities. 
    if (!empty($nids)) { 
    $entities = $nodeStorage->loadMultiple($nids); 
    $nodeStorage->delete($entities); 
    } 
} 

您也可以使用entity_delete_multiple不过这个功能现在已经过时。 https://api.drupal.org/api/drupal/core%21includes%21entity.inc/function/entity_delete_multiple/8.2.x


希望它会帮助您解决问题。