View source
<?php
function views_attach_views_api() {
return array(
'api' => 2.0,
);
}
function views_attach_content_extra_fields($type_name) {
$extras = array();
$items = array();
$views = views_attach_get_node_views($type_name);
foreach ($views as $mode => $info) {
foreach ($info as $entry) {
$items[] = $entry;
}
}
foreach ($items as $item) {
if (!$item['title']) {
$item['title'] = t('Unnamed attached view');
}
$extras[$item['name'] . '_' . $item['display']] = array(
'label' => $item['title'],
'description' => t('Attached view.'),
'weight' => 10,
);
}
return $extras;
}
function views_attach_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'view':
$views = views_attach_get_user_views($category);
foreach ($views as $info) {
$view = views_get_view($info['name']);
$view
->set_display($info['display']);
$args = $view->display_handler
->get_option('default_argument') === 'uid' ? array(
$account->uid,
) : array();
$result = $view
->execute_display($info['display'], $args);
if (!empty($result)) {
$account->content[$view->name . '_' . $info['display']] = array(
'#type' => 'user_profile_category',
'#weight' => $view->display_handler
->get_option('weight'),
'#title' => $view
->get_title(),
);
$account->content[$view->name . '_' . $info['display']]['content'] = array(
'#type' => 'markup',
'#value' => $result,
);
}
}
}
}
function views_attach_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'view':
if (isset($node->build_mode)) {
$mode = $node->build_mode;
if ($mode === NODE_BUILD_NORMAL) {
$mode = $teaser ? 'teaser' : 'full';
}
}
else {
$mode = 'full';
}
$views = views_attach_get_node_views($node->type, $mode);
foreach ($views as $info) {
$view = views_get_view($info['name']);
$view
->set_display($info['display']);
$args = $view->display_handler
->get_option('default_argument') === 'nid' ? array(
$node->nid,
) : array();
$result = $view
->execute_display($info['display'], $args);
if (!empty($result)) {
$node->content[$view->name . '_' . $info['display']] = array(
'#weight' => module_exists('content') ? content_extra_field_weight($node->type, $view->name . '_' . $info['display']) : 10,
'#value' => $result,
);
}
}
break;
}
}
function views_attach_get_user_views($category) {
static $used_views = array();
if (is_null($category)) {
$category = 'Default';
}
if (empty($used_views)) {
views_include('cache');
$cache = views_cache_get("views_attach:profile");
if (isset($cache->data)) {
$used_views = $cache->data;
}
else {
$views = views_get_applicable_views('uses hook user');
foreach ($views as $data) {
list($view, $display_id) = $data;
$view_category = $view->display_handler
->get_option('category');
if (empty($view_category)) {
$view_category = 'Default';
}
$used_views[$view_category][] = array(
'name' => $view->name,
'display' => $display_id,
);
$view
->destroy();
}
views_cache_set("views_attach:profile", $used_views);
}
}
return isset($used_views[$category]) ? $used_views[$category] : array();
}
function views_attach_get_node_views($type, $mode = NULL) {
static $used_views = array();
if (empty($used_views)) {
views_include('cache');
$cache = views_cache_get("views_attach:nodes");
if (isset($cache->data)) {
$used_views = $cache->data;
}
else {
$views = views_get_applicable_views('uses hook nodeapi');
foreach ($views as $data) {
list($view, $display_id) = $data;
foreach ($view->display_handler
->get_option('types') as $type_to_use) {
$modes = $view->display_handler
->get_option('modes');
foreach ($modes as $value) {
$used_views[$type_to_use][$value][] = array(
'name' => $view->name,
'display' => $display_id,
'title' => $view
->get_title(),
);
}
}
$view
->destroy();
}
views_cache_set("views_attach:nodes", $used_views);
}
}
if (is_null($mode)) {
return isset($used_views[$type]) ? $used_views[$type] : array();
}
else {
return isset($used_views[$type][$mode]) ? $used_views[$type][$mode] : array();
}
}
function views_attach_build_modes() {
$modes = array();
if (module_exists('content')) {
foreach (content_build_modes() as $mode => $value) {
$modes[$mode] = $value['title'];
}
}
else {
$modes = array(
'full' => 'Full node',
'teaser' => 'Teaser',
);
}
$modes['edit'] = 'Edit page';
return $modes;
}
function views_attach_form_alter(&$form, $form_state, $form_id) {
if ($form['#id'] == 'node-form') {
$views = views_attach_get_node_views($form['type']['#value'], 'edit');
foreach ($views as $info) {
$view = views_get_view($info['name']);
$view
->set_display($info['display']);
$args = $view->display_handler
->get_option('default_argument') === 'nid' ? array(
$node->nid,
) : array();
$result = $view
->execute_display($info['display'], $args);
if (!empty($result)) {
$form[$view->name . '_' . $info['display']] = array(
'#title' => $view->display_handler
->get_option('show_title') ? $view
->get_title() : '',
'#type' => 'item',
'#weight' => module_exists('content') ? content_extra_field_weight($form['type']['#value'], $view->name . '_' . $info['display']) : 10,
'#value' => $result,
);
}
}
}
}