Etkin temayı programlı olarak nasıl değiştirebilirim?


20

Etkin Drupal 8 temasını program aracılığıyla nasıl değiştirebilirim?

Drupal 6'da aşağıdaki kodu kullandık.

global $custom_theme;
$custom_theme = 'garland';

Drupal 7'de kullandık hook_custom_theme().

Drupal 8'de bunu yapmanın doğru yolu nedir?

Yanıtlar:


22

Drupal 8'de, esasen belirli bir etiketi kullanan hizmetler olan tema müzakerecilerini kullanırsınız. Nasıl çalıştıklarını tam olarak anlamak için Drupal tarafından uygulanan tema müzakerecilerine bakın; değişiklik kaydında verilen örnek güncellenmez.

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');
  }

}

Kodun anlaşılması oldukça kolaydır: Geçerli rota, modülünüzün temayı değiştirmek istediği rota olduğunda applies()döner TRUE; determineActiveTheme()yöntem uygulamak için tema tema makine adını döndürür.

Ayrıca bkz. ThemeNegotiator :: determActiveTheme (), tema müzakerecileri tarafından kullanılan yöntemlerden alınan bağımsız değişkenlerde olası bir değişiklik için bir RouteMatch iletilmesini gerektirmemelidir ; bu yama uygulanırsa tema müzakereci kodunuzu da değiştirmeniz gerekir.


Yukarıdaki örnekte geçerlidir () geçerli olarak yazılmamalı mı ($ route_match)? Aynı soruyu bağlantılı do sayfasına gönderdi. Teşekkürler!
— Stefanos Petrakis

@StefanosPetrakis Hmmm ... Herhangi bir mevcut uygulama, değişiklik kaydının söylediklerinin aksine bunu parametre olarak alıyor.
— kiamlaluno

Drupal core aslında tema müzakerecilerin birinde kullanıyor kodunu kullanarak, cevap güncelledik.
— kiamlaluno
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.