Search code examples
drupal-8drupal-nodesdrupal-preprocess

Where to place presave functions?


I need to manipulate a content type (people) before being saved from ADMIN.

I need to save the name + surname in the title

I know the presave functions (hook_node_presave or hook_entity_presave) should be used.

My problem is where should these functions be written?

I am writing them in themes / custom / MYPROJECT / MYPROJECT.theme but they do not run

Where are the preset functions written for ADMIN to use instead of the normal ones?


Solution

  • You can put your hook in a .module file of a module. You should create a custom module for this, example below:

    in modules/custom create a new folder {PROJECT_NAME}_general now referred to as MODULE_NAME) and add 2 files: {MODULE_NAME}.info.yml and {MODULE_NAME}.module

    In the .info.yml file you just put some information regarding your custom module, for example:

    name: Name of your module
    type: module
    description: Description of your custom module
    core: 8.x
    package: Custom
    

    Now you can enable your module using either drush (drush en MODULE_NAME -y) or using "extend" from the admin menu.

    Finally add your hook to the .module file and write the desired code.

    You can also find all of this information here and here

    Hope this helps you out!