View source
<?php
module_load_include('inc', 'eva', 'eva.theme');
function eva_views_api() {
return array(
'api' => 3,
);
}
function eva_field_extra_fields() {
$extras = array();
$views = eva_get_views();
foreach ($views as $entity => $data) {
foreach ($data as $view) {
if (!empty($view['bundles'])) {
$bundles = $view['bundles'];
}
else {
$entity_info = entity_get_info($entity);
$bundles = array_keys($entity_info['bundles']);
}
foreach ($bundles as $bundle) {
$context = $view['show_on'];
$extras[$entity][$bundle][$context][$view['name'] . '_' . $view['display']] = array(
'label' => empty($view['title']) ? $view['name'] : $view['title'],
'description' => $view['title'],
'weight' => 10,
);
if ($context == 'display' && !empty($view['exposed form']) && !empty($view['exposed form split'])) {
$extras[$entity][$bundle][$context][$view['name'] . '_' . $view[$context] . '_' . 'form'] = array(
'label' => (empty($view['title']) ? $view['name'] : $view['title']) . ' (' . t('Exposed form') . ')',
'description' => t('The exposed filter form of the view.'),
'weight' => 9,
);
}
}
}
}
return $extras;
}
function eva_entity_view_alter(&$build, $entity_type) {
if (!($entity = _eva_extract_entity_from_build($build, $entity_type))) {
return;
}
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
if (!($fields = field_extra_fields_get_display($entity_type, $bundle, $build['#view_mode']))) {
return;
}
if (!($views = eva_get_views($entity_type))) {
return;
}
foreach ($views as $info) {
$longname = $info['name'] . '_' . $info['display'];
if (isset($fields[$longname]) && $fields[$longname]['visible']) {
_eva_build_extra_fields($build, $entity_type, $entity, $fields, $info['name'], $info['display'], 'display');
}
}
}
function eva_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
if (!($views = eva_get_views($entity_type))) {
return;
}
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
if (!($fields = field_info_extra_fields($entity_type, $bundle, 'form'))) {
return;
}
foreach ($views as $info) {
if (isset($fields[$info['name'] . '_' . $info['display']])) {
_eva_build_extra_fields($form, $entity_type, $entity, $fields, $info['name'], $info['display'], 'form');
}
}
}
function _eva_build_extra_fields(&$build, $entity_type, $entity, $fields, $name, $display_id, $context) {
if ($view = views_get_view($name)) {
$view
->set_display($display_id);
if ($view
->access($display_id)) {
$view->current_entity = $entity;
$view->current_entity_type = $entity_type;
$view->current_entity_view_mode = $context === 'form' ? 'form' : $build['#view_mode'];
$longname = $name . '_' . $display_id;
if ($context == 'display' && isset($fields[$longname . '_form'])) {
$view
->init_handlers();
$exposed_form = $view->display_handler
->get_plugin('exposed_form');
$build[$longname . '_form'] = array(
'#markup' => $exposed_form
->render_exposed_form(TRUE),
);
}
$arg_mode = $view->display_handler
->get_option('argument_mode');
if ($arg_mode == 'token') {
$args = array();
if ($token_string = $view->display_handler
->get_option('default_argument')) {
$token_values = eva_get_arguments_from_token_string($token_string, $entity_type, $entity);
foreach ($token_values as $key => $value) {
$args[$key] = $value;
}
}
}
elseif ($arg_mode == 'id') {
list($id, , ) = entity_extract_ids($entity_type, $entity);
$args = array(
$id,
);
}
else {
$args = array();
}
$result = $view
->execute_display($display_id, $args);
if (!empty($result)) {
$build[$longname] = array(
'#markup' => $result,
'#weight' => $fields[$longname]['weight'],
);
}
}
}
}
function eva_get_views($type = NULL, $reset = FALSE) {
$used_views =& drupal_static(__FUNCTION__);
if (!isset($used_views) || $reset) {
views_include('cache');
if (!$reset) {
$cache = views_cache_get("eva");
if (isset($cache->data)) {
$used_views = $cache->data;
}
}
if (!isset($used_views)) {
$used_views = array();
ctools_include('export');
ctools_export_load_object_reset('views_view');
$views = views_get_applicable_views('uses hook entity view');
foreach ($views as $data) {
list($view, $display_id) = $data;
$view_entity = $view->display_handler
->get_option('entity_type');
$view
->init_handlers();
$used_views[$view_entity][] = array(
'name' => $view->name,
'title' => 'EVA: ' . $view
->get_human_name() . ' - ' . $view->display[$display_id]->display_title,
'display' => $display_id,
'show_on' => $view->display_handler
->get_option('show_on'),
'bundles' => $view->display_handler
->get_option('bundles'),
'exposed form' => $view->display_handler
->uses_exposed(),
'exposed form split' => $view->display_handler
->get_option('exposed_form_as_field'),
);
$view
->destroy();
}
views_cache_set("eva", $used_views);
}
}
if (isset($type) & isset($used_views)) {
return isset($used_views[$type]) ? $used_views[$type] : array();
}
else {
return isset($used_views) ? $used_views : array();
}
}
function _eva_extract_entity_from_build($build, $entity_type = NULL) {
if (!empty($build['#entity'])) {
return $build['#entity'];
}
elseif (!empty($build['#entity_type']) && !empty($build['#' . $build['#entity_type']])) {
return $build['#' . $build['#entity_type']];
}
elseif ($entity_type && !empty($build['#' . $entity_type])) {
return $build['#' . $entity_type];
}
elseif ($build['#entity_type'] == 'user') {
return $build['#account'];
}
elseif ($build['#entity_type'] == 'taxonomy_term') {
return $build['#term'];
}
return FALSE;
}
function eva_get_arguments_from_token_string($string, $type, $object) {
$args = trim($string);
if (empty($args)) {
return array();
}
$args = token_replace($args, array(
$type => $object,
), array(
'sanitize' => FALSE,
'clear' => TRUE,
));
return explode('/', $args);
}
function eva_modules_enabled($modules) {
drupal_static_reset('eva_get_views');
cache_clear_all('*', 'cache_views', TRUE);
}
function eva_modules_disabled($modules) {
drupal_static_reset('eva_get_views');
cache_clear_all('*', 'cache_views', TRUE);
}
function eva_form_views_ui_edit_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['save']["#submit"][] = 'field_cache_clear';
}