You are here

class DeleteContent in Drush Delete All 3.x

Same name and namespace in other branches
  1. 8.2 src/DeleteContent.php \Drupal\drush_delete\DeleteContent

Delete content service class.

Hierarchy

Expanded class hierarchy of DeleteContent

1 string reference to 'DeleteContent'
drush_delete.services.yml in ./drush_delete.services.yml
drush_delete.services.yml
1 service uses DeleteContent
drush_delete.entity in ./drush_delete.services.yml
Drupal\drush_delete\DeleteContent

File

src/DeleteContent.php, line 11

Namespace

Drupal\drush_delete
View source
class DeleteContent {

  /**
   * The message to be shown to the user.
   *
   * @var string
   */
  protected $saysomething;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The current database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * DeleteContent constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager object.
   * @param \Drupal\Core\Database\Connection $connection
   *   The Connection object containing the key-value tables.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $connection) {
    $this->saysomething = 'Hello World!';
    $this->entityTypeManager = $entity_type_manager;
    $this->connection = $connection;
  }

  /**
   * Displays the Hello message.
   */
  public function sayHello($name = '') {
    if (empty($name)) {
      return $this->saysomething;
    }
    else {
      return "Hello " . $name . "!";
    }
  }

  /**
   * Function that provides the deleteing content functionality.
   */
  public function deleteAllEntityType($type) {
    $nids = $this
      ->getEntityIds($type);
    if (!empty($nids)) {
      $no_delete = 0;
      foreach ($nids as $nid) {
        $this->entityTypeManager
          ->getStorage('node')
          ->load($nid)
          ->delete();
        $no_delete++;
      }
      return $no_delete . " no of contents are deleted";
    }
    else {
      return "Content not found !";
    }
  }

  /**
   * Provides the Entity Ids of the given type.
   *
   * @param string $type
   *   Entity type.
   *
   * @return array|Null
   *   The array of all the nids.
   */
  public function getEntityIds($type) {
    $nids = [];
    $result = $this->connection
      ->select('node', 'n')
      ->fields('n', [
      'nid',
    ])
      ->condition('type', $type, '=')
      ->execute()
      ->fetchall();
    if (empty($result)) {
      return NULL;
    }
    else {
      foreach ($result as $item) {
        $nids[] = $item->nid;
      }
    }
    return $nids;
  }

  /**
   * To delete the entity content.
   *
   * @param string $type
   *   Entity type.
   *
   * @return string
   *   The message to be displayed when entity contents are deleted.
   */
  public function deleteAllEntity($type) {
    $query = $this->entityTypeManager
      ->getStorage($type)
      ->getQuery();
    $nids = $query
      ->execute();
    $no_delete = 0;
    if (!empty($nids)) {
      foreach ($nids as $nid) {
        $this->entityTypeManager
          ->getStorage($type)
          ->load($nid)
          ->delete();
        $no_delete++;
      }
      return $no_delete . " entities deleted.";
    }
    else {
      return "Nothing to delete.";
    }
  }

  /**
   * To delete the taxonomy vocabulary terms.
   *
   * @param string $type
   *   Entity type.
   *
   * @return string
   *   The message to be displayed when terms are deleted.
   */
  public function deleteAllTerms($type) {
    $tids = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->getQuery()
      ->condition('vid', $type)
      ->execute();
    if (!empty($tids)) {
      $count = count($tids);
      $controller = $this->entityTypeManager
        ->getStorage('taxonomy_term');
      $entities = $controller
        ->loadMultiple($tids);
      $controller
        ->delete($entities);
      return $count . " terms deleted.";
    }
    else {
      return "Nothing to delete.";
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeleteContent::$connection protected property The current database connection.
DeleteContent::$entityTypeManager protected property The entity type manager service.
DeleteContent::$saysomething protected property The message to be shown to the user.
DeleteContent::deleteAllEntity public function To delete the entity content.
DeleteContent::deleteAllEntityType public function Function that provides the deleteing content functionality.
DeleteContent::deleteAllTerms public function To delete the taxonomy vocabulary terms.
DeleteContent::getEntityIds public function Provides the Entity Ids of the given type.
DeleteContent::sayHello public function Displays the Hello message.
DeleteContent::__construct public function DeleteContent constructor.