Jump to content
alexcapes

Limit usage of template in PageTable field

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?

Share this post


Link to post
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:

Share this post


Link to post
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

Share this post


Link to post
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

 

Share this post


Link to post
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

Share this post


Link to post
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.

 

 

 

Share this post


Link to post
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

Share this post


Link to post
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!

 

 

Share this post


Link to post
Share on other sites
3 hours ago, alexcapes said:

@Robin S 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. 

+1 :) 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Peter Troeger
      Hello Community 🙂
      Has anyone ever tried having multiple elements on one page that get their info with $pages->find('selector, limit=n') and tried using pagination on one of these elements without effecting the other?
      I have a slider on a page where I display content with pagination. But when I go to page two, the slider content also goes to page two, which I don't want it to do 🙂
      Any tips are greatly appreciated!
      Thanks!
      -Peter
    • By tron1000
      Hello! I use PW 3.0.98 and I have frontend editing enabled for a PageTable Field. Somehow, when I double click the field in the frontend, the iframe in wich the content is displayed is very small (see screenshot). I couldn't find out if thats some CSS conflict or another problem. Any suggestions? Thanks, Andrej
       

    • By Noel Boss
      I use a PageTable field to make edits to children of pages more intuitive…
      To register the hooks, insert the following Snippet inside your init function in your module (or add it to your init.php file):
      /** * Initialize the module. * * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. */ public function init() { // Prefill pageTable field $this->wire()->addHookBefore('InputfieldPageTable::render', $this, 'addChildrenToPageTableFieldsHook'); $this->wire()->addHookBefore('InputfieldPageTableAjax::checkAjax', $this, 'addChildrenToPageTableFieldsHook'); } Then, add this hook method:
      /** * Fill pagetable fields with children before editing…. * * @param HookEvent $event */ public function addChildrenToPageTableFieldsHook(HookEvent $event) { $field = $event->object; // on ajax, the first hook has no fieldname if (!$field->name) { return; } // get the edited backend page $editID = $this->wire('input')->get->int('id'); if (!$editID && $this->wire('process') instanceof WirePageEditor) { $editID = $this->wire('process')->getPage()->id; } $page = wire('pages')->get($editID); // disable output formating – without this, the ajax request will not populate the field $page->of(false); // you could also insert a check to only do this with sepcific field names… // $page->set($field->name, $page->children('template=DesiredTemplate')); // just specific templates $page->set($field->name, $page->children); } Now whenever there is a page-table field on your page, it gets populated with the children
    • By dragan
      Is it somehow possible to limit the number of pages one can select with the page reference field?
      I have a case where I need to have exactly three, not more, not less. Is this doable with a hook? Or would you suggest another approach? (three single pageref fields and make each mandatory)
    • By pppws
      Hey there,
      i've set up a page wich uses pageTable and it works like a charm.
      for my home page i get several pages. they are sperated in two types:
      a) page has the module 'featured_img'
      b) page doesn't have the 'featured_img'
       
      my pageTable field is called add_modules. so i was wondering if something like 
      <?php foreach ($featured as $child): ?> <?php if ($child->add_modules->module_featuredimg): ?> // show image <?php else: ?> // show text <?php endif; ?> <?php endforeach; ?> is possible?
       
      thanks for your input!
×
×
  • Create New...