ฉันจะเปลี่ยนชุดรูปแบบที่ใช้งานอยู่โดยทางโปรแกรมได้อย่างไร


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 :: defineActiveTheme () ไม่ควรกำหนดให้ RouteMatch ถูกส่งผ่านเพื่อให้มีการเปลี่ยนแปลงอาร์กิวเมนต์ที่ได้รับจากวิธีการที่ใช้โดยผู้เจรจาต่อรองของธีม หากมีการใช้โปรแกรมปะแก้คุณจะต้องเปลี่ยนรหัสโปรแกรมเปลี่ยนธีมของคุณด้วย


ไม่ควรใช้ () เขียนตามใช้ ($ route_match) ในตัวอย่างด้านบน โพสต์คำถามเดียวกันไปยังหน้าทำเชื่อมโยง ขอบคุณ!
Stefanos Petrakis

@StefanosPetrakis อืม ... การนำไปใช้ในปัจจุบันจะได้รับสิ่งนั้นเป็นพารามิเตอร์ตรงกันข้ามกับสิ่งที่บันทึกการเปลี่ยนแปลงกล่าว
kiamlaluno

ฉันได้อัปเดตคำตอบแล้วการใช้รหัส Drupal core ก็คือการใช้งานจริงในหนึ่งในการต่อรองธีม
kiamlaluno
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.