PHPackages                             cwsdigital/twill-metadata - 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. cwsdigital/twill-metadata

ActiveLibrary

cwsdigital/twill-metadata
=========================

Add SEO Metadata to your Twill.io models.

v1.4.1(2y ago)3356.2k↓11.2%10[1 issues](https://github.com/cwsdigital/twill-metadata/issues)[2 PRs](https://github.com/cwsdigital/twill-metadata/pulls)1MITPHPPHP ^8.0

Since May 15Pushed 1y ago2 watchersCompare

[ Source](https://github.com/cwsdigital/twill-metadata)[ Packagist](https://packagist.org/packages/cwsdigital/twill-metadata)[ RSS](/packages/cwsdigital-twill-metadata/feed)WikiDiscussions v1.x Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (21)Used By (1)

Twill Metadata
==============

[](#twill-metadata)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5c29f73bc00ef387b8c849c08bb69733b35896aca83ba1f920922931206215da/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6377736469676974616c2f7477696c6c2d6d657461646174612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cwsdigital/twill-metadata)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/39278b47fe03f196cf2e145095aa8d4399b2f6cb040e996363f917bac1abe9cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6377736469676974616c2f7477696c6c2d6d657461646174612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cwsdigital/twill-metadata)

What it does
------------

[](#what-it-does)

This package offers a simple way to add SEO metadata to your [Twill](https://twill.io/) models by providing a drop-in fieldset to add all the required fields into your model edit form. With sensible defaults, configurable fallbacks, and a global settings screen; this package should meet most of the needs for optimising meta tags within a site.

[![default and expanded views of twill metadata fieldset](https://github.com/cwsdigital/twill-metadata/raw/master/Twill-Metadata-Preview.jpg)](https://github.com/cwsdigital/twill-metadata/blob/master/Twill-Metadata-Preview.jpg)

Requirements
------------

[](#requirements)

This package requires Laravel 8 or higher, PHP8 or higher, and Twill 3.0 or higher.

If you are looking for support for Twill 2 then make sure you use [v1.3.0](https://github.com/cwsdigital/twill-metadata/releases/tag/v1.3.0)

Upgrade Notes
-------------

[](#upgrade-notes)

**v1.0.0**
This version introduces support for translated metadata. This means if you are upgrading from pre-v1.x version in an existing site with content you will need to migrate your content from the columns on the `metadata` table to the `metadata_translations` table.

**v1.1.0**
This version drops the translated columns from the `metadata` table.

**WARNING! Do not upgrade to v1.1.x from a pre-1.0 installation on an existing site with content. YOU WILL LOSE DATA.**If you wish to upgrade to this version, upgrade to v1.0.0 first, then perform any content migrations required. Only once you have moved all translatable data from the `metadata` table to the `metadata_translations` table should you upgrade to v1.1.x.

**v1.4.0**
This version drops support for Twill 2 as the package now uses the new Twill 3.x Settings rather than the legacy Twill 2.x

**Note:** When migrating from 1.3.0 to 1.4.0 any settings saved in the metadata-settings will need to be re-entered in the CMS in the new settings section.

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

[](#installation)

First you want to install this dependency using composer, you can do this by running the following command:

```
$ composer require cwsdigital/twill-metadata
```

Next we need to migrate the required database tables, you can do this by running the Laravel migration command:

```
$ php artisan migrate
```

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

[](#configuration)

### Adding metadata to a Module

[](#adding-metadata-to-a-module)

If your module requires SEO Metadata (e.g. Pages) then you need to update the following files:

1. ModuleModel
2. ModuleController
3. ModuleRepository

#### 1 – Update the Module Model

[](#1--update-the-module-model)

Set your model to use the `HasMetadata` trait, and add the public property `$metadataFallbacks`.

Note: Your model also must include the HasMedias trait. This trait is used for generating for OpenGraph images.

```
// App/Models/Page.php
class Page extends Model {

    use HasMetadata;
    use HasMedias;

    public \Illuminate\Contracts\Foundation\Application|array|\Illuminate\Config\Repository|\Illuminate\Foundation\Application $metadataFallbacks = [];
...
}
```

#### 2 – Update the Module Controller

[](#2--update-the-module-controller)

In the Twill admin controller for the module you need to call the fieldset for the Metadata. We do this by making use of BladePartial.

```
// App/Http/Controllers/Admin/PageController.php
public function getForm(TwillModelContract $model): Form
{
    $form = parent::getForm($model);

    // add your fields here...

    // copy the below to include metadata fieldset
    $form->addFieldset(
        \A17\Twill\Services\Forms\Fieldset::make()
        ->title(trans('twill-metadata::form.titles.fieldset'))
        ->id('metadata')
        ->fields([
            \A17\Twill\Services\Forms\BladePartial::make()->view('twill-metadata::includes.metadata-fields')
            ->withAdditionalParams([
                'metadata_card_type_options' => config('metadata.card_type_options'),
                'metadata_og_type_options' => config('metadata.opengraph_type_options'),
            ]),
        ])
    );

    return $form;
}
```

#### 3 – Update the Module Repository

[](#3--update-the-module-repository)

Add `use HandleMetadata` onto your page repository.

```
// App/Repositories/PageRepository.php
class PageRepository extends ModuleRepository
{
    use HandleBlocks, HandleSlugs, HandleMedias, HandleFiles, HandleRevisions, HandleMetadata;

    public function __construct(Page $model)
    {
        $this->model = $model;
    }
}
```

### Global Settings

[](#global-settings)

Global settings for metadata allows you to set defaults for the following:

1. Meta Title – this will be appended after the page meta title.
2. Social Graph Image - this will render as the fallback sharing image
3. Twitter (X) handle for the social card.

#### 1 – Make sure the default social media image crop is in your twill.php config

[](#1--make-sure-the-default-social-media-image-crop-is-in-your-twillphp-config)

```
{{-- config/twill.php --}
return [
    'settings' => [
        'crops' => [
            'default_social_image' => [
                'default' => [
                    [
                        'name' => 'default',
                        'ratio' => 1.91 / 1,
                        'minValues' => [
                            'width' => 1200,
                            'height' => 627,
                        ],
                    ],
                ],
            ],
        ],
    ]
];
```

#### 2 – Create new settings file

[](#2--create-new-settings-file)

```
{{-- views/twill/settings/seo/metadata.blade.php --}}
@twillBlockTitle(twillTrans('twill-metadata::form.titles.fieldset'))
@twillBlockIcon('text')
@twillBlockGroup('app')

@metadataSettings
```

#### 3 – Add to Twill Settings Menu

[](#3--add-to-twill-settings-menu)

```
{{-- app/Providers/AppServiceProvider.php --}}
public function boot(): void
{
    // Register Twill Settings
    TwillAppSettings::registerSettingsGroups(
        SettingsGroup::make()->name('seo')->label(trans('twill-metadata::form.titles.fieldset')),
    );

}
```

How to use Metatags in your frontend code.
------------------------------------------

[](#how-to-use-metatags-in-your-frontend-code)

Firstly we need to set the Metadata.

Let's assume you have a module called Pages which has Metadata linked.

In your frontend routes you will have something like:

```
Route::get('{slug}', \App\Http\Controllers\Frontend\PageController::class)
    ->name('frontend.page')->where('slug', '.*');
```

In the controller for your frontend application you can add the trait `SetsMetadata` and then use the `setMetadata()` function to set the metadata.

```
