function ContextualLinksSerializer::linksToId
Serializes #contextual_links property value array to a string.
Examples:
- node:node=1:langcode=en
- views_ui_edit:view=frontpage:location=page&view_name=frontpage&view_display_id=page_1&langcode=en
- menu:menu=tools:langcode=en|block:block=olivero.tools:langcode=en
So, expressed in a pattern: <group>:<route parameters>:<metadata>
The route parameters and options are encoded as query strings.
Parameters
array $contextualLinks: The $element['#contextual_links'] value for some render element.
Return value
string A serialized representation of a #contextual_links property value array for use in a data-* attribute.
File
-
core/
modules/ contextual/ src/ ContextualLinksSerializer.php, line 41
Class
- ContextualLinksSerializer
- Helper methods to handle contextual links <-> ID conversion.
Namespace
Drupal\contextualCode
public function linksToId(array $contextualLinks) : string {
$ids = [];
$langcode = $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_URL)
->getId();
foreach ($contextualLinks as $group => $args) {
$routeParameters = UrlHelper::buildQuery($args['route_parameters']);
$args += [
'metadata' => [],
];
// Add the current URL language to metadata so a different ID will be
// computed when URLs vary by language. This allows to store different
// language-aware contextual links on the client side.
$args['metadata'] += [
'langcode' => $langcode,
];
$metadata = UrlHelper::buildQuery($args['metadata']);
$ids[] = "{$group}:{$routeParameters}:{$metadata}";
}
return implode('|', $ids);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.