Meta Data

Enso Meta is a package to make it easy(er) to add Meta Data to posts/pages/things. It should be automatically installed by Enso Core

There are a few parts to it:

  1. The meta database table that stores the data
  2. The relationship between your models and the meta models
  3. The bit that adds the fields to your backend forms.
  4. The view that outputs them on the frontend

Creating the table

This should happen automatically when you run php artisan enso:install

Add the EnsoMedia Facade

Add the following to config/app.php.

'EnsoMeta' => Yadda\Enso\Facades\EnsoMeta::class,

Model-Meta Relationship

This bit is a work in progress!

You can use the Yadda\Enso\Meta\Traits\HasMeta trait to add meta to your model.

This will add an relationship meta to your model.

namespace App;

use Illuminate\Database\Eloquent\Model;
use Yadda\Enso\Meta\Traits\HasMeta;

class Thing extends Model
{
    use HasMeta;

    // ...
}

Backend Form Fields

The php artisan enso:make:crud will do this for you.

To do it manually, add the following to the create method of your app/Crud/Thing.php configuration file.

$this->getMetaTab($form);

Outputting on Frontend

Make your meta available

This is done by default on Pages but you will need to do it yourself on models/controllers that you create.

// Use the EnsoMeta Facade
use EnsoMeta;

// Find a Meta model somewhere. Probably from a
// relationship on the model you are displaying
// but you can create them on the fly if you need to
$meta = new Yadda\Enso\Meta\Models\Meta;

// Use that Meta
EnsoMeta::use($meta);

Output the meta tags

In you <head> somewhere, remove your <title> attribute and replace it with this:

@include('enso-meta::display')

Fallback Order

When a Facebook title is requested the first item from the list below that exists (and is not an empty string) will be used:

  • Facebook Title - specified in the Meta tab of the item
  • Meta Title - specified in the Meta tab of the item
  • Override Title - depends on the setup but probably the item title, specified in code on the controller (see below)
  • Facebook Title - specified in the Settings Meta tab
  • Meta Title - specified in the Settings Meta tab

The same goes for Facebook description and image and Twitter title, description and image.

Overrides - customising meta to work with your models and controllers

You probably don't want the client to have to enter meta data to get relevant meta data from the page. For instance, your models probably have a title which could be used as the meta title and could then be overridden in the Meta tab if the client wants to.

This is what overrides are for. They don't override the meta entirely, they just override the fallbacks. If you can think of a better name, please let me know.

For example:

// MyThingController.php

// Find a model that has meta attached
$thing = Thing::findOrFail($id);

// Get the meta object from it
$meta = $thing->getMeta();

// Apply a some overrides
$meta->overrideTitle($thing->title);
$meta->overrideImage($thing->previewImage);
$meta->overrideDescription($thing->excerpt);
$meta->overrideKeywords($thing->tags->implode(',');