How to create a simple Drupal module that updates a custom node field I have a custom node type "product" which has a custom Decimal type field "field_price".

I would like to create a simple module in Drupal 8 to set this value to 10.75 every time I create or update a node.

edit

I have now restructured the module as follows. These are the files / folder structure I have (excluding the .info.yml) :

Module folder:
/modules/updateprices

Routing.yml:
/modules/updateprices/update_prices.routing.yml

Routing.yml code:
update_prices:
    path: /updateprices
    defaults:
        _controller: 'Drupal\updateprices\Controller\UpdatePricesController::update_prices_node_presave'
    requirements:
        _permission: 'access content'


Controller code:
/modules/updateprices/src/Controller/UpdatePricesController.php

&lt;?php
namespace Drupal\updateprices\Controller;
use Drupal\Core\Entity\EntityInterface;

class UpdatePricesController  {
        function update_prices_node_presave(EntityInterface $entity) {

          $entity_type = 'node';
          $bundles = ['product'];

          $fld_price = 'field_price';
          if ($entity-&gt;getEntityTypeId() != $entity_type || !in_array($entity-&gt;bundle(), $bundles)) {
            return;
          }
             $entity-&gt;{$fld_price}-&gt;setValue(10.75);
        } 
}


I am now getting an error:


  ReflectionException: Class
  Drupal\updateprices\Controller\UpdatePricesController does not
  exist in ReflectionMethod->__construct() (line 123 of
  /home/website.com/core/lib/Drupal/Core/Entity/EntityResolverManager.php).
Hooks, in a module, only work inside the .module file. 

You don't need a controller nor a routing file here, these are used for creating pages.

/modules/updateprices/updateprices.module

&lt;?php

use Drupal\Core\Entity\EntityInterface;

function update_prices_node_presave(EntityInterface $entity) {

  $entity_type = 'node';
  $bundles = ['product'];

  $fld_price = 'field_price';
  if ($entity-&gt;getEntityTypeId() != $entity_type || !in_array($entity-&gt;bundle(), $bundles)) {
    return;
  }
  $entity-&gt;{$fld_price}-&gt;setValue(10.75);

}