Example linkify module

The following example module demonstrates how to turn text that appears in changelist, job, or code review descriptions, comments, and activity entries into links.

  1. Create a folder called Rickroll in the module folder.

  2. Create the file Module.php within module/Rickroll and edit it to contain:

    <?php
    /**
     * Helix Swarm
     *
     * @copyright   2014 Perforce Software. All rights reserved.
     * @license     Please see LICENSE.txt in top-level folder of this distribution.
     * @version     <release>/<patch>
     */
    
    namespace Rickroll;
    
    use Application\Filter\Linkify;
    
    class Module
    {
      public function onBootstrap()
      {
        Linkify::addCallback(
          function ($value, $escaper) {
            if (strcasecmp($value, 'rickroll')) {
              // not a hit; tell caller we did not handle this one
              return false;
            }
    
            return '<a target="_new" href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">'
                   . $escaper->escapeHtml($value) . "</a>";
          },
          'rickroll',
          strlen('rickroll')
        );
      }
    
      public function getConfig()
      {
        return array();
      }
    }
    

    This file achieves several things. It:

    • makes the Rickroll folder a recognized module.

    • declares the module's namespace, which matches the module's folder name Rickroll.

    • provides an onBootstrap() method that allows the module's configuration to be established immediately after the module is loaded

    • Adds a callback to the Linkify module that declares what text is to be searched for (rickroll) and, when found, how to compose the link for that text.