Custom metadata

By default, media comes with a few metadata attributes that can be filled in from the media managers: Tags, Alt text and caption.

Say we want to add more metadata, some of which might be translatable. To do this we have to go through a few steps. In the steps below we will add a translatable field for attribution and a non-translatable field for source.

First thing we have to do is add the new metadata fields in the twill settings.

In the twill config (config/twill.php) we add the following:

1return [
2 'media_library' => [
3 'extra_metadatas_fields' => [
4 [
5 'name' => 'source',
6 'label' => 'source',
7 ],
8 [
9 'name' => 'attribution',
10 'label' => 'attribution',
11 ],
12 ],
13 'translatable_metadatas_fields' => [
14 'attribution',
15 ],
16 ],
17];

This will already ensure the fields are visible from the media manager. However, saving is not yet possible because the database fields are missing. So to make everything work we have to write a migration.

For regular fields, it is best to use a text field, for translatable fields we suggest to use a json field.

1<?php
2 
3use Illuminate\Database\Migrations\Migration;
4use Illuminate\Database\Schema\Blueprint;
5use Illuminate\Support\Facades\Schema;
6 
7return new class extends Migration
8{
9 public function up()
10 {
11 Schema::table(config('twill.medias_table', 'twill_medias'), function (Blueprint $table) {
12 $table->text('source')->nullable();
13 $table->json('attribution')->nullable();
14 });
15 }
16 
17 public function down()
18 {
19 Schema::table(config('twill.medias_table', 'twill_medias'), function (Blueprint $table) {
20 $table->dropColumn('source');
21 $table->dropColumn('attribution');
22 });
23 }
24};

And that is all. The most important part is to make sure your migration fields are named the same as your custom metadata values in the twill config.