Создаю, свой модуль который рисует таблицу, и хочу добавить стиль css для этой таблицы:
Пытаюсь сделать как написано в документации:
mymod.info.yml:
description: 'My first module'
type: module
core: 8.x
libraries:
- mymod/global-styling
version: 1.0
package: Examples
mymod.libraries.yml:
style.css:
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;
}
}
?>
Но стиль ни как не задается! И при отладке в браузере его нет.
В чем моя ошибка?
Комментарии
css:
css/myStyle.css: {}
Сделал так:
version: 8.x
css:
css/style_mymod.css: {}
Отчистил кэш, удалил модуль, отчистил кэш установил модуль, и опять отчистил кэш, всеравно безрезультатно.
попробуйте переименовать модуль и включить
Не помогло
отключите кэширование так https://www.drupal.org/node/2598914
Не помогло
Думаю дело не в кешах, просто надо внимательно построить модуль, советую посмотреть https://www.drupal.org/project/examples
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:
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:
version: 1.x
css:
theme:
css/style.css: {}
mymod.info.yml:
description: 'My first module'
type: module
core: 8.x
version: 1.0
package: Examples
style.css (в каталоге css в корне модуля):
color: #FF0000;
}