unlcms / project-herbie

Drupal 10 implementation at the University of Nebraska–Lincoln
https://cms.unl.edu
GNU General Public License v2.0
5 stars 6 forks source link

Patch Views to allow @ in class names #69

Closed ericras closed 4 years ago

ericras commented 4 years ago

Adding a CSS class of "dcf-grid-halves@md" to a view gives:

CSS classes must be alphanumeric or dashes only.

Adding @ won't hurt anything in /web/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php

  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    $section = $form_state->get('section');
    switch ($section) {
      case 'display_title':
        if ($form_state->isValueEmpty('display_title')) {
          $form_state->setError($form['display_title'], $this->t('Display title may not be empty.'));
        }
        break;
      case 'css_class':
        $css_class = $form_state->getValue('css_class');
        if (preg_match('/[^a-zA-Z0-9-_@ ]/', $css_class)) {
          $form_state->setError($form['css_class'], $this->t('CSS classes must be alphanumeric or dashes only.'));
        }
        break;
macburgee1 commented 4 years ago

It looks like we have two options:

  1. Extend the DisplayPluginBase class, or
  2. Patch DisplayPluginBase and submit an issue on Drupal.org
macburgee1 commented 4 years ago

Option 1 isn't possible.

The "correct" way to solve this would be to 1) modify Drupal\views\Plugin\views\display\DisplayPluginBase::validateOptionsForm to check css classes with \Drupal\Component\Utility\Html::cleanCssIdentifier, which 2) in turn would need to be modified to accept escaped unicode characters. There's an issue on Drupal.org for the second issue:

Html::cleanCssIdentifier Strips Valid Escaped Characters

This change is supported by the official CSS specs: https://www.w3.org/TR/CSS21/syndata.html#characters.

Tests would need to be updated, and then both changes would need to be committed to Drupal core.

A much easier alternative would be to simply patch Drupal\views\Plugin\views\display\DisplayPluginBase::validateOptionsForm with a local patch.

The issue with \Drupal\Component\Utility\Html::cleanCssIdentifier may continue to cause problems with DCF, so we may be forced to revisit this.

Patch solution is coming.

ericras commented 4 years ago

Third option:

  1. Just put '@' in there and don't tell anyone
ericras commented 4 years ago

Yeah, so I only read option 1 and then repeated option 2. Whatever