/** * 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 | Display sweet, Views, Twig Tweak, Views field formatter | テーマに頼らず コンテンツのフィールドとしてViewsを埋め込み表示する方法 |
サイトの構築 | 8.9.x | Webform | drupalを使用した登録、一覧表示の方法について |
アップデート | 8.9.x | Composer update | composer update するときに パッチが必要なモジュールが更新されパッチが消えてしまうことへ対応する方法 |
サイト情報 | 8.9.x | api | Drupal::request() メモ |
コンテンツの管理 | 8.9.x | 写真の変更を行いたい | |
フォーム | 8.9.x | api | GET/POSTされた情報を得る方法 |
Migrate | 9.x | drush config | Upgrade source(環境) を 変更したときにやるべきこと |