/** * 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. | 関連モジュール | タイトル |
---|---|---|---|
コンテンツの管理 | 7,51 | Views Data Export | Views Data ExportでCSVデータが欠けてしまう |
コンテンツの作成 | 6.x | Views | カルーセルのように回転するコンテンツを作りたい |
コンテンツの管理 | 6.x | Views Flag Calendar | 空き室予約のような仕組みは実現できますか? |
ダウンロード | 6.x | Views Bonus Pack | Viewsで表示した内容をCSVダウンロード |
コンテンツの作成 | 6.x | Insert Block | ブロックの内容を本文中に表示するには? |
言語 | 6.x | i18n | 多言語対応サイトのサイト名やスローガン/ミッションなどを多言語化する方法を教えてください |
言語 | 6.22 | URLエイリアスを使った場合に、翻訳したページで同じURLエイリアスが使えないという不具合 |