View source
<?php
$plugin = array(
'single' => TRUE,
'title' => t('Node terms'),
'icon' => 'icon_node.png',
'description' => t('Taxonomy terms of the referenced node.'),
'required context' => new ctools_context_required(t('Node'), 'node'),
'category' => t('Node'),
'defaults' => array(
'vid' => 0,
'term_format' => 'term-links',
'link' => TRUE,
'term_delimiter' => ', ',
),
);
function ctools_node_terms_content_type_render($subtype, $conf, $panel_args, $context) {
if (empty($context) || empty($context->data)) {
return;
}
$node = $context->data;
$query = db_select('taxonomy_index', 't');
$result = $query
->fields('t')
->condition('t.nid', $node->nid)
->execute();
$tids = array();
foreach ($result as $term) {
$tids[] = $term->tid;
}
$term_objects = taxonomy_term_load_multiple($tids);
$terms = array();
if (empty($conf['vid'])) {
foreach ($term_objects as $term) {
$terms['taxonomy_term_' . $term->tid] = array(
'title' => check_plain($term->name),
'href' => 'taxonomy/term/' . $term->tid,
'attributes' => array(
'rel' => 'tag',
'title' => strip_tags($term->description),
),
);
}
}
else {
foreach ($term_objects as $term) {
if ($term->vid == $conf['vid']) {
if ($conf['term_format'] == 'term-links') {
$terms['taxonomy_term_' . $term->tid] = array(
'title' => $term->name,
'href' => 'taxonomy/term/' . $term->tid,
'attributes' => array(
'rel' => 'tag',
'title' => strip_tags($term->description),
),
);
}
elseif (empty($conf['link'])) {
$terms[] = check_plain($term->name);
}
else {
$terms[] = l($term->name, 'taxonomy/term/' . $term->tid, array(
'attributes' => array(
'rel' => 'tag',
'title' => strip_tags($term->description),
),
));
}
}
}
}
$formatted_terms = '';
switch ($conf['term_format']) {
case 'term-links':
drupal_alter('link', $terms, $node);
$formatted_terms = theme('links', array(
'links' => $terms,
));
break;
case 'ul':
$formatted_terms = theme('item_list', array(
'items' => $terms,
));
break;
case 'inline-delimited':
$delimiter = isset($conf['term_delimiter']) ? $conf['term_delimiter'] : ', ';
$processed_terms = array();
foreach ($terms as $key => $term) {
if (is_string($term)) {
$processed_terms[$key] = $term;
}
else {
$terms[$key] = l($term['title'], $term['href'], $term);
}
}
$formatted_terms = implode($delimiter, $processed_terms);
break;
}
$block = new stdClass();
$block->module = 'node_terms';
$block->delta = $node->nid;
$block->title = t('Terms');
$block->content = $formatted_terms;
return $block;
}
function ctools_node_terms_content_type_edit_form($form, &$form_state) {
ctools_include('dependent');
$conf = $form_state['conf'];
$options = array();
$options[0] = t('- All vocabularies -');
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
}
$form['vid'] = array(
'#title' => t('Vocabulary'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $conf['vid'],
'#description' => t('Optionally restrict the terms to a specific vocabulary, or allow terms from all vocabularies.'),
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
);
$form['term_format'] = array(
'#type' => 'radios',
'#title' => t('Term formatting'),
'#options' => array(
'term-links' => t("Taxonomy links (uses theme('links'))"),
'ul' => t('Unordered list'),
'inline-delimited' => t('Inline, delimited'),
),
'#default_value' => $conf['term_format'],
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
);
$form['link'] = array(
'#title' => t('Link to terms'),
'#type' => 'checkbox',
'#default_value' => $conf['link'],
'#description' => t('Check here to make the terms link to the term paths.'),
'#dependency' => array(
'radio:term_format' => array(
'inline-delimited',
'ul',
),
),
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
);
$form['term_delimiter'] = array(
'#type' => 'textfield',
'#title' => t('Term delimiter'),
'#default_value' => $conf['term_delimiter'],
'#size' => 10,
'#dependency' => array(
'radio:term_format' => array(
'inline-delimited',
),
),
);
return $form;
}
function ctools_node_terms_content_type_edit_form_submit($form, &$form_state) {
foreach (array_keys($form_state['plugin']['defaults']) as $key) {
$form_state['conf'][$key] = $form_state['values'][$key];
}
}
function ctools_node_terms_content_type_admin_title($subtype, $conf, $context) {
$placeholders['@s'] = $context->identifier;
if (!empty($conf['vid'])) {
$vocabulary = taxonomy_vocabulary_load($conf['vid']);
$placeholders['@vocabulary'] = $vocabulary->name;
return t('"@s" terms from @vocabulary', $placeholders);
}
return t('"@s" terms', $placeholders);
}