1

I have installed the Taxonomy Title module 7.x-1.5 in order to change the grey content title on top of certain taxonomy pages. The module added a new field (Term page heading) where I add my custom titles. The title is changed as expected. Now I also need to use that same custom title in certain nodes, so I need to find out how to get the value added in that field.

After a look at the module code, I tried using _taxonomy_title_get_title($tid), which works. Is it safe for me to use this function?

1
  • I think that you must rewrite again your question, after your edit, yes it's safe use this function Feb 6, 2016 at 20:38

1 Answer 1

1

Drupal adds a underscore to its function to mean they are for internal use only, and modules/themes should not depend upon them. For example, in Drupal 8, the theme() function was renamed _theme() as reported in theme() renamed to _theme() and should not be called directly, which also states:

It has been renamed to _theme() and is for internal use only.

Then, it was removed.

Third-party modules follows the same naming convention: If the function name starts with an underscore, they don't want they use that function. This doesn't mean the function is necessarily going to be removed, but the maintainers could remove it at any time, even passing from version 7.x-1.5 to version 7.x-1.6.

So, if by safe you mean sure that the code I am writing will not need any change, then I would say no.
You could eventually write your own code that uses just safe functions made available from that module, using _taxonomy_title_get_title() as starting point; otherwise, you need to check your code is still working when updating that module. Depending on your specific use, you could also use tokens; the module defines [term:title] which is set using the function you are calling.

/**
 * Implements hook_tokens().
 */
function taxonomy_title_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);

  if ($type == 'term' && !empty($data['term'])) {
    $term = $data['term'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'title':
          $title = _taxonomy_title_get_title($term->tid);
          if ($title) {
            $replacements[$original] = ($sanitize) ? check_plain($title) : $title;
          }
          else {
            $replacements[$original] = $term->name;
          }
          break;
      }
    }
  }

  return $replacements;
}

See also Call to undefined function _taxonomy_title_get_title(); it is for the version 6.x of the module, but users are essentially saying that, passing from version 6x-1.3 to 6.x-1.4/6.x-1.5, they get an error about the function _taxonomy_title_get_title() being not defined. (In fact, it was renamed _taxonomy_title_get().) Since the development snapshot doesn't call _taxonomy_title_get_title(), I get they are talking of other code.

3
  • Thanks a lot for this answer! I learn as I go. The token [term:title] is not available among the tokens listed when checking with Devel in my node pages. That's why I tried to come up with an alternative. Would it be safer, in your opinion to use that same db query in my own code, instead of calling the underscore function? Tried to give you un upvote, but it turns out my reputation is not high enough yet. Thanks again!
    – TBJ
    Feb 7, 2016 at 10:43
  • I suggested the token, in the case you were allowing users to set a string your module then use, and you wanted to allow users to use tokens; in this case, you need to tell token_replace() you want tokens for taxonomy terms, as token_replace('[term:title]', array('user' => $user)) would not return the string you are expecting. Yes, using the query that function uses is safer than calling that function.
    – apaderno
    Feb 9, 2016 at 12:50
  • Thanks for the suggestions. I will look into token_replace().
    – TBJ
    Feb 10, 2016 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.