/** * 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.15 | Entity Translation | 各フィールドのラベルの多言語切り替え漏れ(フィールド・ラベル) |
言語 | 7.15 | Entity Translation | nodeのEntity Translation |
言語 | 7.15 | Language | まだ翻訳されていない英語文、どうにかならない? |
言語 | 7.28 | vim | vimで全角が化けてしまう |
言語 | 7.14 | Entity Translation | 各フィールドのラベルの多言語切り替え漏れ(接頭子、接尾子) |
言語 | 6.22 | URLエイリアスを使った場合に、翻訳したページで同じURLエイリアスが使えないという不具合 | |
開発 mail | 7.50 | Mail Safety | 開発/テスト用サイトのメール |