Jump to content

page reference numbers in front of a title (admin area)?


Roych
 Share

Recommended Posts

Hello,

I was wondering if it's possible to add numbers in front of a title in backend in page reference field:

reference.jpg.a6a51e1db0c06f29e5822579222bc21a.jpg

#1 title   #2 title  #3 title and so on.

But only in admin. Would be great if automated when creating a new page.

Thank you

R

 

Link to comment
Share on other sites

It's definitely doable (almost anything is in PW), but before suggesting an approach, I feel like asking a few questions.

Are these numbers to be static, i.e. does a page always have to have the same number in front? Can the pages in question be deleted or resorted, and what happens to the numbers then?

Link to comment
Share on other sites

15 minutes ago, BitPoet said:

Are these numbers to be static, i.e. does a page always have to have the same number in front? Can the pages in question be deleted or resorted, and what happens to the numbers then?

hi,

well the pages can be sorted added and deleted but because alergens are usualy noted with numbers like #1 are always cereals and #5 are always Crustaceans. So when you add a new dish, it says it contains alergens 1,5,2,14, ...

and would be easyer to see the number rather than count and read through all of them. So just static number for each would be ok I guess (I would add the right number to the right alergen). Or by sorting pages, so first is #1 and so on. Hope u understand 😉 

Thank you

R

Link to comment
Share on other sites

Do you already have the numbers in the pages (as a field) that you show in the selection?

Because you can change the label field in the input tab (from the page select field) to "Custom Format" and also display multiple fields. 

  • Like 1
Link to comment
Share on other sites

In that case it's pretty straight forward:

  • Create a new field of type "Integer" for holding your allergen number, I'll call it 'allergennumber' here
  • Add the field to the template for your allergens
  • Assign a number to the existing allergens in page editor
  • In the configuration of your page select, on tab "Input", field "Label field", select the "Custom format (multiple fields)" option
  • In the "Custom page label format", enter "{allergennumber} {title}"
  • Your checkboxes will now show numbers.

Now for the automatic part for new allergens. Let's assign the highest existing number plus one to every newly created page with template allergen. For that, I'll assume that the template is named "allergen". The PHP code goes into site/ready.php and is executed every time before a page is saved:

<?php namespace ProcessWire;

wire()->addHookAfter('Pages::saveReady', function(HookEvent $event) {
  
  $page = $event->arguments(0);
  
  // Adapt the template name to your naming
  if($page->template->name !== 'allergen')
    return;
  
  // Only fill the field allergennumber for new pages
  if(! $page->isNew())
    return;
  
  // Let's get the allergen page with the highest existing number
  $maxNumPage = $pages->findOne('template=allergen, sort=-allergennumber');
  
  // Just in case we're starting from scratch, handle the first allergen page
  $maxNum = ($maxNumPage instanceof NullPage) ? 0 : $maxNumPage->allergennumber;
  
  // Increment by 1 and set the new page's allergen number
  $page->allergennnumber = $maxNum + 1;
  
});
  • Thanks 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...