Roych Posted January 17, 2023 Share Posted January 17, 2023 Hello, I was wondering if it's possible to add numbers in front of a title in backend in page reference field: #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 More sharing options...
BitPoet Posted January 17, 2023 Share Posted January 17, 2023 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 More sharing options...
Roych Posted January 17, 2023 Author Share Posted January 17, 2023 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 More sharing options...
zoeck Posted January 17, 2023 Share Posted January 17, 2023 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. 1 Link to comment Share on other sites More sharing options...
BitPoet Posted January 17, 2023 Share Posted January 17, 2023 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; }); 1 Link to comment Share on other sites More sharing options...
Roych Posted January 17, 2023 Author Share Posted January 17, 2023 Nice, took the first one, so I can have full control. Works perfect. Didn't know about "custom label field option" Will help alot in the future. Thank you very much R Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now