Jump to content

Best approach for repetitive field needs


MilenKo
 Share

Recommended Posts

Hello all. I know it might sound silly, but wanted to check how are you guys proceeding if in the theme development you have a need of some fields that are repeating in several templates. For example, I am having an image field that can be used as the site logo in "Settings" template or as to show the page image in the markup of the inner page template or else. I am aware, that if I leave the resizing in the markup, than the field can be used on multiple templates, but the field has some specific label/description, notes and/or placeholder that would be not-related. 

Sticking to the logo example, I have a label saying: Add your logo image here. So if I use the same field for something else, the same text would show while editing the new next page calling for image.

How are you guys taking care of this? Are you creating specific fields for every aspect of your theme or there are some tricks I am missing in the big picture?

P.S. Besides the descriptive text of the field some might require to have a single image and some an array (e.g. you don't need to upload multiple logos, unless you want to randomize or call one based on some criterias, but for a gallery - sure I would have multiple)

Link to comment
Share on other sites

Let me introduce you to Field - Template Contexts

https://processwire.com/videos/field-template-context/

As for the multiple/single image fields, I usually have a single image field that is set to return an array, and use only what I need by getting the first image or filtering with tags. I rarely, if ever need to create a second images or files field. With template contexts, change the description and notes to guide the editor and use it in the templates however you want. You might add some checks with hooks and throw error if someone added more than one image to a template.

wire()->addHookBefore('Pages::saveReady', function (HookEvent $e) {
    $limitedTemplates = ['post', 'event']; // limit to only one image

    /** @var Page $page */
    $page = $e->arguments('page');
    if(!in_array($page->template->name, $limitedTemplates)) return;
    
    if ($page->images->count > 1) throw new WireException('Only one image is allowed');
});
  • Like 3
Link to comment
Share on other sites

@abdus i like the idea of using only one field and adding options via hooks, but i don't think it's the most userfriendly to throw an exception after pagesave in case someone uploads more than 1 image. i guess it should be possible to set the max-file setting and show the upload on the client side before upload?

  • Like 2
Link to comment
Share on other sites

You're absolutely right, @bernhard. It's certainly possible. When an exception is thrown, page doesn't save and whatever changes has been made will be reverted back. It was more of a proof of concept. I guess showing a warning instead of throwing exception would be a better, non-destructive way of warning the editors.

I'm not sure if I can hook into ProcessPageEdit and change image field settings before it's rendered. I'm going to look into it.

  • Like 1
Link to comment
Share on other sites

@bernhard, I've found a way. Hooking ProcessPageEdit::buildForm works

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;
});

When I try to add more images, field just replaces the first one, not allowing more than one. If there were multiple images before, they disappear after page save, only the first one remains.

  • Like 8
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...