Preprocesar variables solo para ciertos bloques

11

¿Es posible preprocesar variables solo para ciertos bloques? He creado dicha función: mytheme_preprocess_block__aggregator(&$vars)pero no funciona.

- EDITAR -

Parece estar solucionado en Drupal 8 https://drupal.org/node/1751194

ya.teck
fuente
La edición que hiciste y el enlace que pegaste allí solo parece tratar con sugerencias de gancho de tema, ¿no?
leymannx

Respuestas:

20

Desafortunadamente, no hay forma de hacerlo así (similar a hook_form_alter ()).

La mejor manera de hacer esto sería usar $ variables ['bloque'] -> oferta para aplicar modificaciones solo a los bloques que desee:

function mytheme_preprocess_block(&$variables) {
  if ($variables['block']->bid === 'target_block_id') {
    // do something for this block
  } else if ($variables['block']->bid === 'other_target_block_id') {
    // do something else for this other block
  }
}
Alex Weber
fuente
44
Miré el código de Drupal, y Drupal no busca hook_preprocess_block_MODULE()o THEME_preprocess_block_MODULE(). Maneja de una manera particular los __contenidos en los nombres de funciones del tema, cuando theme()se invoca como theme('links__contextual__node', ...), por ejemplo.
kiamlaluno
Hmm, es bueno saberlo!
Alex Weber
'block_id' suele ser el nombre de máquina del bloque correcto?
chrisjlee
2
Utilice $variables['block']->bidy no $variables['block_id']como 'block_id' no es exclusivo de ese bloque.
Duncanmoo
3
Creo que es un poco más amigable encender condicionalmente $variables['block']->deltasi$variables['block']->module == 'MODULE'
Pete
3

Solo para confirmar, en Drupal 8 puede escribir funciones de preproceso para bloques específicos. Por ejemplo:

Drupal 8

mytheme_preprocess_block__system_branding_block(&$vars) {
  // Make changes to the the system branding block
}

Pero también podría usar hook_preprocess_block y la ID del complemento:

function mytheme_preprocess_block(&$vars) {
  if ($vars['plugin_id'] == 'system_branding_block') {
    // Make changes to the the system branding block
  }
}

Como mencionó Alex, en Drupal 7 tendrá que seguir con HOOK_preprocess_block y una verificación de identificación:

Drupal 7

mytheme_preprocess_block(&$vars) {
  if ($vars['block']->bid === 'target_block_id') {
    // make changes to this block
  }
}
Bryanbraun
fuente
Lo probé ahora mismo en D8, ni MYTHEME_preprocess_block__system_branding_block(&$vars)funciona ni funciona MYTHEME_preprocess_block__page_title_block(&$variables).
leymannx
2
mytheme_preprocess_block__{my_block_machine_name}(&$variables)funciona en D8.3
Tim