Front to Back 07

Planning development.

Templates to meta fields

The idea behind the Front to Back plugin is to allow for a developer to write a page template file, define the meta fields that particular page will need in the same template file and have the site content curator presented with the meta fields for the page in the backend.
See an example and a longer explanation in the first post.
This is heavily inspired by Perch CMS.

Code flow

I’ve put in place the part of the plugin that’s taking care of managing template files when a post is created, updated or deleted and it’s now time to lay out the flow to go from those templates to the page administration UI.
I will start with a usage scenario to move development forward and a simple one at that to start:

Given the user can edit pages And the template for that page exists in the templates folder And the template defines a text meta field with a title of “User string” And no meta boxes have been added programmatically to the page post type or the page post When the user accesses the page edit screen Then the user should be presented with a text meta field titled “User string”

The template file itself will be

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page template</title>
</head>
<body>
   <h3>Here is an editable string</h3>
   <p>
       <ftb-text title="User string" />
   </p>
</body>
</html>

Which is nothing complex and as such a good start.
What needs to happen to go from this template file to Custom Meta Boxes 2 managed meta boxes?

  1. the template must be searched for ftb-* pseudo tags
  2. each ftb-* pseudo tag that’s defining a meta field a meta field must be registered in a CMB2 configuration function
  3. CMB2 will finally process said configuration and add the meta fields

Times

Starting from third point of the list above the last possible moment a configuration can be passed to CMB2 is the cmb2_admin_init hook.
This hook is called in CMB2 bootstrap file on the init hook, in administration screens only and on priority above 9000. Taking worst case scenario into account a page might not have a template created for it yet: in that case the template creator class will create a duplicate of the master template on the load-page.php hook which comes after the init one and, in turn, the cmb2_admin_init one.
Attempting to read a template that’s not there would be silly so I’ve added a redirection on the same page upon template creation.
This out of the way it’s time to parse those fields.

Next

I will begin the TDD development of field parsing!