drupal-generics/entity_theme_suggestions

为所有类型的实体提供主题建议插件。

8.1.0-alpha1 2017-12-14 13:53 UTC

This package is not auto-updated.

Last update: 2024-10-02 05:09:57 UTC


README

此模块提供了一个插件,可以定义实体的新主题建议。它替代了 hook_theme_suggetions_alter()。模块提供了一个基本建议类 "EntityThemeSuggestionsBase",任何插件都可以扩展它,以便继承最常见的主建议。

用法

在你的模块的 "Plugin/ThemeSuggestions" 目录结构中创建一个类,实现 "EntityThemeSuggestionsInterface" 接口。 "alterSuggestions" 函数应该返回你实体的所有主题建议。

要将你的类标记为实体主题建议插件,你必须添加一个注解

/**
 * @EntityThemeSuggestions(
 *   id = "your_plugin_id",
 *   entityType = "your_entity_type_id",
 *   priority = "the priority of the suggestion"
 * )
 */

插件 ID 是必需的,并且必须是唯一的。实体类型也是必需的。优先级字段是可选的,默认设置为 1。

示例

<?php

namespace Drupal\example\Plugin\ThemeSuggestions;

use Drupal\entity_theme_suggestions\EntityThemeSuggestionsBase;

/**
 * Alters suggestions for user.
 *
 * @EntityThemeSuggestions(
 *   id = "user_theme_suggestions",
 *   entityType = "user"
 * )
 */
class UserThemeSuggestions extends EntityThemeSuggestionsBase {
}

此代码为用户实体创建了模块提供的默认建议。建议在基类中通过 "alterSuggestions()" 方法定义。该方法提供了以下建议

  • [entity_type_id]
  • [entity_type_id]--[view_mode]
  • [entity_type_id]--[bundle]
  • [entity_type_id]--[bundle]--[view_mode]
  • [entity_type_id]--[entity_id]
  • [entity_type_id]--[entity_id]--[view_mode];