abdus

Extending Template-Field contexts to increase field reuse

Recommended Posts

Based on this forum post, I wanted to find a more elegant solution for changing a field's settings per template. Hooking the page editor after it generates the form works and may still be needed for more complex modifications. But using field & template context, it's easier to modify the field settings and it greatly reduces the need for creating an almost identical field just to adjust a few things.

In my blog post, I've written on how to extend contextual options to allow any fieldtype / inputfield settings to be changed depending on the template. Hope you find it useful, and if you have any questions or comments, feel free to post them here.

https://abdus.co/blog/doing-more-with-fewer-fields-in-processwire/

  • Like 13

Share this post


Link to post
Share on other sites

very interesting article and very well put together.

i'm wondering why for example the maxfiles setting for images fields is not activated by default for template context. any ideas on this?

  • Like 1

Share this post


Link to post
Share on other sites

From @ryan in this blog post

Quote

But thus far, it has been limited to predefined properties: field label, description, visibility, column width, and required state. I've been reluctant to go further than that, because I think there's a fine line between what's useful and what's problematic here. Taken too far, you could easily end up with a confusing mess. Ultimately, the best ones to determine what's useful for field/template context and what's not, are the developers of Fieldtypes and Inputfields. 

 

  • Like 1

Share this post


Link to post
Share on other sites

yeah, he is the developer of the images fieldtype... so the question is why he thinks it would be bad to handle allowed extensions and maxfile settings in a template context?!

Share this post


Link to post
Share on other sites
1 minute ago, bernhard said:

yeah, he is the developer of the images fieldtype... so the question is why he thinks it would be bad to handle allowed extensions and maxfile settings in a template context?!

Personal preference, I should think, but he also gave us (well, those of us skilled enough to take advantage) the tools to change that behaviour. ^-^

Share this post


Link to post
Share on other sites

Hi @abdus,

Do you know if it's possible to use this technique to add config fields such as minWidth and maxWidth to the template context for InputfieldImage? My first attempt isn't working:

// Add more image config fields to template context
$this->addHookMethod('InputfieldImage::getConfigAllowContext', function (HookEvent $e) {
    $allowables = ['minWidth', 'minHeight', 'maxWidth', 'maxHeight'];
    $e->return = array_merge($e->return, $allowables);
});

I have a feeling that the reason this doesn't work is because those config fields are inside a fieldset. Adding the whole fieldset would be fine, but the problem is that the fieldset doesn't have a name to add to getConfigAllowContext().

Any ideas?

-----

EDIT: have sussed it out. I used another hook to give names to those fieldsets.

// Give names to image config fieldsets
$wire->addHookAfter('InputfieldImage::getConfigInputfields', function(HookEvent $event) {
    /* @var InputfieldWrapper $wrapper */
    $wrapper = $event->return;
    $f = $wrapper->getChildByName('maxWidth');
    $fieldset = $f->parent;
    $fieldset->name = 'maxDimensions';
    $f = $wrapper->getChildByName('minWidth');
    $fieldset = $f->parent;
    $fieldset->name = 'minDimensions';
});

// Add more image config fields to template context
$this->addHookMethod('InputfieldImage::getConfigAllowContext', function (HookEvent $e) {
    $allowables = ['maxDimensions', 'minDimensions'];
    $e->return = array_merge($e->return, $allowables);
});

 

  • Like 4

Share this post


Link to post
Share on other sites

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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By mwjt42
      How do you include fields in Twig templates?
      Tried using {% $page->Title %}, but that creates an error.
      Thanks
    • By Livius
      Hello fellow Process wire Developers!
      Today i have encountered an issue using the repeater where I can't find out how to solve it therefore i am asking for help.
      To be specific, I am trying to get the "file" field working properly in the repeater but i couldn't find a way on how to assign a property of a field that is being used by the repeater.
      download_pdf is the name of the field that i want to access properties from.
      What i wished i could do was something like $download->download_pdf.size or $download->download_pdf->name, something like that but obviously these didn't work
      Hope there is a solution and thanks in advance!
      Best wishes,
      Livius

    • By quickjeff
      Hi Guys, 
      I recently have received a client who has various page layouts. I wanted to give them the ability to simply add their HTML into a body field with CKEditor to allow HTML customization.
      However, the client isn't very familiar with HTML.
      This has left me to build a template that has multiple of the similar field types.
      The thing is, I cannot use the repeater since I cannot repeat lets say an image for repeated item 1 and only body field for repeater item 2.
      Therefore, I have to literally add 15 fields for them to totally be able to edit the page without issues.
      What are your thoughts on this approach?
      Also, I have to name the template according to what it contains, so I was thinking of using numbers instead of words.
      Reason being, numbers can describe the amount of title fields, body fields and images.
      Therefore something like, 3-3-2.php or three-three-two.php 
      I have never run into a situation where the client needed so many fields. I was always able to get around it with repeaters etc. 
      Thoughts?
    • By EyeDentify
      Hello There fellow PW gurus.

      I am trying to make a field show as open only if the field is filled out with at string.
      for example: "my_styles.css" and stay hidden if the field is empty.

      The field in question is of type "text" if that helps.

      And i put in the "show this field only if"
      css_filename!=''

      because the field name is "css_filename"

      But no mather what the field is closed because i have choosen it as "closed" as default.

      What am i doing wrong?

      i am running PW 3.0.83 Dev.
       


    • By bobbit6k
      Hi guys, 
       
      is there a way to declare numbers other than integer and float?
       
      My issue is that intgere is not enough for me because i'm dealing with multiple of bilions (let's say something like 111,532,372,567)
       
      Thanks,
       
      Rob