I'll keep today's post short, as this week's updates have been mostly housekeeping related. Last week we had a lot of positive feedback from you about moving to the new ProcessWire account on GitHub, so thank you for your responses!

We've gone ahead and converted that account to be a GitHub organization, and in the weeks ahead we'll be figuring out how best to utilize the new capabilities. Though so far, it looks like GitHub's organization features are very limited in terms of access control at present, so may not give us as much to work with as we'd like. But it's still a lot better than without it, so I can't complain.

To get things started, I've pushed ProcessWire 3.0.33 RC1 to this new organization account in the new ProcessWire repository. This is our way of testing things out. You'll see the same version posted on the devns branch of our old repository, so it doesn't matter where you download it from. But once 3.x is our new official version, then this new repository is where you'll want to get it from.

In terms of actual release date for 3.x, we're going to target September 23, 2016. I'd aim for a week or two earlier, except that I've got to travel a bit between now and then, and I'd like to be fully present upon release, so this date is what works the best. There likely won't be any major code changes until official release, just bug fixes for anything that turns up.

Speaking of code changes, nearly every file in the core code base changed this week, but it was primarily aimed at updating code comments in file headers for version and dates, in preparation for the 3.x release. Beyond that, several updates were made to phpdoc documentation in files, and several minor bugs were fixed per GitHub issue reports. For more details on that see the commit log.


Recipe: hooks and partials

Last week the ProcessWire Weekly started a new recipe of the week, and since this week's post is kind of short I thought I'd also throw one out there I've been experimenting with this week.

Every now and then, I need to include some kind of partial in the middle of a CKEditor field. Something like a subscribe form, photo gallery, PayPal donate button, things of that sort. Basically anything that takes some markup that wouldn't usually work in a rich text field. For example, lets say I've got these files as my partials:

  • /site/templates/includes/subscribe-form.php
  • /site/templates/includes/donate-button.php
  • /site/templates/includes/photo-gallery.php

…and I want to be able to include any of them in my body copy (CKEditor/Textarea) field just by typing "include" and the name of it on it's own line, for example:

include-subscribe-form

Here's a hook you can put in your /site/ready.php file that will let you accomplish this:

$wire->addHookAfter('FieldtypeTextarea::formatValue', function($e) {
  if(strpos($e->return, "<p>include-") !== false) {
    $regex = '/<p>include-([-_a-zA-Z0-9]+)\s*<\/p>/';
    if(preg_match($regex, $e->return, $matches)) {
      $file = $e->wire('config')->paths->templates;
      $file .= "includes/$matches[1].php";
      if(is_file($file)) {
        $out = $e->wire('files')->render($file);
      } else {
        $out = '<p>file does not exist</p>';
      }
      $e->return = str_replace($matches[0], $out, $e->return);
    }
  }
});

You might notice this is kind of like a home brewed Hanna Code. The benefit over Hanna Code in this case is that the file is included in your site's /site/templates/ files, and thus managed along with the rest of the code in your site. Of course there are also benefits to using Hanna Code, but this is just one more tool in your toolbox that is nice when you want all of your code together in one spot.

As another (perhaps simpler) example, lets say that you want to be able to output a Font Awesome icon anywhere that you type "icon-something", i.e.

icon-question-circle Have questions?

We'll assume you have already loaded Font Awesome's CSS file (if not, here's how). And here's how you might automatically convert that "icon-question-circle" to a Font Awesome icon on the front-end (again, in your /site/ready.php file):

$wire->addHookAfter('FieldtypeTextarea::formatValue', function($e) {
  if(strpos($e->return, 'icon-') !== false) {
    if(preg_match('/icon-([-a-z0-9]+)/', $e->return, $matches)) {
      $icon = "<i class='fa fa-$matches[1]'></i>";
      $e->return = str_replace($matches[0], $icon, $e->return);
    }
  }
});

Hope that you all have a great weekend, and be sure to check out the ProcessWire Weekly this weekend!


Comments

  • Szabesz

    Szabesz 6 months ago 01

    Thank you Ryan! I do like this "home brewed Hanna Code" a lot. It could even be extended with parameters like this (for example):
    include-photo-gallery--id_1
    where -- separates the file name from the key/value pair of the parameter.
    It just needs to be implemented first :)

  • Zahari M

    Zahari M 6 months ago 01

    This particular include feature is simply awesome Ryan!!!

    Just curious.. is this site/ready.php a special file? I don't think I have come across it before...

  • ukyo

    ukyo 6 months ago 11

    @ZAHARI M

    You can check this post for ready.php https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/

  • mp

    mp 6 months ago 01

    Cool idea. But where is $value coming from? And what is the diff between $e->wire('config')->paths->templates and wire('config')->paths->templates?

    • ryan

      ryan 6 months ago 00

      You can use $e->wire('config') or wire('config'). Using the wire() method call off of any ProcessWire object (like the HookEvent $e) is preferable in PW3 since it supports multi-instance. Pulling it from $e ensures there's no ambiguity about what instance it is. But if you aren't using multiple instances at once, then it makes no difference. It's more just like a best practice. That $value was a typo, it should be $e->return.

  • Manlio

    Manlio 5 months ago 00

    Hi and thank you for the excellent work! I'm trying to substitute some hanna code script with this fantastic recipe but I'm not able to make it work. I have a multilanguage field so I changed the code from "FieldtypeTextarea::formatValue" to "FieldtypeTextareaLanguage::formatValue".
    I have updated to the latest processwire version and created ready.php file in site directory. Debug is true but I don't see any errors in admin interface. Any idea? Thank you.

    • Manlio

      Manlio 5 months ago 00

      Ok I solved! The text was in side a tag and prevent it from working. Now everything is perfect! Thank you

Post a Comment

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