function Html::getUniqueId
Same name in other branches
- 9 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::getUniqueId()
- 10 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::getUniqueId()
- 11.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::getUniqueId()
Prepares a string for use as a valid HTML ID and guarantees uniqueness.
This function ensures that each passed HTML ID value only exists once on the page. By tracking the already returned ids, this function enables forms, blocks, and other content to be output multiple times on the same page, without breaking (X)HTML validation.
For already existing IDs, a counter is appended to the ID string. Therefore, JavaScript and CSS code should not rely on any value that was generated by this function and instead should rely on manually added CSS classes or similarly reliable constructs.
Two consecutive hyphens separate the counter from the original ID. To manage uniqueness across multiple Ajax requests on the same page, Ajax requests POST an array of all IDs currently present on the page, which are used to prime this function's cache upon first invocation.
To allow reverse-parsing of IDs submitted via Ajax, any multiple consecutive hyphens in the originally passed $id are replaced with a single hyphen.
Parameters
string $id: The ID to clean.
Return value
string The cleaned ID.
28 calls to Html::getUniqueId()
- ArgumentPluginBase::processContainerRadios in core/
modules/ views/ src/ Plugin/ views/ argument/ ArgumentPluginBase.php - Custom form radios process function.
- BlockTest::testBlockThemeSelector in core/
modules/ block/ tests/ src/ Functional/ BlockTest.php - Tests that the block form has a theme selector when not passed via the URL.
- claro_preprocess_status_messages in core/
themes/ claro/ claro.theme - Implements hook_preprocess_HOOK() for status_messages.
- claro_preprocess_system_themes_page in core/
themes/ claro/ claro.theme - Implements hook_preprocess_HOOK() for system_themes_page.
- CommentForm::form in core/
modules/ comment/ src/ CommentForm.php - Gets the actual form array to be built.
File
-
core/
lib/ Drupal/ Component/ Utility/ Html.php, line 171
Class
- Html
- Provides DOMDocument helpers for parsing and serializing HTML strings.
Namespace
Drupal\Component\UtilityCode
public static function getUniqueId($id) {
// If this is an Ajax request, then content returned by this page request
// will be merged with content already on the base page. The HTML IDs must
// be unique for the fully merged content. Therefore use unique IDs.
if (static::$isAjax) {
return static::getId($id) . '--' . Crypt::randomBytesBase64(8);
}
// @todo Remove all that code once we switch over to random IDs only,
// see https://www.drupal.org/node/1090592.
if (!isset(static::$seenIdsInit)) {
static::$seenIdsInit = [];
}
if (!isset(static::$seenIds)) {
static::$seenIds = static::$seenIdsInit;
}
$id = static::getId($id);
// Ensure IDs are unique by appending a counter after the first occurrence.
// The counter needs to be appended with a delimiter that does not exist in
// the base ID. Requiring a unique delimiter helps ensure that we really do
// return unique IDs and also helps us re-create the $seen_ids array during
// Ajax requests.
if (isset(static::$seenIds[$id])) {
$id = $id . '--' . ++static::$seenIds[$id];
}
else {
static::$seenIds[$id] = 1;
}
return $id;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.