# Entity Note Block

Entity Note Block is a fully AJAX-powered Drupal module that allows site administrators to attach internal notes to entities via a modal form. Notes are saved to a custom database table and optionally mirrored as unpublished `note_log` nodes.

All interactions (Add, Edit, Delete, Clear, Reload) happen in-place, without page reloads, for a smooth and responsive editorial experience.

## Features

- Add internal notes to nodes, users, taxonomy terms, or any entity.
- AJAX-based modal form with no page reloads.
- Notes saved in a custom table (`entity_note_block`).
- Optional logging as unpublished `note_log` nodes.
- Full CRUD (Create, Read, Update, Delete) support.
- Reusable as a block that can be placed anywhere.
- Built-in reload and clear functionality.
- Developer-friendly service for interacting with notes in code.

## Installation

1. Download or clone the module into your custom modules directory:

   modules/contrib/entity_note_block

2. Enable the module via the admin interface or with Drush:

   drush en entity_note_block

3. Clear the cache:

   drush cr

## Usage

### Placing the Block

1. Navigate to **Structure → Block layout** in the admin UI.
2. Locate the block labeled:

   admin_label = @Translation("Entity Note Block")

3. Click **Place block**, assign it to a region (e.g., Sidebar or Content).
4. Save the block configuration.
5. Visit a page where the block is placed.

### Using the Modal

- Click the **Add/View Notes** button that appears in the block.
- A modal will open where you can:
  - Add a new note.
  - View existing notes in a table with timestamps.
  - Edit an existing note inline.
  - Delete notes with AJAX.
  - Clear the form fields.
  - Reload the form to see latest data.

All interactions are AJAX-powered and reflect changes without refreshing the page.

## Architecture

- Notes are saved to a custom database table: `entity_note_block`.
- When a note is saved, a hidden unpublished node (`note_log`) is also created.
- If the content type or fields don’t exist, they are auto-created by the module.
- A block plugin is used to render the modal launcher button.
- The modal is rendered using core `drupal.dialog.ajax`.

## Developer API

You can interact with notes programmatically using the service:

```php
$storage = \Drupal::service('entity_note_block.storage');

// Get all notes
$notes = $storage->getNotes();

// Get a single note
$note = $storage->getNotesValue($entity_id);

// Save a new note
$node_id = $storage->saveNoteWithNode('Some internal comment');
$storage->saveNote($node_id, 'Some internal comment');

// Update a note
$storage->updateNote($entity_id, 'Updated content');

// Delete a note
$storage->deleteNote($entity_id);
