Write your own method plugin
Your custom method plugin must implements Drupal\purl\Plugin\Purl\Method\MethodInterface
.
The purl
module uses annotation as the plugin discovery method, and expects custom method plugins to exist under the modules/<module name>/src/Plugin/Purl/Method
directory. Implementations must tag themselves using the Drupal\purl\Annotation\PurlMethod
annotation.
Here is an example method plugin:
<?php
namespace App\cookie_monster\Plugin\Purl\Method;
use Drupal\purl\Plugin\Purl\Method\MethodInterface;
use Drupal\purl\Annotation\PurlMethod;
use Symfony\Component\HttpFoundation\Request;
/**
* @PurlMethod(id="fragment")
*/
class FragmentMethod implements MethodInterface
{
public function contains(Request $request, $modifierKey)
{
// A piece of Javascript code sets the `purl_fragment` cookie to a fragment detected in the URL.
if ($request->cookies->get("purl_fragment") === $modifierKey) {
return true;
}
return false;
}
public function enterContext($modifier, $path, array &$options)
{
$options['fragment'] = $modifier;
return $path;
}
public function exitContext($modifier, $path, array &$options)
{
$options['fragment'] = 'no_purl';
return $path;
}
}
The purl
module will consult your custom plugin for each modifiers that is configured to use the fragment
method, and its your plugin's job to respond to the purl
module whether or not a particular modifier is to be considered activated for the current request.
Both MethodInterface#enterContext
and MethodInterface#exitContext
are consulted by Drupal's URL generator during the processing of outbound links. See Generating PURL-aware URLs for more info.
App\purl\Plugin\Purl\Method\RequestAlteringInterface
If your method plugin needs to modify the request object in any way, make it implement this interface. You will need to implement the alterRequest(Request $request, $modifierKey)
method which will be called once all modifiers are checked for. See how the path_prefix
makes a use of this to strip out the modifier key from the prefix so that Drupal can still route request to the correct page (look for purl/src/Plugin/MPurl/Method/PathPrefix.php
)