You are here

function _contextual_links_to_id in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/contextual/contextual.module \_contextual_links_to_id()

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=bartik.tools:langcode=en

So, expressed in a pattern: <group>:<route parameters>:<metadata>

The route parameters and options are encoded as query strings.

Parameters

array $contextual_links: 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.

3 calls to _contextual_links_to_id()
ContextualLinks::render in core/modules/contextual/src/Plugin/views/field/ContextualLinks.php
Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::render().
ContextualUnitTest::testContextualLinksToId in core/modules/contextual/tests/src/Kernel/ContextualUnitTest.php
Tests _contextual_links_to_id().
contextual_preprocess in core/modules/contextual/contextual.module
Implements hook_preprocess().

File

core/modules/contextual/contextual.module, line 176
Adds contextual links to perform actions related to elements on a page.

Code

function _contextual_links_to_id($contextual_links) {
  $ids = [];
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage(LanguageInterface::TYPE_URL)
    ->getId();
  foreach ($contextual_links as $group => $args) {
    $route_parameters = 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}:{$route_parameters}:{$metadata}";
  }
  return implode('|', $ids);
}