アクティブテーマをプログラムで変更するにはどうすればよいですか?


20

アクティブなDrupal 8テーマをプログラムで変更するにはどうすればよいですか?

Drupal 6では、次のコードを使用しました。

global $custom_theme;
$custom_theme = 'garland';

Drupal 7では、を使用しましたhook_custom_theme()

Drupal 8では、これを行う正しい方法は何ですか?

回答:


22

Drupal 8では、基本的に特定のタグを使用するサービスであるテーマネゴシエーターを使用します。Drupalが実装するテーマネゴシエーターを参照して、それらがどのように機能するかを正確に理解してください。変更レコードで指定された例は更新されません。

user.services.yml

  theme.negotiator.admin_theme:
    class: Drupal\user\Theme\AdminNegotiator
    arguments: ['@current_user', '@config.factory', '@entity.manager', '@router.admin_context']
    tags:
      - { name: theme_negotiator, priority: -40 }

AdminNegotiator.php

namespace Drupal\user\Theme;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;

/**
 * Sets the active theme on admin pages.
 */
class AdminNegotiator implements ThemeNegotiatorInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $user;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The route admin context to determine whether a route is an admin one.
   *
   * @var \Drupal\Core\Routing\AdminContext
   */
  protected $adminContext;

  /**
   * Creates a new AdminNegotiator instance.
   *
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Routing\AdminContext $admin_context
   *   The route admin context to determine whether the route is an admin one.
   */
  public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, AdminContext $admin_context) {
    $this->user = $user;
    $this->configFactory = $config_factory;
    $this->entityManager = $entity_manager;
    $this->adminContext = $admin_context;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return ($this->entityManager->hasHandler('user_role', 'storage') && $this->user->hasPermission('view the administration theme') && $this->adminContext->isAdminRoute($route_match->getRouteObject()));
  }

  /**
   * {@inheritdoc}
   */
  public function determineActiveTheme(RouteMatchInterface $route_match) {
    return $this->configFactory->get('system.theme')->get('admin');
  }

}

コードの理解は非常に簡単です。applies()メソッドはTRUE、現在のルートが、モジュールがテーマを変更したいルートである場合に戻ります。このdetermineActiveTheme()メソッドは、適用するテーマのテーママシン名を返します。

ThemeNegotiator :: determineActiveTheme()も参照してください。テーマネゴシエーターが使用するメソッドから受け取った引数を変更するために、RouteMatchを渡す必要はありません。そのパッチが適用されている場合は、テーマネゴシエーターコードも変更する必要があります。


上記の例では、applys()をapplys($ route_match)と書くべきではありませんか?リンクされたdoページに同じ質問を投稿しました。ありがとう!
ステファノスペトラキス

@StefanosPetrakis Hmmm ...現在の実装では、その変更レコードの内容に反して、パラメーターとしてそれを取得します。
キアムルノ

Drupalコアが実際にテーマネゴシエーターの1つで使用しているコードを使用して、答えを更新しました。
キアムルノ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.