Browse Source

Merge branch '3423794-pre-check-contextual-link' into '3.x'

Implemented a E_USER_WARNING if wrong parameter count in drupalContextualLinks()

See merge request project/twig_tweak!44
merge-requests/44/merge
Julian Pustkuchen 2 years ago
parent
commit
69b2704c50
  1. 12
      src/TwigTweakExtension.php

12
src/TwigTweakExtension.php

@ -419,6 +419,18 @@ class TwigTweakExtension extends AbstractExtension {
public static function drupalContextualLinks(string $id): array {
$build['#cache']['contexts'] = ['user.permissions'];
if (\Drupal::currentUser()->hasPermission('access contextual links')) {
// Pre-check the format of $id, as expected by Drupal core without further checks:
$contexts = explode('|', $id);
foreach ($contexts as $context) {
// Drupal core expects exactly three parameters:
// [$group, $route_parameters_raw, $metadata_raw] = explode(':', $context);
// in _contextual_id_to_links($id):
$contextParamCount = substr_count($context, ':')+1;
if ($contextParamCount !== 3) {
trigger_error("Wrong {{ drupal_contextual_links() }} parameter given. 3 parameters expected separated by colon (\":\"), but {$contextParamCount} given in \"{$id}\"", E_USER_WARNING);
}
}
$build['#type'] = 'contextual_links_placeholder';
$build['#id'] = $id;
}

Loading…
Cancel
Save