$html_attributes = [ 'class' => [ 'my-class', 'some-other-class' ], 'id' => 'my-element' ]; $class_string = \Drupal\Component\Utility\Html::getClass($html_attributes); // Output: 'my-class some-other-class'
$html_attributes = [ 'id' => 'my-div' ]; $class_string = \Drupal\Component\Utility\Html::getClass($html_attributes); // Output: ''In Example 1, we provide an array of HTML attributes to the Html::getClass() method, and it returns the properly formatted CSS class string. In Example 2, we provide an array of HTML attributes that does not include a 'class' attribute, so the method returns an empty string. This method is part of the Drupal\Component\Utility package library.