I am trying to create a module in Drupal 8 (8.9.11) and this module uses the function hook_entity_presave in order to update a node / entity programmatically. I've tried the answers from hook_entity_presave() doesn't work, I was able to add those in my code.
sno.routing.yml
sno.content:
path: /node/add/issuances
defaults:
_controller: Drupal\sno\Controller\SnoController::sno_entity_presave
requirements:
_permission: 'access content'
src/Controller/SnoController.php
namespace Drupal\sno\Controller;
use Drupal\Core\Entity\EntityInterface; use Drupal\node\NodeInterface;
class SnoController {
public function sno_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityType()->id() == 'issuances') {
$entity->set('field_s', ', s. ');
}
}
}
When I got into adding a content of content type issuance (/node/add/issuances), I am getting the following error.
The website encountered an unexpected error. Please try again later.
RuntimeException: Controller "Drupal\sno\Controller\SnoController::sno_entity_presave()" requires that you provide a value for the "$entity" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one. in Symfony\Component\HttpKernel\Controller\ArgumentResolver->getArguments() (line 78 of /var/www/senate-library/vendor/symfony/http-kernel/Controller/ArgumentResolver.php).