Same filename in other branches
  1. 5.0.x advagg_validator/src/Form/CssLintForm.php
  2. 6.0.x advagg_validator/src/Form/CssLintForm.php
  3. 8.x-2.x advagg_validator/src/Form/CssLintForm.php
  4. 8.x-3.x advagg_validator/src/Form/CssLintForm.php

Namespace

Drupal\advagg_validator\Form

File

advagg_validator/src/Form/CssLintForm.php

View source
<?php

namespace Drupal\advagg_validator\Form;

use Drupal\Core\Form\FormStateInterface;

/**
 * Configure form for CSSHint validation of CSS files.
 */
class CssLintForm extends BaseValidatorForm {
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'advagg_validator_csslint';
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        $form = parent::generateForm('css');
        $rules = [];
        if (file_exists(DRUPAL_ROOT . '/.csslintrc')) {
            $rule_string = file_get_contents(DRUPAL_ROOT . '/.csslintrc');
            if (empty($rule_string)) {
            }
            elseif (substr($rule_string, 0, 1) === '{') {
                $rules = json_decode($rule_string, TRUE);
            }
            elseif (substr($rule_string, 0, 1) === '-') {
                $rules = $this->decodeRules($rule_string);
            }
        }
        $form['#attached']['library'][] = 'advagg_validator/csslint';
        $form['#attached']['drupalSettings']['csslint'] = [
            'rules' => $rules,
        ];
        $form = parent::buildForm($form, $form_state);
        unset($form['actions']);
        return $form;
    }
    
    /**
     * Decode a .csslintrc file to rules array.
     *
     * @param string $rule_string
     *   The raw rule string.
     *
     * @return array
     *   The rules array.
     */
    protected function decodeRules($rule_string) {
        $values = [
            'errors' => 2,
            'warning' => 1,
            'ignore' => 0,
            'exclude-list' => 1,
        ];
        $rules = [];
        $group = '';
        $raw = explode("\n", $rule_string);
        foreach ($raw as $rule) {
            if (empty($rule)) {
                continue;
            }
            if (substr($rule, 0, 1) === '-') {
                $split = explode('=', trim($rule, " \t\n\r\x00\v-,"));
                $group = $split[0];
                $rules[$group] = [];
                if (isset($split[1])) {
                    $rules[$group][$split[1]] = $values[$group];
                }
                continue;
            }
            $rules[$group][trim($rule, " \t\n\r\x00\v,")] = $values[$group];
        }
        return $rules;
    }

}

Classes

Title Deprecated Summary
CssLintForm Configure form for CSSHint validation of CSS files.