Croft

Articles

Declaring a content type

A worked example: what each line of a type file does, and what the CMS builds from it.

A content type is one PHP file that returns an array. Everything the CMS knows about that kind of content comes from it.

return [
    'label'    => 'Recipe',
    'features' => ['slug', 'status', 'schedule', 'seo', 'revisions'],
    'title'    => 'name',
    'public'   => ['index' => '/recipes', 'single' => '/recipes/{slug}', 'template' => 'recipe'],
    'fields'   => [
        ['name' => 'name',   'type' => 'text',     'panel' => 'title'],
        ['name' => 'method', 'type' => 'markdown', 'panel' => 'main', 'editor' => true],
        ['name' => 'serves', 'type' => 'number',   'panel' => 'sidebar'],
    ],
];

What each part does

features switches on machinery. slug gives an address and automatic redirects when it changes. status gives draft and published. schedule adds a publish date, so a future date holds the item back until the day arrives. seo adds a per-item title, description and social image. revisions keeps every previous version.

title names the field that becomes the big input at the top of the editor, and the name shown in lists.

panel decides where a field sits: title at the top, main in the wide column, sidebar in the right rail.

public turns on the routes. Leave it out entirely and the type has no public pages — useful for things that only ever appear inside other pages, like testimonials.

Adding a field later

Add a line to fields and reload. The column is created and the input appears. Remove the line and it disappears from the admin while the column and its data stay exactly where they were.

guidetypes