Jump to content

Title field generated by PHP code


Pete
 Share

Recommended Posts

I've been using the option "Custom PHP code to find selectable pages" today and didn't realise quite how powerful that was (a real "D'oh!" moment there!).

Something I do find reasinably often is that I want to display custom page titles in the page tree more and more for certain projects and, whilst concatenation is nice, it would be extremely useful to be able to use something similar to generate the titles.

For example, in one case I've got some pages that have categories attached to them whilst the pages themselves are simply named 1-infinity, but I'd like to pad the number with zeros. It would be great to be able to create a title based off something like this:

return $page->category->prefix . str_pad($page->name, 4, 0, STR_PAD_LEFT) . $page->title;

There are other more complicated examples with IF ...  ELSE where it checks something from the parent page if a value in the current page is set, but this one is a relatively simple example to post.
 

Is this possible, or could it potentially introduce too much overhead in the page tree?

  • Like 1
Link to comment
Share on other sites

I think you could modify that PageList label by hooking in after ProcessPageListRenderJSON::getPageLabel. Argument 0 to the method is the $page that you want to get the label from, and the method simply returns the label that will be printed. Here it is if you'd like to take a closer look. 

  • Like 2
Link to comment
Share on other sites

I'm using this feature quite a lot. 

Here's an example of creating a indented list for using with checkboxes, selects. IT creates a new PageArray and modifies the "title" to indent, and here with a (count) used in the context of the categories to see how many products are under that category.

$children = $pages->get("/shop/")->children();
$list = new PageArray();

function mylist($children, $ind='', $arr){
  $ind .= "– ";
  foreach($children as $cat) {
    $countproducts = wire("pages")->find("template=product,categories=$cat")->count();
    $cat->title = $ind . $cat->title . " ($countproducts)";
    $arr->add($cat);
    if($cat->numChildren) {
      mylist($cat->children,$ind, $arr);
    }
  }
  return $arr;
}

return mylist($children, $ind='',$list);

This will produce something like

  •  –– Gewürze (0)
  •  –– –– Kräuter (27)
  •  –– –– Kräutermischungen (7)
  •  –– –– Einzelgewürze (0)
  •  –– –– –– Anis (2)
  •  –– –– –– Blaumohnsaat (1)
  •  –– –– –– Bockshornsamen (2)
  •  –– –– –– Chili (16)
  •  –– –– –– Glutamat (1)
  •  –– –– –– Galgantwurzel (3)
  •  –– –– –– Ingwer (4)
  •  –– –– –– Kardamom (4)
  •  –– –– –– Knoblauch (5)
  •  –– –– –– Koriander (2)
  •  –– –– –– Kurkuma (2)
  • Like 7
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...