Deleting an entity
In this recipe, we will walk through deleting an entity. Deleting an entity allows you to remove an entity from the database so that it no longer exists.
How to do it…
- Create a
delete
method in theArticleController
controller in your module that has a parameter for the node entity object that will be provided by a route parameter:<?php
namespace Drupal\mymodule\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class ArticleController extends ControllerBase {
public function delete(NodeInterface $node):
JsonResponse {
}
}
- To delete an entity, you will invoke the
delete
method:public function delete(NodeInterface $node):
JsonResponse {
$node->delete();
}
The delete
method immediately removes the entity...