30 dicembre 2011

Modify limit for taxonomy pager in Drupal!

ENGLISH:

Dear readers of Il Bestiario,

today's post will be written both in English and Italian, because many developers around the world can't sleep at night thanks to this little bug.

 First of all, let me explain you the fact: in Drupal 7, when you access a taxonomy term page you will see a list of nodes which are identified by that specific term. If there are more than 10 nodes (the default limit for a clean installation of Drupal), the results will be paginated by a simple pager.

Ok, this is neat, but what can I do to increase the max number of items for page? Many developers asked this question but the most interesting answer was "This bug will be fixed in Drupal 8". What? Are you joking pal? XD

So! I opened my faithful shell and used my loyal grep in order to discover the lost temple of limit. And obviously I have found the treasure (fear the power of grep!).

You can find clear evidence in "modules/taxonomy/taxonomy.pages.inc", line 44:

if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {


The third parameter of taxonomy_select_nodes is actually the limit that we are searching for.
We know that variable_get returns the value of a configuration variable: so where can we modify it?

After using the grep-inator, the truth descended on this little Earth:

Configuration => Site information => Number of posts on front page


Ok, this is an awful solution by Drupal developers: is there a reason for not assigning to every taxonomy term a nice variable for this aim?
Of course, you can change the taxonomy_term_page function so that you check the existence of the relative custom field and only if it isn't found use the default for the front page.

So, happy patch to everyone (and sorry for my poor English)! XD

ITALIANO:

Cari lettori del Bestiario,

come avete potuto notare, ho voluto scrivere un post bilingue per aiutare più gente possibile con questa bestialata all'interno del codice core di Drupal 7.

Alcuni di voi si saranno imbattuti in una grossa limitazione del sistema: ovvero l'impossibilità di modificare agilmente il numero di risultati per pagina del paginatore della tassonomia. Ovvero, quanti nodi visualizzare per pagina quando si vede l'elenco di un determinato termine di un vocabolario.

Se masticate un po' l'inglese, sapete già dove sta il barbatrucco, ma dato che io stessa, la Compiuta Donzella del 2000, non mi fido della mia capacità di espressione nella lingua dell'oltre Manica, vi riassumo brevemente il tutto!

Vaccata dei programmatori di Drupal: il limite di nodi per pagina della tassonomia viene impostato tramite il numero di post della home page nella configurazione delle informazioni del sito. Questo perché come limit di default della funzione taxonomy_select_nodes all'interno della funzione taxonomy_term_page in "modules/taxonomy/taxonomy.pages.inc" si sfrutta proprio quel parametro.

I casi sono due:
- vi accontentate e modificate il parametro della configurazione, per un massimo di 30 nodi per lista
- non vi accontentate e modificate a manina il tutto (non so ancora bene se si può fare nel tema un overwrite della funzione della tassonomia): la cosa più intelligente da fare è creare un custom field per il vocabolario che volete limitare in modo personalizzato e leggere quel valore all'interno della taxonomy_term_page. Solo nel caso in cui questo valore non sia presente, potete prendere di default quello della conf del sito

Spero di avervi aiutato nello svelare l'arcano mistero!

4 commenti :

Marco ha detto...

<>

Epico!


Felice 2012!

Marco ha detto...

Ma nooo!! Perché?! Risolvi anche questo bug!! Ho scritto
MINOREMINORE fear the power of grep!MAGGIOREMAGGIORE
e il risultato è quello che vedi. No, non dirmi che è un problema di Blogger e che non lo puoi risolvere. Facci vedere le tue abilità sovrumane :)

Compiuta Donzella del 2000 ha detto...

<< fear the power of grep! >>

Hmmm...corretto?

P.S. Buon anno!

Marco ha detto...

This is cheating :D