t("Taxonomy term"),
'keyword' => 'term', // keyword to use for %substitution
'description' => t('Restricts the argument to a taxonomy term.'),
'context' => 'panels_term_context',
'settings form' => 'panels_term_settings_form',
'settings form submit' => 'panels_term_settings_form_submit',
'title function' => 'panels_term_title',
'displays' => 'panels_term_displays',
'choose display' => 'panels_term_choose_display',
);
return $args;
}
/**
* Discover if this argument gives us the term we crave.
*/
function panels_term_context($arg = NULL, $conf = NULL, $empty = FALSE) {
// If unset it wants a generic, unfilled context.
if ($empty) {
return panels_context_create_empty('term');
}
if (!is_numeric($arg)) {
return FALSE;
}
switch ($conf['input_form']) {
case 'tid':
default:
if (!is_numeric($arg) && $conf['input_form'] == 'tid') {
return FALSE;
}
$term = taxonomy_get_term($arg);
break;
case 'term':
$terms = taxonomy_get_term_by_name($arg);
if (count($terms) != 1) {
return FALSE;
}
$term = array_shift($terms);
break;
}
if (empty($term)) {
return FALSE;
}
if (array_filter($conf['vids']) && empty($conf['vids'][$term->vid])) {
return FALSE;
}
return panels_context_create('term', $term);
}
/**
* Settings form for the argument
*/
function panels_term_settings_form($conf) {
if (empty($conf)) {
$conf = array(
'input_form' => 'tid',
);
}
$options = array();
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
}
$form['input_form'] = array(
'#title' => t('Argument type'),
'#type' => 'radios',
'#options' => array('tid' => t('Term ID'), 'term' => t('Term name')),
'#default_value' => $conf['input_form'],
'#prefix' => '
',
'#suffix' => '
',
);
$form['vids'] = array(
'#title' => t('Vocabularies'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $conf['vids'],
'#prefix' => '',
'#suffix' => '
',
);
$form['own_default'] = array(
'#title' => t('Use different default display'),
'#type' => 'checkbox',
'#description' => t('If checked, when this argument is present it will use its own display rather than the default. Displays not selected in the "Own display" field will use this one.'),
'#default_value' => $conf['own_default'],
);
$form['displays'] = array(
'#title' => t('Own display'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $conf['displays'],
'#description' => t('Each checked type will get its own special display to layout its content. Only types set in Node types, above, should be set here. Types not set here will use the default display.'),
'#prefix' => '',
'#suffix' => '
',
);
return $form;
}
/**
* There appears to be a bit of a bug with the way we're handling forms; it causes
* 'checkboxes' to get invalid values added to them when empty. This takes care
* of that.
*/
function panels_term_settings_form_submit(&$values) {
$vocs = taxonomy_get_vocabularies();
if (!empty($values['vids'])) {
foreach ($values['vids'] as $vid => $value) {
if (empty($vocs[$vid])) {
unset($values['vids'][$vid]);
}
}
}
if (!empty($values['displays'])) {
foreach ($values['displays'] as $vid => $value) {
if (empty($vocs[$vid])) {
unset($values['displays'][$vid]);
}
}
}
}
/**
* What additional displays does this argument provide?
*/
function panels_term_displays($conf, $id) {
$displays = array();
if (!empty($conf['own_default'])) {
$displays['default'] = array(
'title' => t('Taxonomy term @id Default', array('@id' => $id)),
'context' => 'term',
);
}
if (is_array($conf['displays'])) {
$options = array();
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
}
foreach (array_keys(array_filter($conf['displays'])) as $vid) {
$displays[$vid] = array(
'title' => t('Taxonomy term @id @vocabulary', array('@id' => $id, '@vocabulary' => $options[$vid])),
// Tell it to base the template for this display off of the default.
'default' => 'default',
'context' => 'term',
);
}
}
return $displays;
}
/**
* Based upon the settings and the context, choose which display to use.
*/
function panels_term_choose_display($conf, $context) {
if (empty($context->data)) {
return;
}
if (!empty($conf['displays'][$context->data->vid])) {
return $context->data->vid;
}
// Please note that 'default' is a special display.
if (!empty($conf['own_default'])) {
return 'default';
}
// Empty return says to use the default display.
return;
}
/**
* Determine the title for substitution with this context
*/
function panels_term_title($context) {
if (isset($context->data->name)) {
return $context->data->name;
}
if (isset($context->page_title)) {
return $context->page_title;
}
}