Jump to content
celfred

Sort page reference field

Recommended Posts

Hello,

I'm facing an issue with my page fieldtype. Pages appear in the order they were created although I'd like to see them sorted in alphabetical order (by title). The pages are well-sorted in my tree, but in that particular Page fieldtype (on another page), I can't find any place where I would decide on what field I can sort them. Am I missing something here ?

Let me know if I'm not clear enough in my explanations.

Thanks in advance for a clue ;) 

Share this post


Link to post
Share on other sites

Use the "Custom find" or "Selector string" options to define the selectable pages for the field, and specify a "sort" in the selector, e.g. "sort=title".

  • Like 3

Share this post


Link to post
Share on other sites

Is it also possible to sort the selectable pages in a template in the admin interface? 

I have hundreds of pages that are 'works'. To add a list of which works were in an exhibition I have a 'works-id' page field in my 'exhibition' template where I can add them just by typing the title. After a while it becomes difficult to see if one is already in the list or not. Sort by name would be a great help.

On the website they are sorted (chronological) in the template code: http://www.amvk.be/exhibitions/solo-exhibitions/amvk/more

sortable_works_id_list.png

Share this post


Link to post
Share on other sites
7 hours ago, BFD Calendar said:

Is it also possible to sort the selectable pages in a template in the admin interface? 

You can sort them in a saveReady hook. In /site/ready.php:

$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    // If the page has the relevant template...
    if($page->template == 'exhibition') {
        // Sort the Page Reference field by title
        $page->works_id->sort('title') ;
    }
});

This of course prevents you from applying a manual sort to the field, and does affect the sort order of the field value when you get it in your template. But it sounds like you are specifying a different sort (chronological) there anyway so that won't be an issue.

  • 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 louisstephens
      I have done a bit of searching, but I can not seem to find an actual answer. I have a list of services as child pages under "Services". I can output the services just fine, but I cant wrap my head around how to group them "alphabetically" like:
      Services A - Service "A" 1 - Service "A" 2 - Service "A" 3 B - Service "B" 1 - Service "B" 2 - Service "B" 3 C - Service "C" 1 - Service "C" 2 - Service "C" 3 Has anyone achieved this type of functionality before?
    • By nickngqs
      How do you guys sort according to the order of page listing?

      My template is sort order none, so I assume it would return according to order of page listing.
    • By rolisx
      Hi all!
      I have a little problem here. I want to sort a list of addresses by streetname and then by number. First, I just had a field "prod_objekt" (address) containing both and then have a foreach loop like this: $pages->get('/produzenten/')->prod_repeater->sort('prod_objekt') as $produzent. Unfortunately, this would not sort by streetnumber if I had the same streetname but multiple numbers.
      Now, I thought I could sort by two fields. prod_object (adress) first and then by prod_hausnummer (streetnumber). But I just don't know the code for it. I tried this: $pages->get('/produzenten/')->prod_repeater->sort('prod_objekt.prod_hausnummer') as $produzent, but it wouldn't work.
      Any ideas on how to solve this?
      Thanks for your help
      Roli
    • By Vigilante
      This is a pretty typical thing. Open a page, it does a $pages query, finds 112 things and I list them on the page. There is pagination too, 20 per page.
      I want a sort dropdown box so the visitor can change alpha sort or whatever other sorts and filters I eventually use.
      My first thought is to just refresh the page with a url query like "page/?sort=az" and then adjust the query in the template code. But to do this, it will require a page refresh, which means the sort box goes back to its default, and not sure how it effects pagination.
      If I do a page refresh, I'll have to keep the sort settings in the session or something, to make sure I always apply correct sort on page load.
      But at the same time, I've already got the $pages array, it would be much nicer to shuffle it and update the page without a page refresh, rather than run the query over and over again.
      But then again, the pagination buttons already cause page refreshes, so that is already happening anyway, unless I convert the pagination buttons to also be no-refresh somehow.
       
      I figure this is a very common problem, to paginate results and give users a sorting/filtering option. What is the common methods for handling it? Session, url query, ajax? If I just add the sort to the url query, how do I make that work in the pagination links?
    • By Moebius
      I need to sort results by title ascending, and title includes special characters (croatian letters).
      The problem is that results are sorted but not 100% correct:
      e.g.
      $authors = $pages->find("template=author,sort=title"); Cavali Cavilon Čavlović Cesare ... You see intruder (bolded), it looks like sort engine treats letters C and Č as same ones.
      I'm not sure that this is Processwire related, but maybe API has solution for this kind of behavior?
      I have UTF-8 page content type .
       
       
       
×
×
  • Create New...