/** * 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. | 関連モジュール | タイトル |
---|---|---|---|
jQuery Revolution | 7.50 | jQuery Revolutionのリンクがスマホで機能しない | |
テーマ | 8.9.x | スマホに向いたテーマについて | |
PCRE | 7.50 | preg_match()がおかしい | |
テーマ | 7.34 | スマホ、PCおよびアプリを同時に運用可能なおすすめのテーマはありますでしょうか? | |
コンテンツの管理 | 8.9.x | 写真の変更を行いたい | |
コンテンツの作成 | 8.4x | コンテンツ作成時のフォームレイアウトの変更 | |
サイトの環境設定 | 7.15 | トップ(ホーム)ページを変更するには? |