> For the complete documentation index, see [llms.txt](https://cathy-thy.gitbook.io/drupal-9/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cathy-thy.gitbook.io/drupal-9/custom-modules/hook-introduction/hook_entity_type_update/sample-code.md). Agents can also search and read this documentation through its [MCP server](https://cathy-thy.gitbook.io/drupal-9/~gitbook/mcp).

# Sample Code

{% embed url="<https://github.com/cathy-thy/d9-demo/tree/main/custom/set_custom_msg>" %}
View my full code
{% endembed %}

### 1. Folder Structure&#x20;

![](https://3571417386-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2c2poVQgh9M8bTCLRSBz%2Fuploads%2F2q3jqeMq017cVUym2mpZ%2Fimage.png?alt=media\&token=cbaa2ee4-9c2e-426f-81db-6cea747a12f2)

### 2. Enable Module

![](https://3571417386-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2c2poVQgh9M8bTCLRSBz%2Fuploads%2Fg9rRZbL1W1qnWc8EkAsk%2Fimage.png?alt=media\&token=2a67a344-5527-4dc8-9186-843f32cda2d8)

### 3. Code Analysis

Since we are using hook, this module will be triggered when you update a node.&#x20;

#### 3.1 Function Name

![](https://3571417386-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2c2poVQgh9M8bTCLRSBz%2Fuploads%2FsL1s4vLGqRTU6DMRa1lU%2FPicture1.png?alt=media\&token=417d7695-d888-4601-9f65-3f549583384e)

* "**hook**" is replaced by your **custom** **module name**&#x20;
* "**ENTITY\_TYPE**" is replaced by "**node**" this time, because I want to detect when node is updated.

#### 3.2 Getting Machine Name of Content Type

You can check your content type machine name on the UI.

#### 3.3 Getting the Machine Name on Your Code

This line of code gets the type of entity (machine name)

```
$type = $entity->getType();
```

#### 3.4 Comparing the Machine Name

Assuming if we update the "page" content type node, then you do something.

```
//check if we are updating node from basic page (machine name: page)
if($type == "page"){
    //set success message
    \Drupal::messenger()->addStatus(t('This is a successful message from the custom hook.'));  
}
```
