Jump to content

Limit usage of template in PageTable field


alexcapes
 Share

Recommended Posts

Hi,

I have a PageTable field where one template type should only be used once per PageTable as otherwise it will break the lazy-loading pagination on the front-end.

Is there a way to limit the number of times a template is used?

I think I basically need to create a module that...

  1. Checks children of specific template for 'date_listing' template
  2. If one child is using the template, disable this template from being available for new pages under that parent
  3. Disable the button on the PageTable field for creating a new page with 'date_listing' template

(1) is pretty straightforward but (2) and (3) have me puzzled - is it possible to disable template use dynamically?

Link to comment
Share on other sites

You would have to have some Javascript/jQuery to search the DOM for a pageTable row that uses that template and then add/remove the disabled attribute from the corresponding add new button. However, I'm not sure if the page template name is actually output anywhere in the table row or not. If it isn't, then you'd probably need to use a php hook to add it in.

Or you could pull the page ID and use AJAX to check which template that page uses :ph34r:

Link to comment
Share on other sites

This seemed like a fun challenge so I put together a little proof-of-concept module.

For this to work you need to do a couple of things:

1. Correct a bug in InputfieldPageTable.js - hopefully will be fixed in the core soon. In the meantime you can copy the InputfieldPageTable directory to /site/modules/ and change line 18 of  InputfieldPageTable.js to: Edit: sorry, I was wrong about this

2. Include "template" or "template.label" in "Table fields to display in admin" for your PageTable field.

pt1.png

 

A screenshot of the module config:

ptconfig.png

 

Download v0.0.3: LimitPageTable.zip

Edited by Robin S
LimitPageTable v0.0.3
  • Like 6
Link to comment
Share on other sites

8 hours ago, Robin S said:

This seemed like a fun challenge so I put together a little proof-of-concept module.

For this to work you need to do a couple of things:

1. Correct a bug in InputfieldPageTable.js - hopefully will be fixed in the core soon. In the meantime you can copy the InputfieldPageTable directory to /site/modules/ and change line 18 of  InputfieldPageTable.js to:


$container.parents(".InputfieldPageTable").trigger('reloaded', ['InputfieldPageTable']);

2. Include "template" in "Table fields to display in admin" for your PageTable field.

pt1.png

 

A screenshot of the module config:

module2.png

 

Download: LimitPageTable.zip

Wonderful! Thank you Robin. Working for me on v3.0.39

I'll dig through the code as there's some updates I'd like to make...

  • Display template label instead of name in admin (mainly for readability for client)
  • Disable button on saving the page in the PageTable field rather than the parent page

 

Link to comment
Share on other sites

I was wrong about the bug in PageTable - I misinterpreted what the purpose of the 'reloaded' event was.

So unfortunately there's no event that's guaranteed to trigger when the inputfield does an AJAX reload. Instead we have to check the PageTable field with every ajaxComplete(). But that's not actually a bad thing because we want the check to fire if the PageTable was inside an AJAX loaded field.

If you placed a modified InputfieldPageTable in /site/modules/ please remove this and install LimitPageTable v0.0.2: LimitPageTable.zip

 

12 hours ago, alexcapes said:

I'll dig through the code as there's some updates I'd like to make...

  • Display template label instead of name in admin (mainly for readability for client)
  • Disable button on saving the page in the PageTable field rather than the parent page

1. This is straightforward - in "Table fields to display in admin" just use template.label instead of template, and change the module to match against tpl_label:

return $(this).text() == tpl_label;

I considered making this the default for the module but I don't like the header given to the table column when this option is set.

column.png

2. Not sure what you mean here - the button is disabled in the PageTable field, isn't it?

  • Like 2
Link to comment
Share on other sites

On 04/11/2016 at 10:00 PM, Robin S said:

I was wrong about the bug in PageTable - I misinterpreted what the purpose of the 'reloaded' event was.

So unfortunately there's no event that's guaranteed to trigger when the inputfield does an AJAX reload. Instead we have to check the PageTable field with every ajaxComplete(). But that's not actually a bad thing because we want the check to fire if the PageTable was inside an AJAX loaded field.

If you placed a modified InputfieldPageTable in /site/modules/ please remove this and install LimitPageTable v0.0.2: LimitPageTable.zip

 

1. This is straightforward - in "Table fields to display in admin" just use template.label instead of template, and change the module to match against tpl_label:


return $(this).text() == tpl_label;

I considered making this the default for the module but I don't like the header given to the table column when this option is set.

column.png

2. Not sure what you mean here - the button is disabled in the PageTable field, isn't it?

1. Changing to use template.label as you describe seems to stop the module from working. Does any other code need updating for this to work?

2. When I was saving the PageTable page in the overlay I would need to save the page with the PageTable field in it before the button would disappear. However v0.0.2 seems to fix this - I see the button disappear when I click off the overlay.

 

 

 

Link to comment
Share on other sites

10 hours ago, alexcapes said:

1. Changing to use template.label as you describe seems to stop the module from working. Does any other code need updating for this to work?

Did you do this bit...?

On 5/11/2016 at 11:00 AM, Robin S said:

change the module to match against tpl_label:


return $(this).text() == tpl_label;

But in any case if you update to LimitPageTable v0.0.3 linked in my first post above it adds a config option to choose which value you want to match against so no editing of the module is needed now.

ptconfig.png

  • Like 2
Link to comment
Share on other sites

2 hours ago, Robin S said:

Did you do this bit...?

But in any case if you update to LimitPageTable v0.0.3 linked in my first post above it adds a config option to choose which value you want to match against so no editing of the module is needed now.

ptconfig.png

Ahh I'm not sure what was not working for me with the template.label but just installed LimitPageTable v0.0.3 and seems to be working perfectly for me now. Thank you Robin!

 

 

Link to comment
Share on other sites

8 hours ago, alexcapes said:

How easy would it be to turn the options into a repeater? I've found this module so useful I'm in need to use it across multiple PageTable fields.

It won't be a repeater exactly but I can do something that achieves a similar result. Bit busy right now but will update the module over the next few days.

  • Like 2
Link to comment
Share on other sites

@alexcapes, @szabesz, I've updated the module to allow the limiting of multiple PageTable fields and/or multiple templates for the same field. You can now also limit PageTable fields that only allow a single template.

Because the module config options have changed I'm afraid you'll need to re-enter your config if you are upgrading from a previous version.

I made a thread for the module over here which has a link to the GitHub repo.

  • Like 2
Link to comment
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
 Share

×
×
  • Create New...