Right now, we have a matrix field called post body which has all our content, we have text, image, video etc... In our image block we have 2 fields, An asset field called image
& a plain text field called caption
.
When I first set this site up, admittedly I didn't plan it properly at all and now we allow several images to be uploaded in the single field but we want to be able to set individual captions for each of the images.
Not sure if this is the right way to phrase the question, how would I go about moving the contents of postbody.block.caption to postbody.block.image.caption for every image in that field?
This is going to be a once off migration of content so I'm not sure if it warrants a plugin, would I be able to do this with a quick & dirty sql/yii query?
Here is what I have so far
<?php
$craft = require '../craft/app/bootstrap.php';
$criteria = $craft->elements->getCriteria(Craft\ElementType::MatrixBlock);
$criteria->fieldId = $craft->fields->getFieldByHandle('postBody')->id;
$criteria->type = "image";
$criteria->limit = null;
$blocks = $criteria->find();
foreach ($blocks as $block) {
if ($block->caption) {
foreach($block->image as $image) {
$block->image->imageCaption = $block->caption;
}
$block->caption = NULL;
$craft->matrix->saveBlock($block);
}
}
?>
But that gives Property "Craft\MatrixBlockModel.caption" is not defined.
Edit I’m now at the stage where the contents of the caption field has been saved to each of the images in that block, but i cant seem to set the caption
field on the block to null. Here is what I have at this stage
$craft = require '../craft/app/bootstrap.php';
$criteria = $craft->elements->getCriteria(Craft\ElementType::MatrixBlock);
$criteria->fieldId = $craft->fields->getFieldByHandle('postBody')->id;
$criteria->type = "image";
$criteria->limit = null;
$blocks = $criteria->find();
foreach ($blocks as $block) {
if ($block->caption) {
$caption = "<p>".$block->getContent()->caption."</p>";
foreach($block->image as $image) {
$asset = $craft->elements->getElementById($image->id);
if(empty($asset->getContent()->imageCaption)) {
$asset->getContent()->imageCaption = $caption;
$success = $craft->assets->storeFile($asset);
}
}
$block->setContent(array(
'caption' => '',
));
$success = $craft->matrix->saveBlock($block);
dd($success);
}
}