While scanning the Drupal codebase for easy tasks I found that the drupal_html_class function is deprecated and has a pretty easy replacement. You can replace nearly all uses of drupal_html_class with \Drupal\Component\Utility's getClass() and in fact drupal_html_class is now a light wrapper around that function (passes the class string directly to the getClass function).

drupal_html_class is a function that ensures clean class names are passed to the rendering system. There may be a few places where it is not needed. That might be an important finding given that it has been reported in the past that this kind of class cleaning mechanism slows down the performance of themes: #680022: template_preprocess() is too slow in generating valid CSS class name

So here's what needs to be done.

1. Find every usage of drupal_html_class and drupal_clean_css_identifier
2. Replace with Html::getClass or Html::cleanCssIdentifier() and ensure the appropriate use statement is present to register the \Drupal\Component\Utility file.
3. Ensure drupal_html_class and drupal_clean_css_identifier has been replaced in Documentation too.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rpayanm’s picture

Title: Replace drupal_html_class() with Html::getClass() » Remove usage of drupal_html_class()
Issue summary: View changes
Issue tags: +@deprecated
Parent issue: » #2205673: [META] Remove all @deprecated functions marked "remove before 8.0"
rpayanm’s picture

Assigned: Unassigned » rpayanm
rpayanm’s picture

Assigned: rpayanm » Unassigned
Status: Active » Needs review
FileSize
20.51 KB
cosmicdreams’s picture

looks that covers everything but the removal of drupal_html_class itself. Let's see if it passes.

cosmicdreams’s picture

+++ b/core/themes/seven/seven.theme
@@ -18,7 +19,7 @@ function seven_preprocess_html(&$variables) {
+        $variables['attributes']['class'][] = Html::getClass('node-form-layout');

When I was looking into doing this, this was the only line that caused me pause.

Do we really want to clean a full string that's already clean?

Do we have this line because we want to promote to other developers that WHATEVER you use for this string (whether it's combined through a series of variables or a string) that it needs be cleaned by some other function?

ianthomas_uk’s picture

Title: Remove usage of drupal_html_class() » Remove usage of drupal_html_class() and drupal_clean_css_identifier()
Issue summary: View changes
Status: Needs review » Needs work

I agree with #5, that is unnecessary, and we have loads of places where we just add a hard-coded string to ['class'].

There are also lots of calls to drupal_clean_css_identifier(), which is basically the same function but without static caching and strtolower. Given the patch is only 20k I think it would be worth doing those too (it's also deprecated).

rpayanm’s picture

Status: Needs work » Needs review
FileSize
11.13 KB
31.02 KB
ianthomas_uk’s picture

FileSize
1.2 KB
32.07 KB

Looks good, there was just one instance that was missed, which I've fixed in the attached patch.

Other than that I'd mark it RTBC, so @rpayanm if you're happy with my change I think you'd be allowed to mark it RTBC (other reviewers welcome too of course).

rpayanm’s picture

Status: Needs review » Reviewed & tested by the community

Ohhh yes! I searched for drupal_html_class( and drupal_clean_css_identifier(. Nice catch. Looks good for me :)

mpdonadio’s picture

I read the patch, and it looks like a good one-to-one replacement. Not changing status, but a few nits

  1. +++ b/core/includes/theme.inc
    @@ -1352,7 +1353,7 @@ function template_preprocess_html(&$variables) {
    +    $variables['attributes']['class'][] = 'path-' . Html::getClass($segment[1]);
       }
    

    Since things are being touched, though, there is some inconsistent usage of the method. Other places have all of the class parts in the method call. Consistent use may be a good idea.

  2. +++ b/core/modules/file/src/Tests/FileFieldWidgetTest.php
    @@ -74,7 +75,7 @@ function testSingleValuedWidget() {
    +      $label = $this->xpath("//label[@for='edit-" . Html::cleanCssIdentifier($field_name) . "-0-upload']");
           $this->assertTrue(isset($label[0]), 'Label for upload found.');
    

    This should probably use the $arguments parameter to ->xpath() and single quotes.

ianthomas_uk’s picture

#10.1 The function takes an unknown string and converts it to a valid class name. We already know 'path-' is a valid (partial) class name, so there's no need to do the conversion on that. The function uses a static cache, so by not passing that partial you may even get a higher cache hit rate. I don't imagine the performance impact is huge, but these are functions that might be called a lot. Either way it's not something I'd personally worry about changing without a good reason.

#10.2 Yeah I guess that's technically better

mpdonadio’s picture

#11-1, I get that. It was more a comment that the usage is inconsistent:

+++ b/core/includes/theme.inc
@@ -1352,7 +1353,7 @@ function template_preprocess_html(&$variables) {
+    $variables['attributes']['class'][] = 'path-' . Html::getClass($segment[1]);
   }

String concatenation outside method invokation.

+++ b/core/modules/block/src/Tests/BlockRenderOrderTest.php
@@ -74,7 +75,7 @@ function testBlockRenderOrder() {
+        $position[$id] = strpos($test_content, Html::getClass('block-' . $test_blocks[$id]['id']));

String concatenation as part of method invokation.

It's just a nit, though; I should have posted both examples in my first comment.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 8: remove_usage_drupal_html_class-2382543-8.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
31.98 KB

Reroll, just one easy conflict resolved:

+++ b/core/modules/block_content/src/BlockContentForm.php
@@ -102,7 +103,7 @@ public function form(array $form, FormStateInterface $form_state) {
-    $form['#attributes']['class'][0] = drupal_html_class('block-' . $block->bundle() . '-form');
+    $form['#attributes']['class'][0] = 'block-' . Html::getClass($block->bundle()) . '-form';
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed fdec77f and pushed to 8.0.x. Thanks!

This issue is a prioritized change (removal of deprecated function use) as per https://www.drupal.org/core/beta-changes and it's benefits outweigh any disruption.

diff --git a/core/themes/seven/seven.theme b/core/themes/seven/seven.theme
index 39149e6..8e78d77 100644
--- a/core/themes/seven/seven.theme
+++ b/core/themes/seven/seven.theme
@@ -5,7 +5,6 @@
  * Functions to support theming in the Seven theme.
  */
 
-use Drupal\Component\Utility\Html;
 use Drupal\Component\Utility\Xss;
 use Drupal\Component\Utility\String;
 use Drupal\Core\Form\FormStateInterface;

This use was not used. Fixed on commit.

  • alexpott committed fdec77f on 8.0.x
    Issue #2382543 by rpayanm, ianthomas_uk: Remove usage of...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.