We've got to keep this week's post short because it's the last week of summer break, and actually the last DAY of summer break here in Atlanta. That means children rule the day (and week). After this weekend, the kids are back to school and we get back to a little more consistent work schedule here. I'm also hoping that means the weather will cool off a bit, but not likely!

Like mentioned last week, we're shifting emphasis to getting ProcessWire 3.x and 2.8.x released to replace the current 2.7.x versions. The biggest part of that is just to make sure there are as few issues remaining as possible. So rather than trying to add anything new, we're mostly focused on making sure that what we've got is as stable as possible. So this week's updates are primarily focused on relatively minor tweaks and fixes as a result of GitHub issue reports. You'll see these updates in version 3.0.28 and 2.8.28, both now available for download. For more details about what's in the version, see the commit log. I particularly recommend this upgrade if you are using the FieldtypeTable updates introduced last week, as this version also fixes a couple things there.

These incremental updates are likely to continue for the next couple of weeks while we wrap of this version. If you are running 3.x or 2.8.x, please report any issues you run into via GitHub issue reports to the ProcessWire repository. If not running the latest version of 3.x or 2.8.x, please include what version you are running in any reports. While 2.8.x is in a separate "pw28" repository, we prefer to have issue reports in the ProcessWire repository instead when possible. Thanks to everyone that's using and helping to test ProcessWire 3.x and 2.8.x!

Working with custom utility hooks

Shifting gears, here's a useful little hook function you can add to your /site/ready.php file, and it serves as a good example of how you might add similar utility functions. It gives you a way to generate a text summary of any field on any Page object, within a character limit. Think of it like the description text that you see in Google search results, but for something you'd use in your own site page lists.

First off, here's the usage of it:

// Generate a summary of 'body', max 300 characters (default)
$summary = $page->summarize('body');

// Generate a summary of 'body', max 500 characters
$summary = $page->summarize('body', 500);

// Same as above, but uses 'summary' if available, 'body' if not
$summary = $page->summarize('summary|body', 500); 

Note that while the above usage demonstrates $page, the following hook method actually adds this summarize() methods to all Page objects, meaning you can use it on any Page. Here's the implementation that would be placed in /site/ready.php (or somewhere else if preferred):

$wire->addHook('Page::summarize', function($event) {

  $fieldName = $event->arguments(0);
  if(!$fieldName) throw new WireException("No field provided");

  // get max length or use 300 as default if none provided
  $maxLength = (int) $event->arguments(1);
  if(!$maxLength) $maxLength = 300;

  $page = $event->object;
  $value = $page->get($fieldName);

  if(!strlen($value)) {
    // requested value is blank, nothing more to do
    $event->return = '';
    return;
  }

  // get beginning of value, without any HTML in it (if any)
  $value = mb_substr(strip_tags($value), 0, $maxLength);

  // if output formatting on, make sure value is entity encoded
  if($page->of()) $value = $event->sanitizer->entities1($value);

  if(strlen($value) >= $maxLength) {
    // limit length of returned value between words
    // by truncating to the last space character
    $value = substr($value, 0, strrpos($value, ' '));
    // append an ellipsis to indicate there is more
    $value .= '…';
  }

  $event->return = $value;
});

The above is just a fairly simple example of how you can easily add your own methods to the ProcessWire API. Hope that you all have a great weekend and enjoy the ProcessWire Weekly.


Comments

  • Can

    Can 9 months ago 00

    nice hook, I always had a function for this, but having a dedicated property for this is way cooler :D

    Think my first custom property was Pageimage::cdn as mentioned here https://processwire.com/talk/topic/13471-better-ckeditor-image-insertion-at-least-for-me/

  • Zahari M

    Zahari M 9 months ago 00

    This summarize function will come in incredibly handy for what I need to do.
    Thanks Ryan!

  • Caelan Stewart

    Caelan Stewart 9 months ago 10

    If anybody is interested, here is the version I used for this function. The difference being that mine won't leave things like ",..." or "&..." or " ..." at the end.

    http://pastebin.com/hMg56YjX

  • Caelan Stewart

    Caelan Stewart 9 months ago 10

    Also, I integrated (and revised) my version into the one here, if anybody is interested.

    http://pastebin.com/u53grsrF

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.