Puoi chiamare un modello di viste Drupal per più viste?
-
20-08-2019 - |
Domanda
Ho usato il Wizard per la visualizzazione delle viste per generare un modello e mi ha dato il seguente pezzo di codice per andare in template.php.
Preferirei solo mantenere un modello, quindi tutte le mie funzioni chiameranno la stessa, e piuttosto che scrivere numerose versioni della stessa funzione, mi chiedo se c'è un modo per mettere insieme i nomi delle funzioni? Qualcosa del tipo:
function phptemplate_views_view_list_recent_articles($view, $nodes, $type),
phptemplate_views_view_list_popular_articles($view, $nodes, $type) {
$fields = _views_get_fields();
$taken = array();
// Set up the fields in nicely named chunks.
foreach ($view->field as $id => $field) {
$field_name = $field['field'];
if (isset($taken[$field_name])) {
$field_name = $field['queryname'];
}
$taken[$field_name] = true;
$field_names[$id] = $field_name;
}
// Set up some variables that won't change.
$base_vars = array(
'view' => $view,
'view_type' => $type,
);
foreach ($nodes as $i => $node) {
$vars = $base_vars;
$vars['node'] = $node;
$vars['count'] = $i;
$vars['stripe'] = $i % 2 ? 'even' : 'odd';
foreach ($view->field as $id => $field) {
$name = $field_names[$id];
$vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
if (isset($field['label'])) {
$vars[$name . '_label'] = $field['label'];
}
}
$items[] = _phptemplate_callback('views-list-first-with-abstract', $vars);
}
if ($items) {
return theme('item_list', $items);
}
}
Grazie,
Steve
Soluzione
Potresti avere solo i modelli necessari per chiamare una versione della funzione effettiva.
Qualcosa del tipo:
function phptemplate_views_view_list_recent_articles($view, $nodes, $type){
actual_template_function($view, $nodes, $type);
}
function phptemplate_views_view_list_popular_articles($view, $nodes, $type){
actual_template_function($view, $nodes, $type);
}
Altri suggerimenti
Una correzione sopra che mi ha rallentato di 1/2 ora ... è necessario includere un ritorno:
return actual_template_function($view, $nodes, $type);
A parte questo, funziona benissimo.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow