/** * 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.15 | Darty Form | 編集ページでの操作ミス |
サイトの環境設定 | 7.23 | Pathauto | 自動生成されるURLエイリアスがおかしい |
フォーム | 7.16 | Webform, Mail System, Mime Mail, Jp Mail Helper | 送信メールのサブジェクトが長いと文字化けする |
ユーザ | 7.23 | User | 長い投稿者名の表示が切れる |
テーマ | 8.9.x | patch パッチ | 開発環境のファイル変更を本番環境にパッチでデプロイする方法(画像含まず) |
開発 mail | 7.50 | Mail Safety | 開発/テスト用サイトのメール |