Here's a handy bit of code that should help you out if you want to quickly and easily create a MODX Content Blocks Template from an already created resource.
Our use case was that we're about to embark on a large website project, that requires a high degree of QC (quality checking), and we needed to create many Content Blocks templates with all there settings and content to adhere to signed off website designs.
By default to achieve this you have to build the pages on a MODX resource, set any settings/content to ensure it's right, then rebuild the entire page in the content blocks template creator.
How do you then test that what you've done is accurate, and an exact mirror of the already created (and in our case, signed off) page? Well, you have to create a new resource, and assign that newly created template to the resource and check - and if somethings wrong, you have to go back into the template editor, edit, then start the whole process again, normally then deleting all the layouts in the resource before re-adding the template again. This takes up a large degree of time. Something we certainly don't want to be doing, even more so when we've faced this on previous projects.
We new something had to be made to change this - and this is it!
The new process using the method in this article is simple. Build the page once - then when happy, use this tool to take it and make it as a template. A much cleaner, and quicker way of doing it!
This method will allow you to assign a title, description, and assign it to a category (based upon ones that you've made on your installation of MODX, and create a fully usable template with just one click.
This code is free to use, however please use at your own risk.
OK! Let's get started! Now the form isn't pretty, however it works - we'd recommend this is to be only used by site administrators, and is to be made on a resource that is unpublished (only accessible by site admins!)
Step 1 - Make a chunk called makeCBTemplate with the code below:
[[!makeCBTemplate]]
<h2>Take a page resource as make it into a MODX Content Blocks Template</h2>
<p>Ensure that the page is done how you want. This will take the whole resource, and turn it into a MODX content blocks template</p>
<form action="" method="POST">
<label>Resource ID:</label>
<input type="number" name="resource_id" value="" />
<label>Template Name:</label>
<input type="text" name="template_name" value="" />
<label>Template Description:</label>
<textarea name="template_description"></textarea>
<label>Template Category:</label>
<select name="category_id">
<option selected disabled>Select a Category</option>
[[+cb.cats]]
</select><br /><br />
<input type="submit" value="Create Template" />
</form>
Step 2 - Make a snippet called makeCBTemplate with the code below:
<?php
$modx->addPackage('contentblocks', MODX_CORE_PATH . 'components/contentblocks/model/','modx_');
$data = array();
$query = $modx->newQuery('cbCategory');
$query->sortBy('name', 'ASC');
$cats = $modx->getCollection('cbCategory', $query);
foreach ($cats as $cat) {
$data['cats'] .= '<option value="' . $cat->get('id') . '">' . $cat->get('name') .'</option>';
}
if (!empty($_POST)) {
$resource = $modx->getObject('modResource', $_POST['resource_id']);
if (!$resource) {
return "No resource with ID ". $_POST['resource_id'] . " found";
}
$properties = $resource->get('properties');
if (!$properties['contentblocks']['content']) {
return "No Content Blocks properties found against resource ID " . $_POST['resource_id'];
}
$template = $modx->newObject('cbTemplate', array(
'name' => $_POST['template_name'],
'description' => $_POST['template_description'],
'category' => $_POST['category_id'],
'icon' => 'chunk_B',
'content' => $properties['contentblocks']['content']
));
if ($template->save()) {
echo "<h2>Created Template ID " . $template->get('id') . ".</h2>";
} else {
return "Error saving template";
}
}
$modx->setPlaceholders($data, 'cb.');
Step 3 - Make the resource.
Make a new resource, and simply call the chunk - I'd ensure that it's not published - As we only want site administrators using this!
On a site with a client that you don't want them accessing this? Then move it into a folder so it's out of sight.
Viola! You're all done!
Load the resource, pop in a resource ID, Template name, description and a category and click the button. It's as easy as that!
I hope that this helps you out with your MODX content blocks project like it has for us. We love MODX here at GEL Studios - it's our website cms of choice! If you're after some MODX help, be sure to check out our MODX support page.
G