An open API service indexing awesome lists of open source software.

https://github.com/daun/statamic-embed-fieldtype

Fieldtype for embedding and previewing external content in Statamic
https://github.com/daun/statamic-embed-fieldtype

embed statamic statamic-addon

Last synced: about 1 month ago
JSON representation

Fieldtype for embedding and previewing external content in Statamic

Awesome Lists containing this project

README

          

# Statamic Embed Fieldtype

Fieldtype for embedding and previewing external content.

Example embed field

## Features

- Augment URLs with embed data: iframe, thumbnail, title, description
- Live preview of embedded content in the control panel
- Powered by the [Embed](https://github.com/php-embed/Embed) library and extensible with custom adapters
- Supports any provider with oEmbed endpoint or Open Graph meta tags

## Vs. the Built-In Video Fieldtype

If you need to embed videos from YouTube and Vimeo, you should probably stick with Statamic's
built-in [video fieldtype](https://statamic.dev/fieldtypes/video). It comes with less complexity
as it doesn't need to fetch the metadata or embed codes.

The fieldtype of this addon becomes useful if you need:

- Other providers and content types like audio players, social media posts, etc
- Ready-to-render html for embedding the content in an iframe
- Additional metadata like title, description, thumbnail, etc

## Installation

Install the addon via Composer:

```bash
composer require daun/statamic-embed-fieldtype
```

## Fieldtype

The addon ships with an `embed` fieldtype that accepts a url and shows a preview in the editor.

```yaml
fields:
-
handle: embed
field:
type: embed
display: Embed
```

## Frontend

The fieldtype augments the url to an array of embed data. The following example
will render a simple embed card with either an iframe (if available) or a preview
image.

```html
{{ if embed:url }}

{{ if embed:code }}


{{ embed:code:html }}

{{ elseif embed:image }}

{{ /if }}
{{ if embed:title }}


{{ embed:title }}


{{ if embed:description || embed:author:name }}

{{ embed:description ?? embed:author:name }}


{{ /if }}
{{ if embed:provider:url }}

{{ embed:provider:url | replace('https://', '') }}


{{ /if }}

{{ /if }}

{{ /if }}
```

### Unaugmented URL

If you want to use the raw url without augmentation, you can access it via `embed:url`. To turn off
augmentation completely, you can set the field config `augment_to_embed_data` to `false`. This will
return the url as-is in all frontend contexts.

```diff
fields:
-
handle: embed
field:
type: embed
display: Embed
+ augment_to_embed_data: false
```

## Extending the Embed Library

The addon uses the [Embed](https://github.com/php-embed/Embed) library under the hood, which supports
a variety of providers out of the box: YouTube, Vimeo, Instagram, Flickr, etc. You can customize the
library instance and settings and also create custom adapters for unsupported providers by registering
a resolving callback in your service provider. See [Extending Embed](https://github.com/php-embed/Embed?tab=readme-ov-file#extending-embed)
for details and examples.

```php
namespace App\Providers;

use Embed\Embed;

class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->afterResolving(Embed::class, function (Embed $embed) {
$embed->getExtractorFactory()->addAdapter('mysite.com', MySite::class);
$embed->setSettings([
'instagram:token' => '12345678',
'twitter:token' => 'abcdefgh',
]);
});
}
}
```

## License

[MIT](https://opensource.org/licenses/MIT)