Here is the error message I have:

Strict warning: Only variables should be passed by reference in LpTaxonomy->getAllTid() (line 12 of C:\Program Files\wamp\www\lingopolo\sites\all\modules\lingopolo\custom\lp_taxonomy\lp_taxonomy.module).

My code is as follows:

define('LP_TAXONOMY_RECORDING_TYPE_ALL_NAME', 'All');

class LpTaxonomy {
	
	/**
	 * Returns the taxonomy id of the "All" lesson.
	 */
	function getAllTid() {
		$all_term = array_shift(taxonomy_get_term_by_name(
			LP_TAXONOMY_RECORDING_TYPE_ALL_NAME, 'recording_type'));
		
		return $all_term->tid;
	}
	
}

As far as I can see, I'm passing strings. So why the error message?

Another question on this same code: is this the easiest/best way to avoid hard coding a particular taxonomy id? The problem is that I want to reference a taxonomy item with the term name "All" but I don't want to use the taxonomy id (which is more likely to change). Any better way to do it than the code you see?

Thanks!

Comments

Ayesh’s picture

I'm surprised this was not obvious.
See array_unshift php.net doc: http://php.net/manual/en/function.array-shift.php

In order to pass a parameter that expects it to be by reference, you cannot use return values by other functions.

define('LP_TAXONOMY_RECORDING_TYPE_ALL_NAME', 'All');

class LpTaxonomy {
    
    /**
     * Returns the taxonomy id of the "All" lesson.
     */
    function getAllTid() {
        $term = taxonomy_get_term_by_name(
            LP_TAXONOMY_RECORDING_TYPE_ALL_NAME, 'recording_type');
        $all_term = array_shift($term);
        
        return $all_term->tid;
    }
    
}
drupalshrek’s picture

Thaks Ayesh. OK, I get it now. It makes sense, though it is of course unusual to have to put something into a variable rather than being able to use it directly.

By the way, for your "Drupal bad practices" I would add near the bottom something like "Not protecting a blog page against spammers being able to post whatever they like". ;-)

drupalshrek

wadmiraal’s picture

Ayesh's answer is correct; don't worry, we all went through these kind of silly mistakes ;-).

For your second question, it's tricky. You could create an Admin UI, so the site admin can actually select the "All" term. That's probably how I would tackle it, because it is indeed a bad idea to hard code the term ID (especially if the module must be reused elsewhere) or simply rely on the term name.

Does that help?

Ayesh’s picture

I didn't mean to offend him at all. I see his username frequently, and there are many contributions by him. I'm surprised this silly mistake didn't happen any sooner :)