public static function Html::getUniqueId in Zircon Profile 8
Same name and namespace in other branches
- 8.0 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.
25 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/ src/ Tests/ BlockTest.php - Tests that the block form has a theme selector when not passed via the URL.
- CommentForm::form in core/
modules/ comment/ src/ CommentForm.php - Gets the actual form array to be built.
- Container::processContainer in core/
lib/ Drupal/ Core/ Render/ Element/ Container.php - Processes a container element.
- DialogRenderer::determineTargetSelector in core/
lib/ Drupal/ Core/ Render/ MainContent/ DialogRenderer.php - Determine the target selector for the OpenDialogCommand.
File
- core/
lib/ Drupal/ Component/ Utility/ Html.php, line 154 - Contains \Drupal\Component\Utility\Html.
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 = array();
}
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;
}