PHPackages                             charcoal/attachment - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. charcoal/attachment

ActiveLibrary[File &amp; Storage](/categories/file-storage)

charcoal/attachment
===================

Charcoal Attachments Module

v5.0.0(2y ago)0131MITPHPPHP ^7.4 || ^8.0

Since Aug 30Pushed 2y ago2 watchersCompare

[ Source](https://github.com/charcoalphp/attachment)[ Packagist](https://packagist.org/packages/charcoal/attachment)[ Docs](https://charcoal.locomotive.ca)[ RSS](/packages/charcoal-attachment/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (9)Versions (89)Used By (1)

Charcoal Attachment
===================

[](#charcoal-attachment)

The Attachment package provides support for working with relationships between models.

Installation
------------

[](#installation)

```
composer require charcoal/attachment
```

Overview
--------

[](#overview)

The package also provides a collection of basic attachments: Document, Embed, Image, Gallery, Link Video, amongst others.

### Objects

[](#objects)

Objects in the Attachment package extend `Content`, from [charcoal/object](https://github.com/charcoalphp/object), which is an `AbstractModel`, from [charcoal/core](https://github.com/charcoalphp/core).

In addition from the default metadata provided by `Content`, the following properties are default for all `Attachment` objects:

**Standard properties** (used by all attachments objects):

PropertyTypeDescription**id**`id`A uniqid, for referencing.**title**`string` (l10n)**show\_title**`boolean`**categorie**`object (multiple)By default, categories are `charcoal/attachment/object/category/generic` objects.**subtitle**`string` (l10n)**description**`text` (l10n)**keywords**`string` (l10n, multiple)Typically used for searching purpose.**type**`string`**Specialized properties** which can be used differently, depending on context:

PropertyTypeDescription**link**`string` (l10n)URL.**file**`file` (l10n)Uploadable file, or "document".**file\_size**`string`The uploaded file size, in bytes (auto-generated).**file\_type**`string`The uploaded file mimetype (auto-generated).**thumbnail**`image` (l10n)**embed**`text` (l10n)Typically a video.All attachments are assumed to have a `title`, `subtitle`, `description` and `keywords`. Some attachments also

> Read the [charcoal/object](https://github.com/charcoalphp/object) documentation for the other default properties provided by the `Content` object (and `RevisionableInterface`).

> Read the [charcoal/core](https://github.com/charcoalphp/core) documention for the other default properties provided by `AbstractModel` (and `DescribableInterface` and `StorableInterface`).

#### Type of Attachment objects

[](#type-of-attachment-objects)

- **Accordion**
    - A `Container` (grouping) attachment, used for accordion type of display.
    - By default, support `text`, `image`, `gallery` and `embed` attachments.
- **Attachment**
    - The most generic attachment, can be anything.
- **Container**
    - Base "grouping" attachment.
- **Embed**
    - Embedded content, typically video embed code.
    - Force the `file` property to be an `image`, and `description` to be `html`.
- **File**
    - An uploadable *Document*.
- **Link**
    - A URL (link to a resource).
- **Image**
    - An uploadable image
    - Force the `file` property to be an `image`.
- **Gallery**
    - A `Container` (grouping) attachment, used for a gallery of multiple images.
    - Is limited to `image` attachments.
- **Text**
    - Text (HTML) content.
- **Video**

### Widgets

[](#widgets)

The packages provides its own admin widgets namespaced as `Charcoal\Admin`.

The attachment widget can be use more than once in a form. In order for it to work properly, you need to define a group ident `group` different for each instanciated widgets.

```
"attachment": {
    "type": "charcoal/admin/widget/attachment",
    "group": "main"
}
```

In this case, we set the group to "main". If none defined, the default group will be "generic". Without those ident, widgets won't be able to know which attachments are his.

You can than access a perticular "group" attachments calling the object's method "attachments(group\_ident)". In this case, `$object->attachments('main')` will return attachments associated with the widgets that has the group set to "main".

Configuration
-------------

[](#configuration)

Add the views path and metadata path to the config file.

```
"metadata": {
    "paths": [
        "...",
        "vendor/charcoal/attachment/metadata/"
    ]
},
"view": {
    "paths": [
        "...",
        "vendor/charcoal/attachment/templates/"
    ]
},
"translations": {
    "paths": [
        "...",
        "vendor/charcoal/attachment/translations/"
    ]
}
```

Then, we need to add the necessary routes for the widgets in admin.json config file.

```
"routes": {
    "actions": {
        "join": {
            "ident": "charcoal/admin/action/join",
            "methods": [ "POST" ]
        },
        "add-join": {
            "ident": "charcoal/admin/action/add-join",
            "methods": [ "POST" ]
        },
        "remove-join": {
            "ident": "charcoal/admin/action/remove-join",
            "methods": [ "POST" ]
        }
    }
}
```

Usage
-----

[](#usage)

You need to make your object(s) "Attachment Aware", so that it knows it can have attachments. To do that, use/implement attachmentAware:

```
use Charcoal\Attachment\Traits\AttachmentAwareTrait;
use Charcoal\Attachment\Interfaces\AttachmentAwareInterface;
```

Then, just add in the widget in the edit dashboard or the form like this:

```
"attachment": {
    "title": "Documents",
    "type": "charcoal/admin/widget/attachment",
    "group": "main",
    "attachable_objects": {
        "charcoal/attachment/object/file": {
            "label": "Document / File"
        }
    }
}
```

Available attachable objects as provided by the current modile are:

- `charcoal/attachment/object/image`
- `charcoal/attachment/object/gallery`
- `charcoal/attachment/object/file`
- `charcoal/attachment/object/link`
- `charcoal/attachment/object/text`
- `charcoal/attachment/object/video`

To create a new attachment, you need to extend the base Attachment object `charcoal/attachment/object/attachment` and provide a "quick" form.

To remove unnecessary join when deleting an object, you need to add this to your object:

```
public function preDelete()
{
    // AttachmentAwareTrait
    $this->removeJoins();
    return parent::preDelete();
}
```

### Attachment creation

[](#attachment-creation)

The one thing you need to know about the attachment is that it is all in a single table. You can't associate custom objects with other objects if they are not `attachments`.

Then, how could you create new attachments? It all depends on what you want.

#### Adding or modifying properties

[](#adding-or-modifying-properties)

IF you need to add properties to an existing attachment, you can always extend it. Let's say you want to change the editor options for the description field given with the attachments. The first step is to create a new object that will extend the existing one.

```
/**
 * Extended text class.
 */
namespace My\Namespace;

use Charcoal\Attachment\Object\Text as AttachmentText;

class Text extends AttachmentText
{
}
```

Now that we have the extend, let's add to the JSON by creating a `my/namespace/text.json` file.

```
{
    "properties": {
        "description": {
            "editor_options": {
                "style_formats": [],
                "body_class": "s-wysiwyg",
                "content_css": "../../../../../styles/main.css"
            }
        }
    },
    "data": {
        "type": "my/namespace/text"
    }
}
```

In that case, the editor options are changed to remove the base style formats, change the body class and add the appropriate css. The important part is to set the data type to the current object. This is used in live edit and delete features.

If you added some extra properties, you can use the alter script to add them into the table.

`vendor/bin/charcoal admin/object/table/alter --obj-type=my/namespace/text`

Notes
-----

[](#notes)

**Don't use "attachments" method directly in mustache template**. This will return ALL attachments without considering the group.

Custom templates for the attachment preview in the backend widget is on the to-do list.

Other actions such quick view are on the to-do list as well.

Resources
---------

[](#resources)

- [Contributing](https://github.com/charcoalphp/.github/blob/main/CONTRIBUTING.md)
- [Report issues](https://github.com/charcoalphp/charcoal/issues) and [send pull requests](https://github.com/charcoalphp/charcoal/pulls)in the [main Charcoal repository](https://github.com/charcoalphp/charcoal)

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~32 days

Recently: every ~15 days

Total

83

Last Release

796d ago

Major Versions

0.10.7 → v2.1.22022-06-21

v2.2.3 → v3.1.12022-08-08

v3.1.8 → v4.0.02022-09-21

v4.1.0 → v5.0.02024-03-13

PHP version history (3 changes)0.9.3PHP &gt;=5.6.0 || &gt;=7.0

0.10.3PHP  &gt;=7.1

v2.1.2PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfb071c0ff7ce9500c528a003a2c53124248debc3e5bf367c17f89f5e6136125?d=identicon)[mducharme](/maintainers/mducharme)

![](https://www.gravatar.com/avatar/0a4f39523b4b2837562ba0848a0327b8d340118d1ba87cb0f5d59b1d5cb6beba?d=identicon)[mcaskill](/maintainers/mcaskill)

![](https://www.gravatar.com/avatar/f3f29e38395113e2400bdd7e51bea982b17f120d4d5a53d4473b53118ee46f8d?d=identicon)[JoelAlphonso](/maintainers/JoelAlphonso)

![](https://www.gravatar.com/avatar/4229f19eecd12c2b651b6502dcc5adfba48c5770db3d2dbea55fc92c7a246b2b?d=identicon)[BeneRoch](/maintainers/BeneRoch)

---

Top Contributors

[![mcaskill](https://avatars.githubusercontent.com/u/29353?v=4)](https://github.com/mcaskill "mcaskill (106 commits)")[![JoelAlphonso](https://avatars.githubusercontent.com/u/10762266?v=4)](https://github.com/JoelAlphonso "JoelAlphonso (39 commits)")[![BeneRoch](https://avatars.githubusercontent.com/u/3017380?v=4)](https://github.com/BeneRoch "BeneRoch (38 commits)")[![dominiclord](https://avatars.githubusercontent.com/u/1775204?v=4)](https://github.com/dominiclord "dominiclord (22 commits)")[![actions-user](https://avatars.githubusercontent.com/u/65916846?v=4)](https://github.com/actions-user "actions-user (18 commits)")[![mducharme](https://avatars.githubusercontent.com/u/12157?v=4)](https://github.com/mducharme "mducharme (13 commits)")[![veve40](https://avatars.githubusercontent.com/u/7537381?v=4)](https://github.com/veve40 "veve40 (2 commits)")[![losted](https://avatars.githubusercontent.com/u/165665?v=4)](https://github.com/losted "losted (2 commits)")

---

Tags

attachmentblockscharcoalcontentphpread-only-repository

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/charcoal-attachment/health.svg)

```
[![Health](https://phpackages.com/badges/charcoal-attachment/health.svg)](https://phpackages.com/packages/charcoal-attachment)
```

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15161.6M2.6k](/packages/illuminate-filesystem)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)[madnest/madzipper

Easier zip file handling for Laravel applications.

1382.3M6](/packages/madnest-madzipper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
