# contentauth/c2pa-drupal architecture

> Architecture diagram of the GitHub repository contentauth/c2pa-drupal, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/contentauth/c2pa-drupal
- Repository: https://github.com/contentauth/c2pa-drupal

## Overview

##### Project type and purpose
- This is a Drupal 9/10 module (“c2pa-drupal”) that extends the Drupal Field API by providing a custom image field formatter to render C2PA metadata overlays on images, and a client-side JS component that fetches and displays that metadata in the browser.

##### File structure
- Top-level files:
- c2pa.info.yml, c2pa.install, c2pa.permissions.yml, c2pa.libraries.yml → Drupal module config & hooks
- c2pa.module → any procedural hooks (if used)
- composer.json → PHP dependencies
- CSS/ and JS/ asset directories
- src/Plugin/Field/FieldFormatter/C2paImageFormatter.php → the PHP class registering the “c2pa_image_formatter”
- README, LICENSE, CONTRIBUTING, CODE_OF_CONDUCT → repo metadata

##### Key insights from README
- Two parts:
a. c2pa_image_formatter: server-side formatter that wraps images in a “c2pa-wrapper” div and attaches CSS
b. c2pa.js: client-side script that, on DOMReady, loads the C2PA SDK & web-component UI from jsdelivr CDN, scans for “c2pa-wrapper” images, extracts metadata, and injects UI overlays

##### Main components
a. Drupal Backend
- Drupal Core
- c2pa-drupal Module
- YAML config files (.info, .libraries, .install, .permissions)
- PHP Plugin (C2paImageFormatter)
b. Rendering Pipeline
- Image field → passes through C2paImageFormatter → outputs HTML `
` + `<img>` + `<link>` and `<script>` tags for assets
c. Assets
- css/c2pa.css (styling for wrapper/UI)
- js/c2pa.js (bootstrapper)
d. Browser Client
- c2pa.js executes on page load
- Dynamically loads external C2PA SDK + web components from CDN
- Scans DOM for `.c2pa-wrapper` elements
- Processes image data via SDK → extracts metadata → mounts web component UI overlays
e. External Services
- jsdelivr CDN hosting the C2PA SDK and UI web component library

##### Relationships & data flows
1. Content Editor uploads an image into a Drupal image field.
2. On page render, Drupal Core invokes C2paImageFormatter → wraps the image + attaches CSS & JS via libraries.yml.
3. Browser requests the rendered page → downloads CSS + c2pa.js.
4. c2pa.js (in the browser) fetches the C2PA SDK & web components from the CDN.
5. After dependencies load, the script locates each `.c2pa-wrapper`, reads embedded C2PA data, and injects interactive UI overlays.

