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 14

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. ^-^

  • Like 1

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 7

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 dweeda
      How do I migrate a template with all its fields and values to a new site? Is their an export/import process?
    • By rareyush
      I am using module "FormTemplateProcessor" to get data in a pages but they all are unpublished and when I am trying to display them with relevent pages it not working
       
      page1 p1 p2 p3 page2 (FormTemplateProcessor) up1 (unpublished page and have p1 id on field knows as "ID") up2 (unpublished page and have p3 id on field knows as "ID") up3 (unpublished page and have p2 id on field knows as "ID") now here
      up1, ip2, up3 can be created by p1, p2 or p3 and I am saving respectively I'm saving their id in field which is being used by up1,up2,up3, etc template.
       
      now I want to show the data from unpublished pages which belongs to p1, p2 and p3 respectively
       
       
      anyone has experienced this or knows something which can help me out ?
    • By Peter Knight
      A minor gripe but one that trips me up every time.
      When there's naming error on a field, ALL the field values are erased and I have to setup everything again. IE name, type, label and descriptions details and sometimes notes.
      Would be really helpful if ProcessWire didn't zap all the values but just cleared the illegal field name. 
       

       
       
       
    • By louisstephens
      I have been following @bernhard's great tutorial on creating a Dashboard and everything has very easy to understand so far. However, I need to add a select field that a template is using (so users can create a new page from the dashboard), but I am really at a complete lost on how to achieve this. I have been googling, but can't really find anything on the matter.
      $field = $this->modules->get('InputfieldSelect'); $field->name = "Select Manufacturer"; $field->findPagesSelector = "template=basic-page"; //not needed for select field $field->labelFieldName = "title"; $field->name = "Manufacturer Select"; $field->columnWidth = 34; $fieldset->add($field); I have the above code, which was originally an InputfieldPage, but since this is an options field, I have changed it. Has anyone actually gotten a select from a template (and its' values) in a module?
    • By mwjt42
      How do you include fields in Twig templates?
      Tried using {% $page->Title %}, but that creates an error.
      Thanks