Per the PR here: https://github.com/larowlan/default_content/pull/59

I think it could be useful to give site builders the ability to delete all demo content by uninstalling the module or maybe via an admin form. Perhaps even taking this further and give the user the ability to delete demo content by type. There are many situations where you might want to display examples for the site owner to follow and then delete the demo content once you're ready to take the site live.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

justinlevi created an issue. See original summary.

andypost’s picture

That's needs more discussion about is this really needed and a way to do that

cweagans’s picture

Looks like there was some advice on the linked PR for how to handle this. If I re-do this with the suggested method, would the patch be accepted? We need this for our D8 distro.

piggito’s picture

Status: Active » Needs review
StatusFileSize
new5.11 KB

I created a patch for current dev branch based in the PR mentioned in issue summary

andypost’s picture

Status: Needs review » Needs work
  1. +++ b/default_content.module
    @@ -16,3 +16,15 @@ function default_content_modules_installed($modules) {
    +  // @todo Move this to an event once we have HookEvent.
    +  foreach ($modules as $module) {
    +    if (!\Drupal::isConfigSyncing()) {
    +      \Drupal::service('default_content.deleter')->deleteContent($module);
    

    please move usage of \Drupal:: out of loop, also I suggest to use early return if config sync happening

    Also todo looks pointless

  2. +++ b/src/Deleter.php
    @@ -0,0 +1,110 @@
    +      foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
    +        $reflection = new \ReflectionClass($entity_type->getClass());
    +        // We are only interested in deleting content entities.
    +        if ($reflection->implementsInterface(ConfigEntityInterface::class)) {
    +          continue;
    

    Please don't use reflection here, there's \Drupal\Core\Entity\EntityTypeInterface::entityClassImplements() for that

    Also not sure the check is right for config, so better to check for \Drupal\Core\Entity\FieldableEntityInterface or \Drupal\Core\Entity\ContentEntityInterface

  3. +++ b/src/Deleter.php
    @@ -0,0 +1,110 @@
    +          $entity = $this->entityRepository->loadEntityByUuid($entity_type_id, $entity_uuid);
    +          $entity->delete();
    

    Better to load all entities and delete them multiple

andypost’s picture

+++ b/src/Deleter.php
@@ -0,0 +1,110 @@
+      $root_user = $this->entityTypeManager->getStorage('user')->load(1);
+      $this->accountSwitcher->switchTo($root_user);

You're missing to $this->accountSwitcher->switchBack(); at the end and not sure why it needs to login as UID1

piggito’s picture

Status: Needs work » Needs review
StatusFileSize
new5.19 KB
new2.56 KB

Fixed comments in #5 and #6

andypost’s picture

+++ b/src/Deleter.php
@@ -98,13 +98,12 @@ class Deleter implements DeleterInterface {
+          $entities[] = $this->entityRepository->loadEntityByUuid($entity_type_id, $entity_uuid);

please index this array with uuid to prevent loading same entities and delete bugs

piggito’s picture

StatusFileSize
new5.2 KB
new597 bytes

Fixed last comment

tsplash made their first commit to this issue’s fork.

agentrickard’s picture

FWIW, in our workflow, putting this in uninstall would be very bad. We enable this module, import content, and then disable it, hal, and serialization.

More ideally this would be a drush command, see #3282547: Drush command to delete and reimporting default content,