Drupal → Добавить в токен [term:parents:*] текущий термин
Понадобилось в Metatag вывести через запятую иерархию имён термина. Нашёл токен [term:parents:join:, ]
, но проблема в том, что в него не входит текущий термин. Пришлось реализовать свой токен [term:parents-with-current:*]
:
/** * Implements hook_token_info(). */ function MODULENAME_token_info() { $info['tokens']['term']['parents-with-current'] = array( 'name' => t('Parents with current term'), 'description' => t("An array of all the term's parents with current term, starting with the root."), 'type' => 'array', ); return $info; } /** * Implements hook_tokens(). */ function MODULENAME_tokens($type, $tokens, array $data = array(), array $options = array()) { $replacements = array(); if ($type == 'term' && !empty($data['term'])) { $term = $data['term']; foreach ($tokens as $name => $original) { if ($name == 'parents-with-current') { $terms = token_taxonomy_term_load_all_parents($term->tid); $terms[] = entity_label('taxonomy_term', $term); $replacements[$original] = token_render_array($terms, $options); } } if ($parents_tokens = token_find_with_prefix($tokens, 'parents-with-current')) { $terms = token_taxonomy_term_load_all_parents($term->tid); $terms[] = entity_label('taxonomy_term', $term); $replacements += token_generate('array', $parents_tokens, array('array' => $terms), $options); } } return $replacements; }
Оставить комментарий