View source
<?php
function taxonomy_display_associated_display_handler_views_callback($form, $form_state) {
$field = taxonomy_display_form_fieldset($form, 'associated');
if ($field) {
return $field['display'];
}
}
class TaxonomyDisplayAssociatedDisplayHandlerViews extends TaxonomyDisplayAssociatedDisplayHandler {
public function displayAssociated($term, $options = NULL) {
module_load_include('module', 'views');
$build = array();
$view = views_get_view($options['view']);
if (!$view || !isset($view->display[$options['display']])) {
watchdog('taxonomy_display', 'The view and/or view display for %vocab is missing, go to the <a href="!link">full display page</a> and reconfigure the taxonomy term\'s associated content display.', array(
'%vocab' => $term->vocabulary_machine_name,
'!link' => url('admin/structure/taxonomy/' . $term->vocabulary_machine_name . '/display/full'),
), WATCHDOG_ERROR);
}
elseif ($view
->access($options['display'])) {
$view->override_path = current_path();
$output = $view
->preview($options['display'], array(
$term->tid,
));
if ($output) {
$build['view'] = array(
'#markup' => $output,
);
}
if ($options['use_view_title'] && $view->build_info['title']) {
drupal_set_title(strtr($view->build_info['title'], $view->build_info['substitutions']), PASS_THROUGH);
}
}
return $build;
}
public function formFieldset(&$form, &$values, $options = NULL) {
module_load_include('module', 'views');
$form['#description'] = t('Use <em>Views</em> for displaying associated content.');
$views = views_get_all_views();
$select_options = array();
foreach ($views as $view) {
if (views_view_is_enabled($view)) {
$select_options[$view->name] = empty($view->human_name) ? $view->name : $view->human_name;
}
}
$form['view'] = array(
'#ajax' => array(
'callback' => 'taxonomy_display_associated_display_handler_views_callback',
'wrapper' => 'replace-td-views-display-field',
),
'#default_value' => isset($options['view']) ? $options['view'] : FALSE,
'#description' => t('Select which view you would like to display the associated content.'),
'#options' => $select_options,
'#title' => t('View'),
'#type' => 'select',
);
if (isset($values['view'])) {
$view = $views[$values['view']];
}
elseif (isset($options['view'])) {
$view = $views[$options['view']];
}
else {
reset($select_options);
$view = $views[key($select_options)];
}
$select_options = array();
foreach ($view->display as $key => $display) {
$select_options[$key] = $display->display_title;
}
$form['display'] = array(
'#default_value' => isset($options['display']) ? $options['display'] : 'default',
'#description' => t('The display selected will have the taxonomy term ID supplied as the first argument.'),
'#options' => $select_options,
'#prefix' => '<div id="replace-td-views-display-field">',
'#suffix' => '</div>',
'#title' => t("View's display"),
'#type' => 'select',
);
$form['use_view_title'] = array(
'#default_value' => isset($options['use_view_title']) ? $options['use_view_title'] : 0,
'#return_value' => 1,
'#description' => t('If checked, the page title will be inherited from the view\'s display page title, instead of using the term name.'),
'#prefix' => '<div id="replace-td-views-use_view_title-field">',
'#suffix' => '</div>',
'#title' => t("Set page title the same as view display title"),
'#type' => 'checkbox',
);
}
public function formSubmit($form, &$values) {
return $values;
}
}