function Html::getId

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::getId()
  2. 8.9.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::getId()
  3. 10 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::getId()

Prepares a string for use as a valid HTML ID.

Only use this function when you want to intentionally skip the uniqueness guarantee of self::getUniqueId().

Parameters

string $id: The ID to clean.

Return value

string The cleaned ID.

See also

self::getUniqueId()

11 calls to Html::getId()
BigPipeStrategy::generateBigPipePlaceholderId in core/modules/big_pipe/src/Render/Placeholder/BigPipeStrategy.php
Generates a BigPipe placeholder ID.
BlockEntitySettingTrayForm::buildForm in core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
Form constructor.
ConfigureBlockFormBase::doBuildForm in core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php
Builds the form for the block.
ConfigureSectionForm::buildForm in core/modules/layout_builder/src/Form/ConfigureSectionForm.php
Form constructor.
ExposedFormTest::testExposedIdentifier in core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php
Tests the exposed form with a non-standard identifier.

... See full list

File

core/lib/Drupal/Component/Utility/Html.php, line 224

Class

Html
Provides DOMDocument helpers for parsing and serializing HTML strings.

Namespace

Drupal\Component\Utility

Code

public static function getId($id) {
    $id = str_replace([
        ' ',
        '_',
        '[',
        ']',
    ], [
        '-',
        '-',
        '-',
        '',
    ], mb_strtolower($id));
    // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
    // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
    // colons (":"), and periods ("."). We strip out any character not in that
    // list. Note that the CSS spec doesn't allow colons or periods in identifiers
    // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
    // characters as well.
    $id = preg_replace('/[^A-Za-z0-9\\-_]/', '', $id);
    // Removing multiple consecutive hyphens.
    $id = preg_replace('/\\-+/', '-', $id);
    return $id;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.