1

I have a D8 module that uses the ‘table’ element to which I want to apply a css file residing in the modules-root folder. The module runs well without the css. However when I ‘attached ’ the css I get a PHP error: “LogicException: You are not allowed to use css in #attached.”

The idea is to add classes (e.g. siteOK, ...) to each row of the table as it's built.

So far, all the methods I’ve examined for doing this shows the syntax I've used. Any ideas as to what I’m doing wrong?

../drupal8/modules/Custom/check_website/check_website.css:

    .siteOK{
    background-color:#fcfcfc;   
}

../drupal8/modules/Custom/check_website/src/Form/CheckWebsiteForm.php:

<?php

    /**
     * @file
     * Contains \Drupal\check_website\Form\CheckWebsiteForm.
     */
    namespace Drupal\check_website\Form;

    use Drupal\Core\Form\FormBase;
    use Drupal\Core\Form\FormStateInterface;
    use PDO;

    require 'check_website.config';

    /**
     * Implements an example form.
     */

class CheckWebsiteForm extends FormBase
{

    /**
     * {@inheritdoc}
     */
    public function getFormId()
    {
        $headers = get_headers('http://cto.vermont.gov/');
        return 'check_website';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state)
    {

        $spawnedSites = $this->_getSiteList(); //

        $form['spawnedSites'] = array(
          '#type' => 'table',
          '#caption' => 'Spawned Website Table',
          '#header' => array('Name', 'Title', 'Created By', 'Site ID', 'Palette'),
        );


        $form['spawnedSites']['#attached']['css'][] = array(
          drupal_get_path('module', 'check_website') . '/check_website.css',
        );

        foreach ($spawnedSites as $value)
        {
            $rowColor = $this->_getSiteStatus($value['name']);

            // the ROW level of the table 
            $form['spawnedSites'][$value['siteid']]['#attributes']['class'][] = $rowColor; 

            $form['spawnedSites'][$value['siteid']]['name'] = array(
              '#type' => 'textfield',
              '#title' => t('Name'),
              '#title_display' => 'invisible',
              '#size' => 10,
              '#default_value' => $value['name'],
            );
            $form['spawnedSites'][$value['siteid']]['title'] = array(
              '#type' => 'textfield',
              '#title' => t('Title'),
              '#title_display' => 'invisible',
              '#size' => 10,
              '#default_value' => $value['title'],
            );
            $form['spawnedSites'][$value['siteid']]['createdby'] = array(
              '#type' => 'textfield',
              '#title' => t('Created By'),
              '#title_display' => 'invisible',
              '#size' => 10,
              '#default_value' => $value['createdby'],
            );
            $form['spawnedSites'][$value['siteid']]['siteID'] = array(
              '#type' => 'textfield',
              '#title' => t('Site ID'),
              '#title_display' => 'invisible',
              '#size' => 10,
              '#default_value' => $value['siteid'],
            );
            $form['spawnedSites'][$value['siteid']]['palette'] = array(
              '#type' => 'textfield',
              '#title' => t('Palette'),
              '#title_display' => 'invisible',
              '#size' => 10,
              '#default_value' => $value['palette'],
            );
            $a = 1;
        }



        $form['url'] = array(
          '#type' => 'url',
          '#title' => t('URL:'),
          '#size' => 100,
        );


        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = array(
          '#type' => 'submit',
          '#value' => $this->t('Check'),
          '#button_type' => 'primary',
        );

// /////////////////////////////////////////////////////////////////////////////        
        return $form;
    }

    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state)
    {
        $a = 1;
    }

    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state)
    {
        $urlString = $form_state->getValue('url');
        $headers = get_headers($urlString);

        if (!$headers) {
            drupal_set_message($this->t('No Server at this URL'));
        }
        else {
            $headers0Tokens = explode(" ", $headers[0]);
            switch ($headers0Tokens[1])
            {
                case '200':
                    drupal_set_message($this->t('Server and page found'));
                    break;
                case '204':
                    drupal_set_message($this->t('Server is there but page has no content'));
                    break;
                case '404':
                    drupal_set_message($this->t('Server is there but page not found'));
                    break;
                case '403':
                    drupal_set_message($this->t('Server is there but page forbidden'));
                    break;
            }
        }
    }

    function _getSiteList()
    {
        $pdo = new PDO(sprintf('mysql:host=%s;dbname=%s', COMMANDER_DBHOST, COMMANDER_DB), COMMANDER_DBUSER, COMMANDER_DBPASS);

        $sth = $pdo->prepare('SELECT name, title, createdby, siteid, palette FROM site ORDER BY siteid');
        $site = new \stdClass();
        $sth->bindColumn(1, $site->name);
        $sth->bindColumn(2, $site->title);
        $sth->bindColumn(3, $site->createdby);
        $sth->bindColumn(5, $site->siteid);
        $sth->bindColumn(16, $site->palette);
        $sth->execute();

        $ret = array();

        $ret = $sth->fetchAll(PDO::FETCH_ASSOC);

        return $ret;
    }

    function _getSiteStatus($name)
    {
        $urlString = 'http://' . $name . '.vermont.gov';
        $headers = get_headers($urlString);

        if (!$headers) {
            $rowColor = 'noSerever';
        }
        else {
            $headers0Tokens = explode(" ", $headers[0]);
            switch ($headers0Tokens[1])
            {
                case '200':
                    $rowColor = 'siteOK';
                    break;
                case '204':
                    $rowColor = 'siteNoContent';
                    break;
                case '404':
                    $rowColor = 'siteNoPage';
                    break;
                case '403':
                    $rowColor = 'siteForbidden';
                    break;
            }
        }
        return $rowColor;
    }

}

1 Answer 1

3

In Drupal 8, assets such as CSS and JS are not added directly anymore, but via an asset library.

See the relevant documentation here:

Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module
Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.