¿Es posible definir una función hook_preprocess basada en el tipo de entidad como profile2?

8

Recorrí los posibles ganchos usando la función hook_preprocess(&$vars, $hook)y solo la entidad estaba disponible para usar. ¿Es posible hacer algo como hook_preprocess_profile2_entity(), o tengo que escribir una condición if para registrar el tipo de entidad hook_preprocess_entity()?

Adam S
fuente

Respuestas:

15

Esto adapta el patrón de tema Zen para funciones de preproceso de nodo a entidades:

<?php

/**
 * Implements template_preprocess_entity().
 *
 * Runs a entity specific preprocess function, if it exists.
 */
function MYTHEME_preprocess_entity(&$variables, $hook) {
  $function = __FUNCTION__ . '_' . $variables['entity_type'];
  if (function_exists($function)) {
    $function($variables, $hook);
  }
}

/**
 * Profile2 specific implementation of template_preprocess_entity().
 */
function MYTHEME_preprocess_entity_profile2(&$variables, $hook) {
}

/**
 * Field Collection specific implementation of template_preprocess_entity().
 */
function MYTHEME_preprocess_entity_field_collection_item(&$variables, $hook) {
}
tim.plunkett
fuente
Esto es asombroso Siempre olvido que PHP te permite hacer cosas como esta.
mpdonadio
Si desea hacerlo en un tema principal y heredar / anular, tendrá que profundizar en hook_theme. Zen's hook_theme es un buen ejemplo (aunque largo).
Capi Etheriel
¡Estaría agradecido si alguien me mostrara cómo puedo lograr algo similar fuera de la capa de tema!
Volviéndome