/** * Implements hook_preprocess_HOOK() for html.html.twig. */ function your_theme_preprocess_html(array &$variables) { $current_path = \Drupal::service('path.current')->getPath(); $alias = explode('?', $_SERVER['REQUEST_URI']); $alias = $alias[0]; // Node type. if (preg_match('!^/node/([0-9]+)!', $current_path, $m)) { $variables['attributes']->addClass("node--$m[1]"); } // Alias. if ($alias) { $aliases = explode('/', $alias); array_shift($aliases); $suggestion = []; foreach ($aliases as $a) { $suggestion[] = $a; $variables['attributes']->addClass(implode('--', $suggestion)); } } }
bodyタグにnode idやaliasのClassを追加する方法
OTHER FAQ
カテゴリ | Core Ver. | 関連モジュール | タイトル |
---|---|---|---|
テーマ | 8.9.x | スマホに向いたテーマについて | |
ブロック | 7.23 | 伸縮するfieldsetブロックを追加したい | |
コンテンツの管理 | 8.9.x | 写真の変更を行いたい | |
Apache2.4 | 7.50 | Apache2.4のアクセス制限 | |
CentOS | 7.50 | 消せないファイルを消すには | |
サイトの環境設定 | 8.9.x | 編集 | |
サイトの環境設定 | 7.15 | トップ(ホーム)ページを変更するには? |