Добавить свой css при создании модуля drupal8

Главные вкладки

Аватар пользователя o-chelovek o-chelovek 29 июля 2016 в 17:41

Создаю, свой модуль который рисует таблицу, и хочу добавить стиль css для этой таблицы:
Пытаюсь сделать как написано в документации:
mymod.info.yml:

name: MyMod
description: 'My first module'
type: module
core: 8.x
libraries:
  - mymod/global-styling
version: 1.0
package: Examples

mymod.libraries.yml:

global-styling:
  version: 8.x
  css:
    theme:
      css/style.css: {}

style.css:

#table_test {
        color: #FF0000;
}

MyModController.php:

<?php
    
namespace Drupal\MyMod\Controller;
    use 
Drupal\Core\Controller\ControllerBase;

    class 

MyModController extends ControllerBase {
      public function 
MyMod() {
        
$output = array();

        

$output['#title'] = 'HelloWorld page title';

        
        

$html '<table id = "table_test">';
        
$html .= '<tr>';
        
$html .= '<th>Столбец 1</th>';
        
$html .= '<th>Столбец 2</th>';
        
$html .= '<th>Столбец 3</th>';
        
$html .= '</tr>';
        
        
        
$j 0;
        for (
$i 0$i 30$i++)
        {
            
$html .= '<tr>';
            
$html .= '<td>'.$j++.'</td>';
            
$html .= '<td>'.$j++.'</td>';
            
$html .= '<td>'.$j++.'</td>';        
            
$html .= '</tr>';            
        }
        
        
        
$html .= '</table>'

        

$output['#markup'] = $html;

        return 

$output;
      }

    }

?>

http://img.imgland.net/jUK0Ptr.png

Но стиль ни как не задается! И при отладке в браузере его нет.
В чем моя ошибка?

Лучший ответ

Аватар пользователя negociant negociant 1 августа 2016 в 22:43
1

Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module
Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme
В модуле библиотеки не подключаются в info файле - в данном конкретном случае либо через #attached к рендерному массиву либо через hook_page_attachments()

MyModController.php:

<?php

namespace Drupal\mymod\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Class MyModController.
 *
 * @package Drupal\mymod\Controller
 */

class MyModController extends ControllerBase {

  /**
   *
   * @return array
   *
   */

  public function myMod() {
    $rows = [];
    $header = [];

    for ($i = 1; $i <= 5; $i++) {
      $header[] = $i;
    }

    $n = 0;
    for ($i = 1; $i <= 5; $i++) {
      $row = [];
      for ($j = 1; $j <= 5; $j++) {
        $row[] = $n++;
      }
      $rows[] = array('data' => $row);
    }

    return [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => [
        'id' => 'table_test',
      ],
      '#attached' => [
        'library' => [
          'mymod/global-styling',
        ],
      ],
    ];
  }

}

mymod.libraries.yml:

global-styling:
  version: 1.x
  css:
    theme:
      css/style.css: {}

mymod.info.yml:

name: MyMod
description: 'My first module'
type: module
core: 8.x
version: 1.0
package: Examples

style.css (в каталоге css в корне модуля):

#table_test {
  color: #FF0000;
}

Комментарии

Аватар пользователя o-chelovek o-chelovek 31 июля 2016 в 9:25

Сделал так:

global-styling:
  version: 8.x
  css:
    css/style_mymod.css: {}

Отчистил кэш, удалил модуль, отчистил кэш установил модуль, и опять отчистил кэш, всеравно безрезультатно.

Аватар пользователя negociant negociant 1 августа 2016 в 22:43
1

Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module
Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme
В модуле библиотеки не подключаются в info файле - в данном конкретном случае либо через #attached к рендерному массиву либо через hook_page_attachments()

MyModController.php:

<?php

namespace Drupal\mymod\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Class MyModController.
 *
 * @package Drupal\mymod\Controller
 */

class MyModController extends ControllerBase {

  /**
   *
   * @return array
   *
   */

  public function myMod() {
    $rows = [];
    $header = [];

    for ($i = 1; $i <= 5; $i++) {
      $header[] = $i;
    }

    $n = 0;
    for ($i = 1; $i <= 5; $i++) {
      $row = [];
      for ($j = 1; $j <= 5; $j++) {
        $row[] = $n++;
      }
      $rows[] = array('data' => $row);
    }

    return [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => [
        'id' => 'table_test',
      ],
      '#attached' => [
        'library' => [
          'mymod/global-styling',
        ],
      ],
    ];
  }

}

mymod.libraries.yml:

global-styling:
  version: 1.x
  css:
    theme:
      css/style.css: {}

mymod.info.yml:

name: MyMod
description: 'My first module'
type: module
core: 8.x
version: 1.0
package: Examples

style.css (в каталоге css в корне модуля):

#table_test {
  color: #FF0000;
}