Browse Source

Implemented a E_USER_WARNING if wrong parameter count in drupalContextualLinks()

merge-requests/44/head
Julian Pustkuchen 2 years ago
parent
commit
937a978354
  1. 12
      src/TwigTweakExtension.php

12
src/TwigTweakExtension.php

@ -419,6 +419,18 @@ class TwigTweakExtension extends AbstractExtension {
public static function drupalContextualLinks(string $id): array { public static function drupalContextualLinks(string $id): array {
$build['#cache']['contexts'] = ['user.permissions']; $build['#cache']['contexts'] = ['user.permissions'];
if (\Drupal::currentUser()->hasPermission('access contextual links')) { 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, ':')
if ($contextParamCount !== 3) {
trigger_error("Wrong {{ drupal_contextual_links() }} parameters. 3 parameters expected, separated by colon (\":\"), but {$contextParamCount} given in: \"{$id}\"", E_USER_WARNING);
}
}
$build['#type'] = 'contextual_links_placeholder'; $build['#type'] = 'contextual_links_placeholder';
$build['#id'] = $id; $build['#id'] = $id;
} }

Loading…
Cancel
Save