如何以编程方式更改活动主题?


20

如何以编程方式更改活动的Drupal 8主题?

在Drupal 6中,我们使用了以下代码。

global $custom_theme;
$custom_theme = 'garland';

在Drupal 7中,我们使用hook_custom_theme()

在Drupal 8中,正确的方法是什么?

Answers:


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来更改从主题协商程序使用的方法接收的参数;如果应用了该补丁,则还需要更改主题协商程序代码。


在上面的示例中,apply()是否不应该写成apply($ route_match)?将相同的问题发布到链接的“执行”页面。谢谢!
Stefanos Petrakis '16

@StefanosPetrakis Hmmm ...当前任何实现都将其作为参数,与更改记录所说的相反。
kiamlaluno

我已经更新了答案,使用了Drupal核心实际上在其主题谈判者之一中使用的代码。
kiamlaluno
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.