charcoal / attachment
Charcoal Attachments Module
Requires
- php: ^7.4 || ^8.0
- charcoal/config: ^5.0
- charcoal/core: ^5.0
- charcoal/object: ^5.0
- charcoal/translator: ^5.0
Requires (Dev)
- charcoal/admin: ^5.0
- mockery/mockery: ^1.0
- php-coveralls/php-coveralls: ^2.2
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
Suggests
- charcoal/admin: To use the attachment widgets, actions, and scripts.
Replaces
- dev-main / 5.x-dev
- v5.0.0
- v4.1.0
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.2
- v4.0.1
- v4.0.0
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.2
- 0.10.7
- 0.10.6.1
- 0.10.6
- 0.10.5.3
- 0.10.5.2
- 0.10.5.1
- 0.10.5
- 0.10.4.2
- 0.10.4.1
- 0.10.4
- 0.10.3.1
- 0.10.3
- 0.10.2
- 0.10.1.4
- 0.10.1.3
- 0.10.1.2
- 0.10.1.1
- 0.10.1
- 0.10.0.2
- 0.10.0.1
- 0.10.0
- 0.9.3.1
- 0.9.3
- 0.9.2
- 0.9.1.1
- 0.9.1
- 0.9.0
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1.3
- 0.7.1.2
- 0.7.1.1
- 0.7.1
- 0.7
- 0.6.2
- 0.6.1.2
- 0.6.1.1
- 0.6.1
- 0.6
- 0.5.2
- 0.5.1
- 0.5
- 0.4.2.1
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2
- 0.1
- dev-fix/backend-active
- dev-feature/camelizePropertiesAndUseArrayAccess
- dev-mducharme-dev
- dev-joel-attachment-update
- dev-mcaskill-labeller
This package is auto-updated.
Last update: 2024-11-13 16:33:50 UTC
README
The Attachment package provides support for working with relationships between models.
Installation
composer require charcoal/attachment
Overview
The package also provides a collection of basic attachments: Document, Embed, Image, Gallery, Link Video, amongst others.
Objects
Objects in the Attachment package extend Content
, from charcoal/object, which is an AbstractModel
, from charcoal/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):
Specialized properties which can be used differently, depending on context:
All attachments are assumed to have a title
, subtitle
, description
and keywords
. Some attachments also
Read the charcoal/object documentation for the other default properties provided by the
Content
object (andRevisionableInterface
).
Read the charcoal/core documention for the other default properties provided by
AbstractModel
(andDescribableInterface
andStorableInterface
).
Type of Attachment objects
- Accordion
- A
Container
(grouping) attachment, used for accordion type of display. - By default, support
text
,image
,gallery
andembed
attachments.
- A
- 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 animage
, anddescription
to behtml
.
- File
- An uploadable Document.
- Link
- A URL (link to a resource).
- Image
- An uploadable image
- Force the
file
property to be animage
.
- Gallery
- A
Container
(grouping) attachment, used for a gallery of multiple images. - Is limited to
image
attachments.
- A
- Text
- Text (HTML) content.
- Video
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
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
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
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
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
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.