Repair Crops Saved in Repeaters
Crops saved from a crop badge inside a Repeater or Repeater Matrix item before MediaHub 1.22.31 were stored in the wrong place. A repair script finds them and, if you choose, moves each one back to its repeater item.
Before MediaHub 1.22.31, a crop saved from a crop badge on a MediaHub field inside a Repeater or Repeater Matrix item was stored against the wrong page. The crop was saved, but nothing could find it again. MediaHub 1.22.31 fixes this for every crop saved from then on. Crops saved earlier stay where they are until you repair them.
A small repair script finds those older crops. You can let it move them back to the repeater item they belong to, or use its report to redo them yourself.
Do you need this?
Only if all of these are true:
- A MediaHub field with crop badges (for example 16:9 or Square) sits inside a Repeater or Repeater Matrix item.
- An editor adjusted a crop from one of those badges before your site was updated to 1.22.31.
- You want those crops back rather than redoing them.
The signs on an affected site were:
- After saving a crop from the badge and reloading the page editor, the badge showed a different crop.
- The website showed the default crop for that repeater item, not the one the editor chose.
MediaHub fields that are not inside a repeater are not affected. If you are not sure, run the dry run below: it changes nothing and tells you whether there is anything to repair.
Getting the script
The script is not included in the MediaHub download. Contact support and ask for the repeater crop repair script. You will receive a single file, mediahub-repair-repeater-crops.php.
Before you start
- MediaHub 1.22.31 or later. The script refuses to run on older versions, because they would keep saving crops the old way.
- Terminal (command line) access to the server, with PHP available. The script cannot be run from a browser; requested from a web address it returns "not found".
- A database backup before you apply any changes. The dry run does not need one.
Copy the file anywhere on the server. It does not need to be inside your site, and it is safest kept outside the public web folder. In the examples below, /var/www/example.com is the folder that contains your site's index.php and wire/ folder.
Step 1: Dry run
The dry run is the default. It lists every crop the script found and what it would do, and changes nothing.
php mediahub-repair-repeater-crops.php --root=/var/www/example.com
Example report:
MediaHub: repair crops saved inside repeaters
Site: example.com (MediaHub 1.22.31)
Mode: DRY RUN (nothing will be changed)
------------------------------------------------------------------------
[Will move] Crop #5449 preset 16_9 image #5352 "Harbour at dusk"
Stored as: page 5441, field hero_repeater5446
Belongs to: repeater item 5446, field hero on page 5441 "Summer Campaign"
Edit page: https://example.com/processwire/page/edit/?id=5441
Crop image: https://example.com/site/assets/files/5449/harbour-crop-16_9.jpg
Note: Replaces the automatic default crop #5452, which will be deleted.
[Left as is] Crop #5450 preset 16_9 image #5352 "Harbour at dusk"
Stored as: page 5441, field hero_repeater5447
Belongs to: repeater item 5447, field hero on page 5441 "Summer Campaign"
Edit page: https://example.com/processwire/page/edit/?id=5441
Crop image: https://example.com/site/assets/files/5450/harbour-crop-16_9.jpg
Note: Repeater item already has a crop saved by an editor (#5460), probably after updating. Left as is.
------------------------------------------------------------------------
Will move: 1 Left as is: 1
DRY RUN: nothing was changed.
Each entry shows:
| Line | What it tells you |
|---|---|
| Stored as | Where the crop is saved now: the outer page, and the field name with the repeater item number on the end. |
| Belongs to | The repeater item and field the crop was made for, and the page that item is on. |
| Edit page | A link to that page in the admin. |
| Crop image | A link to the crop the editor saved, so you can see it or match it by hand. |
| Note | Anything unusual, including why a crop is being left as is. |
Will move and Left as is
Will move means the crop's repeater item still exists, still has that MediaHub field, and has no crop of its own that an editor saved. These are the crops --apply will move.
Opening a page in the editor after updating makes a default crop for each badge that has none. Those defaults are made automatically, not by an editor, so the older crop the editor chose replaces them. The note says when this will happen.
Left as is means the script will not touch that crop. The note gives the reason:
| Note | What to do |
|---|---|
| Repeater item already has a crop saved by an editor | Someone adjusted the crop again after updating. That newer crop is kept. Nothing to do unless you prefer the older one, in which case redo it by hand. |
| Repeater item has an automatic crop that another page uses | The default crop has been selected somewhere else, so it is not deleted. Redo the crop by hand if you want the older one. |
| Repeater item no longer exists | The repeater item has been deleted. Nothing to repair. |
| Not a MediaHub field, or the item has no such field | The field has been changed or removed since the crop was saved. Nothing to repair. |
| Image no longer exists | The image has been deleted from the library. Nothing to repair. |
If nothing needs repairing, the report says No crops need repairing. Nothing to do.
Step 2 (optional): Fix crops by hand
You can redo the crops yourself instead of applying the changes. Save the report as a spreadsheet:
php mediahub-repair-repeater-crops.php --root=/var/www/example.com --csv > repeater-crops.csv
The file has one row per crop with these columns:
| Column | Contains |
|---|---|
crop_id | The crop's ID. |
asset_id, asset_title | The image the crop was made from. |
preset | The crop preset, for example 16_9. |
stored_page, stored_field | Where the crop is saved now. |
item_id, field | The repeater item and field it belongs to. |
owner_page, owner_title, edit_url | The page the repeater item is on, and a link to edit it. |
crop_image | A link to the crop the editor saved. |
status | move, conflict (an item that already has its own crop) or skip (the item, field or image is gone). |
replaces | The automatic default crop that would be deleted, or 0. |
note | The same note as the report. |
For each row: open the edit link, expand the repeater item, click the crop badge for that preset, match the crop to the linked image and save. Crops saved from a badge on 1.22.31 or later are stored correctly.
Step 3: Apply
Back up the database, then run:
php mediahub-repair-repeater-crops.php --root=/var/www/example.com --apply
The script prints the same report and asks you to type yes. Anything else cancels without changing anything.
Move 1 crop(s)? Back up the database first. Type yes to continue: yes
Moved crop #5449 to repeater item 5446.
Done. Moved 1, failed 0. Each move is recorded in the media-hub log.
Reload the page editor: each badge now shows the crop the editor saved, and the website shows it for that repeater item. Run the dry run again to confirm: crops that were moved no longer appear.
Each change is recorded in Setup › Logs › media-hub, in lines starting Repeater crop repair (command line).
Options
| Option | What it does |
|---|---|
--root=PATH | The folder that contains your site's index.php and wire/ folder. Defaults to the current folder. |
--apply | Make the changes. Without it, nothing is written. |
--yes | With --apply, skip the typed confirmation. Use only in scripts, after a dry run. |
--csv | Write the dry-run report as CSV. Cannot be combined with --apply. |
--help | Show the built-in help. |
What the script changes
For each crop marked Will move, the script:
- Changes which page and field the crop belongs to, from the outer page to the repeater item.
- Deletes the automatic default crop on that repeater item, if there is one. MediaHub can make it again at any time.
It never:
- Changes or deletes image files, other than the automatic default crop above.
- Touches crops an editor saved after updating, crops outside repeaters or library crops.
- Changes anything during a dry run or a CSV export.
Running it twice is safe. The second run finds only the crops it left as is.
If your site has ProcessWire's debug mode on, PHP may still print warnings of its own. They do not affect the result.
Related
- Image Cropping: crop badges, repeater behaviour from 1.22.31, and the editor UI
- Crops API: library crops and field-level crops
Last updated