Skip to content

Instantly share code, notes, and snippets.

@abdusco
Created September 19, 2017 07:01
Show Gist options
  • Save abdusco/758342696d2a8bdb2f1de413f3bc0fea to your computer and use it in GitHub Desktop.
Save abdusco/758342696d2a8bdb2f1de413f3bc0fea to your computer and use it in GitHub Desktop.
Limit Image field to maximum 1 file for a template in ProcessWire
<?php namespace ProcessWire;
// put this in /site/ready.php
wire()->addHookAfter('ProcessPageEdit::buildForm', function (HookEvent $e) {
/** @var ProcessPageEdit $edit */
$templates = ['post', 'basic'];
$edit = $e->object;
$page = $edit->getPage();
if (!in_array($page->template->name, $templates)) return;
/** @var InputfieldForm $form */
$form = $e->return;
/** @var InputfieldImage $imageField */
$imageField = $form->children->findOne('name=images');
if (!$imageField) return;
$imageField->maxFiles = 1;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment