Optional crop for image

Hello Sebastian,
thanks for your ideas, but any combination of chunks and output filters won’t work. As I have mentioned, we are using output filter on [[*content]] field. This is moment when not all placeholders are parsed, so you are working with unparsed strings instead of parsed values.

But I have found quite an elegant way using newly added CB events.
Consider this field template (simplified use case):

<img class="mx-cb-field-image-cropped" src="[[+crops.Free.url]]" width="[[+crops.Free.targetWidth]]" height="[[+crops.Free.targetHeight]]">

If “Free” crop is not defined, you need to switch to a different template. So I used plugin for ContentBlocks_BeforeParse:

<?php
$tpl = $scriptProperties['tpl'];
$phs = $scriptProperties['phs'];
if (strpos($tpl, 'mx-cb-field-image-cropped') !== false) {
  if(!$phs["crops"] || !$phs["crops"]["free"]) {
    $modx->log(modX::LOG_LEVEL_ERROR, "changing template");
    $modx->event->output('<img src="[[+url]]" width="[[+width]]" height="[[+height]]">');
  }
}
return null;

We have it more complicated than this, rescaling with phpthumbof etc, but the basic idea work just fine.

Unfortunately this doesn’t solve bugs mentioned here: Bugs with Image input type

1 Like