##### Architectural patterns & principles
- Drupal Plugin API / FieldFormatter pattern (server-side extension point)
- Separation of concerns:
- Server-side PHP handles HTML generation & asset inclusion
- Client-side JS handles runtime processing & UI injection
- Externalization of heavy logic (C2PA SDK) to CDN

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Backend Layer
    subgraph "Backend Layer"
        DrupalCore["Drupal Core"]:::server
        subgraph "c2pa-drupal Module"
            InfoYML["c2pa.info.yml"]:::config
            LibYML["c2pa.libraries.yml"]:::config
            InstallYML["c2pa.install"]:::config
            PermYML["c2pa.permissions.yml"]:::config
            ModuleFile["c2pa.module"]:::config
            C2paFormatter["C2paImageFormatter Plugin"]:::plugin
        end
        Composer["composer.json"]:::config
        subgraph "Repository Metadata"
            README["README.md"]:::metadata
            LICENSE["LICENSE"]:::metadata
            CONTRIB["CONTRIBUTING.md"]:::metadata
            CODEOFC["CODE_OF_CONDUCT.md"]:::metadata
        end
    end

    %% Rendered Page & Assets Layer
    subgraph "Rendered Page & Assets"
        HTMLPage["Rendered HTML Page"]:::page
        CSS["css/c2pa.css"]:::asset
        JSAsset["js/c2pa.js"]:::asset
    end

    %% Browser & External CDN Layer
    subgraph "Browser & External CDN"
        Browser["Browser"]:::client
        c2paScript["c2pa.js Execution"]:::client
        subgraph "jsdelivr CDN"
            SDK["C2PA SDK"]:::external
            WebComp["Web Components"]:::external
        end
    end

    %% Connections
    DrupalCore -->|"invokes"| C2paFormatter
    InfoYML -->|"configures"| C2paFormatter
    LibYML -->|"registers assets"| C2paFormatter
    InstallYML -->|"schema/hooks"| C2paFormatter
    PermYML -->|"permissions"| C2paFormatter
    ModuleFile -->|"procedural hooks"| C2paFormatter
    Composer -->|"dependencies"| C2paFormatter
    README -->|"docs"| C2paFormatter
    LICENSE -->|"governance"| C2paFormatter
    CONTRIB -->|"contribution guidelines"| C2paFormatter
    CODEOFC -->|"code standards"| C2paFormatter

    C2paFormatter -->|"wraps image & attaches assets"| HTMLPage
    HTMLPage -->|"includes in <head>"| CSS
    HTMLPage -->|"includes before </body>"| JSAsset

    Browser -->|"requests page"| HTMLPage
    Browser -->|"downloads"| CSS
    Browser -->|"downloads"| JSAsset

    JSAsset -->|"fetches"| SDK
    JSAsset -->|"fetches"| WebComp
    JSAsset -->|"on DOMReady scans"| Browser
    JSAsset -->|"injects overlays"| Browser

    %% Click Events
    click InfoYML "https://github.com/contentauth/c2pa-drupal/blob/main/c2pa.info.yml"
    click LibYML "https://github.com/contentauth/c2pa-drupal/blob/main/c2pa.libraries.yml"
    click InstallYML "https://github.com/contentauth/c2pa-drupal/blob/main/c2pa.install"
    click PermYML "https://github.com/contentauth/c2pa-drupal/blob/main/c2pa.permissions.yml"
    click ModuleFile "https://github.com/contentauth/c2pa-drupal/blob/main/c2pa.module"
    click C2paFormatter "https://github.com/contentauth/c2pa-drupal/blob/main/src/Plugin/Field/FieldFormatter/C2paImageFormatter.php"
    click CSS "https://github.com/contentauth/c2pa-drupal/blob/main/css/c2pa.css"
    click JSAsset "https://github.com/contentauth/c2pa-drupal/blob/main/js/c2pa.js"
    click Composer "https://github.com/contentauth/c2pa-drupal/blob/main/composer.json"
    click README "https://github.com/contentauth/c2pa-drupal/blob/main/README.md"
    click LICENSE "https://github.com/contentauth/c2pa-drupal/tree/main/LICENSE"
    click CONTRIB "https://github.com/contentauth/c2pa-drupal/blob/main/CONTRIBUTING.md"
    click CODEOFC "https://github.com/contentauth/c2pa-drupal/blob/main/CODE_OF_CONDUCT.md"

    %% Styles
    classDef server fill:#D6E9F8,stroke:#2B6CB0,color:#2B6CB0,stroke-width:1px;
    classDef config fill:#E8F0FE,stroke:#4C6EF5,color:#1E3A8A,stroke-width:1px,stroke-dasharray: 5 5;
    classDef plugin fill:#BEE3F8,stroke:#3182CE,color:#1A365D,stroke-width:1px;
    classDef page fill:#F0FFF4,stroke:#2F855A,color:#2F855A,stroke-width:1px;
    classDef asset fill:#F0FFF0,stroke:#38A169,color:#276749,stroke-width:1px;
    classDef client fill:#FEF3C7,stroke:#D69E2E,color:#975A16,stroke-width:1px;
    classDef external fill:#EDF2F7,stroke:#718096,color:#4A5568,stroke-width:1px;
    classDef metadata fill:#FFF5F7,stroke:#ED64A6,color:#702459,stroke-width:1px,stroke-dasharray: 3 3;
```
