web-dev-qa-db-ja.com

エンティティのテンプレート候補を追加する方法は?

ノードに1つ追加する方法 のように、カスタムモジュールのエンティティにテンプレートの提案を追加する必要があります。 Entity API モジュールに実装されているものを見てきましたが、エンティティの前処理にフックして自分のエンティティを追加するにはどうすればよいですか?

4
bryanbraun

解決策を見つけました。 Entity APIモジュール template_preprocess_entity() を定義して、カスタムモジュールで独自の前処理中にテンプレートの提案を追加できるようにします。次に例を示します。

function mymodule_preprocess_entity(&$variables) {
    if($variables['elements']['#entity_type'] = "myentity") {
        $variables['theme_hook_suggestions'][] = "myentity";
    }
}

これは、エンティティAPIから構築されたカスタムエンティティの前処理を実行しますが、Drupalコアに組み込まれたノードまたはエンティティの場合は実行しません。Drupalコアエンティティの場合、Drupalコアで提供されるさまざまなエンティティ固有の前処理フックを使用するときに追加する必要があります。次に例を示します。

// Preprocessing for taxonomy terms
function mymodule_preprocess_taxonomy_term(&$variables) {
  // Add template suggestion code here.
}

// Preprocessing for user profiles
function mymodule_preprocess_user_profile(&$variables) {
  // Add template suggestion code here.
}

// Preprocessing for comments
function mymodule_preprocess_comment(&$variables) {
  // Add template suggestion code here.
}

// Preprocessing for nodes
function mymodule_preprocess_node(&$variables) {
  // Add template suggestion code here.
}
5
bryanbraun