Created
March 3, 2017 13:36
-
-
Save colorfield/1a7ca609d7309f56e988dbd6957c76e2 to your computer and use it in GitHub Desktop.
Delete all entities of a bundle type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\rest_import\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Drupal\Core\Entity\Query\QueryFactory; | |
use Drupal\Core\Entity\EntityTypeManager; | |
/** | |
* Class DeleteEntitiesController. | |
* | |
* @package Drupal\rest_import\Controller | |
*/ | |
class DeleteEntitiesController extends ControllerBase { | |
/** | |
* Drupal\Core\Entity\Query\QueryFactory definition. | |
* | |
* @var \Drupal\Core\Entity\Query\QueryFactory | |
*/ | |
protected $entityQuery; | |
/** | |
* Drupal\Core\Entity\EntityTypeManager definition. | |
* | |
* @var \Drupal\Core\Entity\EntityTypeManager | |
*/ | |
protected $entityTypeManager; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(QueryFactory $entity_query, EntityTypeManager $entity_type_manager) { | |
$this->entityQuery = $entity_query; | |
$this->entityTypeManager = $entity_type_manager; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('entity.query'), | |
$container->get('entity_type.manager') | |
); | |
} | |
/** | |
* Deletes all entities from a bundle type. | |
* | |
* @return string | |
* Return markup. | |
*/ | |
public function deleteEntities($entity_type, $entity_bundle) { | |
$query = $this->entityQuery->get($entity_type); | |
switch($entity_type) { | |
case 'node': | |
$query->condition('type', $entity_bundle); | |
break; | |
case 'taxonomy_term': | |
$query->condition('vid', $entity_bundle); | |
break; | |
} | |
$ids = $query->execute(); | |
$storage = $this->entityTypeManager()->getStorage($entity_type); | |
$entities = $storage->loadMultiple($ids); | |
$storage->delete($entities); | |
return [ | |
'#type' => 'markup', | |
'#markup' => $this->t('Delete entities with parameter(s): $entity_type, $entity_bundle'), | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
entity_helpers.controller_deleteEntities: | |
path: '/admin/entity_helpers/delete/{entity_type}/{entity_bundle}' | |
defaults: | |
_controller: '\Drupal\entity_helpers\Controller\DeleteController::deleteEntities' | |
_title: 'Delete entities' | |
requirements: | |
_permission: 'administer content' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment