Add additional attribute to non-local links
<a href="{{ link }}"{{ if not link | is_local_url }} rel="noopener" target="_blank"{{ /if }}> | |
{{ label }} | |
</a> |
<?php | |
namespace App\Modifiers; | |
use Illuminate\Support\Str; | |
use Statamic\Modifiers\Modifier; | |
class IsLocalUrl extends Modifier | |
{ | |
/** | |
* Modify a value. | |
* | |
* @param mixed $value The value to be modified | |
* @param array $params Any parameters used in the modifier | |
* @param array $context Contextual values | |
* @return mixed | |
*/ | |
public function index($value, $params, $context) | |
{ | |
if (Str::startsWith($value, 'http')) { | |
return Str::contains($value, request()->root()); | |
} | |
return true; | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Passing null to the You can always cast the value to a string if you prefer: Str::startsWith((string) $value, 'http') |
This comment has been minimized.
This comment has been minimized.
Ah didn't know that - thx for providing that information :-) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
i recommend a null check otherwise that might cause a exception