<?php
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\views\ViewExecutable;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\views\Plugin\Core\Entity\View;
const VIEWS_API_VERSION = '3.0';
function views_forms($form_id, $args) {
if (strpos($form_id, 'views_form_') === 0) {
return array(
$form_id => array(
'callback' => 'views_form',
),
);
}
}
function views_form_id($view) {
$parts = array(
'views_form',
$view->storage
->get('name'),
$view->current_display,
);
return implode('_', $parts);
}
function views_element_info() {
$types['view'] = array(
'#theme_wrappers' => array(
'container',
),
'#pre_render' => array(
'views_pre_render_view_element',
),
'#name' => NULL,
'#display_id' => 'default',
'#arguments' => array(),
);
return $types;
}
function views_pre_render_view_element($element) {
$element['#attributes']['class'][] = 'views-element-container';
$view = views_get_view($element['#name']);
if ($view && $view
->access($element['#display_id'])) {
$element['view']['#markup'] = $view
->preview($element['#display_id'], $element['#arguments']);
}
return $element;
}
function views_config_import_create($name, $new_config, $old_config) {
if (strpos($name, 'views.view.') !== 0) {
return FALSE;
}
$view = entity_create('view', $new_config
->get());
$view
->save();
return TRUE;
}
function views_theme($existing, $type, $theme, $path) {
$path = drupal_get_path('module', 'views');
module_load_include('inc', 'views', 'theme/theme');
$base = array(
'file' => 'theme.inc',
'path' => $path . '/theme',
);
$hooks['views_mini_pager'] = $base + array(
'variables' => array(
'tags' => array(),
'quantity' => 10,
'element' => 0,
'parameters' => array(),
),
'pattern' => 'views_mini_pager__',
);
$variables = array(
'display' => array(
'view_array' => array(),
'view' => NULL,
),
'style' => array(
'view' => NULL,
'options' => NULL,
'rows' => NULL,
'title' => NULL,
),
'row' => array(
'view' => NULL,
'options' => NULL,
'row' => NULL,
'field_alias' => NULL,
),
'exposed_form' => array(
'view' => NULL,
'options' => NULL,
),
'pager' => array(
'view' => NULL,
'options' => NULL,
'tags' => array(),
'quantity' => 10,
'element' => 0,
'parameters' => array(),
),
);
$hooks['views_view_field'] = $base + array(
'pattern' => 'views_view_field__',
'variables' => array(
'view' => NULL,
'field' => NULL,
'row' => NULL,
),
);
$hooks['views_view_grouping'] = $base + array(
'pattern' => 'views_view_grouping__',
'variables' => array(
'view' => NULL,
'grouping' => NULL,
'grouping_level' => NULL,
'rows' => NULL,
'title' => NULL,
),
);
$plugins = views_get_plugin_definitions();
foreach ($plugins as $type => $info) {
foreach ($info as $plugin => $def) {
if (isset($def['theme']) && (!isset($def['register theme']) || !empty($def['register theme']))) {
$hooks[$def['theme']] = array(
'pattern' => $def['theme'] . '__',
'file' => $def['theme file'],
'path' => $def['theme path'],
'variables' => $variables[$type],
);
$include = DRUPAL_ROOT . '/' . $def['theme path'] . '/' . $def['theme file'];
if (file_exists($include)) {
require_once $include;
}
if (!function_exists('theme_' . $def['theme'])) {
$hooks[$def['theme']]['template'] = drupal_clean_css_identifier($def['theme']);
}
}
if (isset($def['additional themes'])) {
foreach ($def['additional themes'] as $theme => $theme_type) {
if (empty($theme_type)) {
$theme = $theme_type;
$theme_type = $type;
}
$hooks[$theme] = array(
'pattern' => $theme . '__',
'file' => $def['theme file'],
'path' => $def['theme path'],
'variables' => $variables[$theme_type],
);
if (!function_exists('theme_' . $theme)) {
$hooks[$theme]['template'] = drupal_clean_css_identifier($theme);
}
}
}
}
}
$hooks['views_form_views_form'] = $base + array(
'render element' => 'form',
);
$hooks['views_exposed_form'] = $base + array(
'template' => 'views-exposed-form',
'pattern' => 'views_exposed_form__',
'render element' => 'form',
);
$hooks['views_more'] = $base + array(
'template' => 'views-more',
'pattern' => 'views_more__',
'variables' => array(
'more_url' => NULL,
'link_text' => 'more',
'view' => NULL,
),
);
return $hooks;
}
function _views_find_module_templates($cache, $path) {
$templates = array();
$regex = '/' . '\\.tpl\\.php' . '$' . '/';
$path = preg_replace('/^sites\\/all\\//', '', $path);
$config = str_replace('/', '\\/', conf_path());
$path = preg_replace('/^' . $config . '\\//', '', $path);
$files = drupal_system_listing($regex, $path, 'name', 0);
foreach ($files as $template => $file) {
if (($pos = strpos($template, '.')) !== FALSE) {
$template = substr($template, 0, $pos);
}
$hook = strtr($template, '-', '_');
if (isset($cache[$hook])) {
$templates[$hook] = array(
'template' => $template,
'path' => dirname($file->filename),
'includes' => isset($cache[$hook]['includes']) ? $cache[$hook]['includes'] : NULL,
);
}
if (isset($cache[$hook]['pattern'])) {
$templates[$hook]['pattern'] = $cache[$hook]['pattern'];
}
}
$patterns = array_keys($files);
foreach ($cache as $hook => $info) {
if (!empty($info['pattern'])) {
$pattern = strtr($info['pattern'], '_', '-');
$matches = preg_grep('/^' . $pattern . '/', $patterns);
if ($matches) {
foreach ($matches as $match) {
$file = substr($match, 0, strpos($match, '.'));
$templates[strtr($file, '-', '_')] = array(
'template' => $file,
'path' => dirname($files[$match]->uri),
'variables' => isset($info['variables']) ? $info['variables'] : NULL,
'render element' => isset($info['render element']) ? $info['render element'] : NULL,
'base hook' => $hook,
'includes' => isset($info['includes']) ? $info['includes'] : NULL,
);
}
}
}
}
return $templates;
}
function views_plugin_list() {
$plugin_data = views_get_plugin_definitions();
$plugins = array();
foreach (views_get_enabled_views() as $view) {
foreach ($view
->get('display') as $display_id => $display) {
foreach ($plugin_data as $type => $info) {
if ($type == 'display' && isset($display['display_plugin'])) {
$name = $display['display_plugin'];
}
elseif (isset($display['display_options']["{$type}_plugin"])) {
$name = $display['display_options']["{$type}_plugin"];
}
elseif (isset($display['display_options'][$type]['type'])) {
$name = $display['display_options'][$type]['type'];
}
else {
continue;
}
$key = $type . ':' . $name;
if (!isset($plugins[$key])) {
$plugins[$key] = array(
'type' => $type,
'title' => check_plain($info[$name]['title']),
'module' => check_plain($info[$name]['module']),
'views' => array(),
);
}
$plugins[$key]['views'][$view
->get('name')] = $view
->get('name');
}
}
}
return $plugins;
}
function views_preprocess_node(&$vars) {
if (!empty($vars['node']->view) && $vars['node']->view->storage
->get('name')) {
$vars['view'] = $vars['node']->view;
$vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->storage
->get('name');
if (!empty($vars['node']->view->current_display)) {
$vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->storage
->get('name') . '__' . $vars['node']->view->current_display;
if ($vars['page'] && $vars['view_mode'] == 'full' && !$vars['view']->display_handler
->hasPath()) {
$vars['page'] = FALSE;
}
}
}
if (!empty($vars['view']->style_plugin->row_plugin) && get_class($vars['view']->style_plugin->row_plugin) == 'views_plugin_row_node_view') {
node_row_node_view_preprocess_node($vars);
}
}
function views_preprocess_comment(&$vars) {
if (!empty($vars['comment']->view) && $vars['comment']->view->storage
->get('name')) {
$vars['view'] =& $vars['comment']->view;
$vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['comment']->view->storage
->get('name');
if (!empty($vars['node']->view->current_display)) {
$vars['theme_hook_suggestions'][] = 'comment__view__' . $vars['comment']->view->storage
->get('name') . '__' . $vars['comment']->view->current_display;
}
}
}
function views_permission() {
return array(
'administer views' => array(
'title' => t('Administer views'),
'description' => t('Access the views administration pages.'),
),
'access all views' => array(
'title' => t('Bypass views access control'),
'description' => t('Bypass access control when accessing views.'),
),
);
}
function views_menu() {
views_invalidate_cache();
$items = array();
$items['views/ajax'] = array(
'title' => 'Views',
'page callback' => 'views_ajax',
'theme callback' => 'ajax_base_page_theme',
'delivery callback' => 'ajax_deliver',
'access callback' => TRUE,
'description' => 'Ajax callback for view loading.',
'type' => MENU_CALLBACK,
'file' => 'includes/ajax.inc',
);
$items['admin/views/ajax/autocomplete/user'] = array(
'page callback' => 'views_ajax_autocomplete_user',
'theme callback' => 'ajax_base_page_theme',
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'includes/ajax.inc',
);
$items['admin/views/ajax/autocomplete/taxonomy/%'] = array(
'page callback' => 'views_ajax_autocomplete_taxonomy',
'theme callback' => 'ajax_base_page_theme',
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'includes/ajax.inc',
);
return $items;
}
function views_menu_alter(&$callbacks) {
$our_paths = array();
$views = views_get_applicable_views('uses_hook_menu');
foreach ($views as $data) {
list($view, $display_id) = $data;
$result = $view
->executeHookMenu($display_id, $callbacks);
if (is_array($result)) {
$regex = '#^(' . preg_replace('#%views_arg#', '%[^/]*', implode('|', array_keys($result))) . ')$#';
$matches = preg_grep($regex, array_keys($callbacks));
foreach ($matches as $path) {
if (!isset($our_paths[$path])) {
unset($callbacks[$path]);
}
}
foreach ($result as $path => $item) {
if (!isset($callbacks[$path])) {
$callbacks[$path] = $item;
}
else {
$callbacks[$path]['page arguments'][1] = (array) $callbacks[$path]['page arguments'][1];
$callbacks[$path]['page arguments'][1][] = $display_id;
$callbacks[$path]['access arguments'][] = $item['access arguments'][0];
$callbacks[$path]['load arguments'][1] = (array) $callbacks[$path]['load arguments'][1];
$callbacks[$path]['load arguments'][1][] = $display_id;
}
$our_paths[$path] = TRUE;
}
}
}
foreach ($views as $data) {
list($view, $display_id) = $data;
$view
->destroy();
}
}
function views_arg_load($value, $name, $display_id, $index) {
static $views = array();
$key = $name . ':' . $display_id . ':' . $value . ':' . $index;
if (isset($views[$key])) {
return $views[$key];
}
if ($view = views_get_view($name)) {
$view
->setDisplay($display_id);
$view
->initHandlers();
$ids = array_keys($view->argument);
$indexes = array();
$path = explode('/', $view
->getPath());
foreach ($path as $id => $piece) {
if ($piece == '%' && !empty($ids)) {
$indexes[$id] = array_shift($ids);
}
}
if (isset($indexes[$index])) {
if (isset($view->argument[$indexes[$index]])) {
$arg = $view->argument[$indexes[$index]]
->validate_argument($value) ? $value : FALSE;
$view
->destroy();
$views[$key] = $arg;
return $arg;
}
}
$view
->destroy();
}
}
function views_page($name, $display_id) {
$args = func_get_args();
array_shift($args);
array_shift($args);
if ($view = views_get_view($name)) {
return $view
->executeDisplay($display_id, $args);
}
return MENU_NOT_FOUND;
}
function views_page_alter(&$page) {
if ($view = views_get_page_view()) {
views_add_contextual_links($page, 'page', $view, $view->current_display);
}
}
function views_preprocess_html(&$variables) {
if (!empty($variables['page']['#views_contextual_links_info'])) {
$key = array_search('contextual-region', $variables['attributes']['class']
->value());
if ($key !== FALSE) {
unset($variables['attributes']['class'][$key]);
drupal_add_js(drupal_get_path('module', 'views') . '/js/views-contextual.js', array(
'group' => JS_LIBRARY,
'weight' => -1,
));
}
}
}
function views_contextual_links_view_alter(&$element, $items) {
if (!empty($element['#element']['#views_contextual_links_info']) && !empty($element['#element']['#type']) && $element['#element']['#type'] == 'page') {
$element['#attributes']['class'][] = 'views-contextual-links-page';
}
}
function views_block_info() {
views_include('cache');
$cache = views_cache_get('views_block_items', TRUE);
if ($cache && is_array($cache->data)) {
return $cache->data;
}
$items = array();
$views = views_get_all_views();
foreach ($views as $view) {
if (!$view
->isEnabled()) {
continue;
}
$executable = $view
->get('executable');
$executable
->initDisplay();
foreach ($executable->displayHandlers as $display) {
if (isset($display) && !empty($display->definition['uses_hook_block'])) {
$result = $display
->executeHookBlockList();
if (is_array($result)) {
$items = array_merge($items, $result);
}
}
if (isset($display) && $display
->getOption('exposed_block')) {
$result = $display
->getSpecialBlocks();
if (is_array($result)) {
$items = array_merge($items, $result);
}
}
}
}
$hashes = array();
$keys = array_keys($items);
foreach ($keys as $delta) {
if (strlen($delta) >= 32) {
$hash = md5($delta);
$hashes[$hash] = $delta;
$items[$hash] = $items[$delta];
unset($items[$delta]);
}
}
$old_hashes = state()
->get('views_block_hashes');
if ($hashes != $old_hashes) {
state()
->set('views_block_hashes', $hashes);
}
views_cache_set('views_block_items', $items, TRUE);
return $items;
}
function views_block_view($delta) {
$start = microtime(TRUE);
if (strlen($delta) == 32) {
$hashes = state()
->get('views_block_hashes');
if (!empty($hashes[$delta])) {
$delta = $hashes[$delta];
}
}
if (substr($delta, 0, 1) == '-') {
list($nothing, $type, $name, $display_id) = explode('-', $delta);
$type = '-' . $type;
if ($view = views_get_view($name)) {
if ($view
->access($display_id)) {
$view
->setDisplay($display_id);
if (isset($view->display_handler)) {
$output = $view->display_handler
->viewSpecialBlocks($type);
views_add_block_contextual_links($output, $view, $display_id, 'special_block_' . $type);
$view
->destroy();
return $output;
}
}
$view
->destroy();
}
}
$explode = explode('-', $delta);
if (count($explode) != 2) {
return;
}
list($name, $display_id) = $explode;
if ($view = views_get_view($name)) {
if ($view
->access($display_id)) {
$output = $view
->executeDisplay($display_id);
views_add_block_contextual_links($output, $view, $display_id);
$view
->destroy();
return $output;
}
$view
->destroy();
}
}
function views_add_block_contextual_links(&$block, ViewExecutable $view, $display_id, $block_type = 'block') {
if (!empty($block['content'])) {
if (is_string($block['content'])) {
$block['content'] = array(
'#markup' => $block['content'],
);
}
views_add_contextual_links($block['content'], $block_type, $view, $display_id);
}
}
function views_add_contextual_links(&$render_element, $location, ViewExecutable $view, $display_id) {
if (empty($view->hide_admin_links)) {
$plugin_id = $view->displayHandlers[$display_id]
->getPluginId();
$plugin = drupal_container()
->get('plugin.manager.views.display')
->getDefinition($plugin_id);
if (!isset($plugin['contextual_links_locations'])) {
$plugin['contextual_links_locations'] = array(
'view',
);
}
elseif ($plugin['contextual_links_locations'] == array() || $plugin['contextual_links_locations'] == array(
'',
)) {
$plugin['contextual_links_locations'] = array();
}
else {
$plugin += array(
'contextual_links_locations' => array(
'view',
),
);
}
$plugin['contextual_links_locations'][] = 'special_block_-exp';
$has_links = !empty($plugin['contextual links']) && !empty($plugin['contextual_links_locations']);
if ($has_links && in_array($location, $plugin['contextual_links_locations'])) {
foreach ($plugin['contextual links'] as $module => $link) {
$args = array();
$valid = TRUE;
if (!empty($link['argument properties'])) {
foreach ($link['argument properties'] as $property) {
if (!property_exists($view->storage, $property)) {
$valid = FALSE;
break;
}
else {
$args[] = $view->storage->{$property};
}
}
}
if ($valid) {
$render_element['#contextual_links'][$module] = array(
$link['parent path'],
$args,
);
$render_element['#views_contextual_links_info'][$module] = array(
'location' => $location,
'view' => $view,
'view_name' => $view->storage
->get('name'),
'view_display_id' => $display_id,
);
}
}
}
}
}
function views_language_list($field = 'name', $flags = LANGUAGE_ALL) {
$languages = language_list($flags);
$list = array();
foreach ($languages as $language) {
$list[$language->langcode] = $field == 'name' ? t($language->name) : $language->{$field};
}
return $list;
}
function views_cache_flush() {
return array(
'views_info',
'views_results',
);
}
function views_field_create_instance($instance) {
cache('views_info')
->deleteAll();
cache('views_results')
->deleteAll();
}
function views_field_update_instance($instance, $prior_instance) {
cache('views_info')
->deleteAll();
cache('views_results')
->deleteAll();
}
function views_field_delete_instance($instance) {
cache('views_info')
->deleteAll();
cache('views_results')
->deleteAll();
}
function views_invalidate_cache() {
cache('views_info')
->deleteAll();
cache_delete_tags(array(
'content' => TRUE,
));
state()
->set('menu_rebuild_needed', TRUE);
module_invoke_all('views_invalidate_cache');
}
function views_import_access() {
return user_access('administer views') && user_access('use PHP for settings');
}
function views_access() {
$args = func_get_args();
foreach ($args as $arg) {
if ($arg === TRUE) {
return TRUE;
}
if (!is_array($arg)) {
continue;
}
list($callback, $arguments) = $arg;
$arguments = $arguments ? $arguments : array();
foreach ($arguments as $key => $value) {
if (is_int($value) && isset($args[$value])) {
$arguments[$key] = $args[$value];
}
}
if (function_exists($callback) && call_user_func_array($callback, $arguments)) {
return TRUE;
}
}
return FALSE;
}
function views_check_perm($perm, $account = NULL) {
return user_access($perm, $account) || user_access('access all views', $account);
}
function views_check_roles($rids, $account = NULL) {
global $user;
$account = isset($account) ? $account : $user;
$roles = array_keys($account->roles);
$roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
return user_access('access all views', $account) || array_intersect(array_filter($rids), $roles);
}
function &views_set_page_view($view = NULL) {
static $cache = NULL;
if (isset($view)) {
$cache = $view;
}
return $cache;
}
function &views_get_page_view() {
return views_set_page_view();
}
function &views_set_current_view($view = NULL) {
static $cache = NULL;
if (isset($view)) {
$cache = $view;
}
return $cache;
}
function &views_get_current_view() {
return views_set_current_view();
}
function views_include($file) {
module_load_include('inc', 'views', 'includes/' . $file);
}
function views_hook_info() {
$hooks['views_data'] = array(
'group' => 'views',
);
$hooks['views_data_alter'] = array(
'group' => 'views',
);
$hooks['views_query_substitutions'] = array(
'group' => 'views',
);
$hooks['views_form_substitutions'] = array(
'group' => 'views',
);
return $hooks;
}
function views_library_info() {
$path = drupal_get_path('module', 'views') . '/js';
$libraries['views.ajax'] = array(
'title' => 'Views AJAX',
'version' => VERSION,
'js' => array(
"{$path}/base.js" => array(
'group' => JS_DEFAULT,
),
"{$path}/ajax.js" => array(
'group' => JS_DEFAULT,
),
"{$path}/ajax_view.js" => array(
'group' => JS_DEFAULT,
),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
array(
'system',
'drupalSettings',
),
array(
'system',
'jquery.once',
),
array(
'system',
'jquery.form',
),
array(
'system',
'drupal.ajax',
),
),
);
return $libraries;
}
function views_get_handler($table, $field, $type, $override = NULL) {
$manager = drupal_container()
->get("plugin.manager.views.{$type}");
$data = views_fetch_data($table);
if (isset($data[$field][$type])) {
$definition = $data[$field][$type];
foreach (array(
'group',
'title',
'title short',
'help',
'real field',
'real table',
) as $key) {
if (!isset($definition[$key])) {
if (!empty($data[$field][$key])) {
$definition[$key] = $data[$field][$key];
}
elseif (!empty($data['table'][$key])) {
$definition[$key] = $data['table'][$key];
}
}
}
$plugin_id = $override ?: $definition['id'];
try {
return $manager
->createInstance($plugin_id, $definition);
} catch (PluginException $e) {
try {
return $manager
->createInstance($definition['id'], $definition);
} catch (PluginException $e) {
}
}
}
debug(t("Missing handler: @table @field @type", array(
'@table' => $table,
'@field' => $field,
'@type' => $type,
)));
return $manager
->createInstance('broken');
}
function views_fetch_data($table = NULL, $reset = FALSE) {
views_include('cache');
return _views_fetch_data($table, $reset);
}
function views_fetch_base_tables() {
static $base_tables = array();
if (empty($base_tables)) {
$tables = array();
$data = views_fetch_data();
foreach ($data as $table => $info) {
if (!empty($info['table']['base'])) {
$tables[$table] = array(
'title' => $info['table']['base']['title'],
'description' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
);
}
}
uasort($tables, '_views_weight_sort');
$base_tables = $tables;
}
return $base_tables;
}
function _views_weight_sort($a, $b) {
if ($a['weight'] != $b['weight']) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
if ($a['title'] != $b['title']) {
return $a['title'] < $b['title'] ? -1 : 1;
}
return 0;
}
function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
$definitions = drupal_container()
->get("plugin.manager.views.{$type}")
->getDefinitions();
$plugins = array();
foreach ($definitions as $id => $plugin) {
if ($key && (empty($plugin['type']) || $plugin['type'] != $key)) {
continue;
}
if (isset($plugin['module']) && !module_exists($plugin['module'])) {
continue;
}
if (empty($plugin['no_ui']) && (empty($base) || empty($plugin['base']) || array_intersect($base, $plugin['base']))) {
$plugins[$id] = $plugin['title'];
}
}
if (!empty($plugins)) {
asort($plugins);
return $plugins;
}
return array();
}
function views_get_plugin($type, $plugin_id) {
return drupal_container()
->get("plugin.manager.views.{$type}")
->createInstance($plugin_id);
}
function views_get_plugin_definitions() {
$container = drupal_container();
$plugins = array();
foreach (ViewExecutable::getPluginTypes() as $plugin_type) {
$plugins[$plugin_type] = $container
->get("plugin.manager.views.{$plugin_type}")
->getDefinitions();
}
return $plugins;
}
function views_get_enabled_display_extenders() {
$enabled = array_filter((array) config('views.settings')
->get('display_extenders'));
return drupal_map_assoc($enabled);
}
function views_new_view() {
return entity_create('view', array());
}
function views_create_view(array $values = array()) {
return entity_create('view', $values);
}
function views_get_applicable_views($type) {
$result = array();
$views = views_get_all_views();
foreach ($views as $view) {
if (!$view
->isEnabled()) {
continue;
}
$display = $view
->get('display');
if (empty($display)) {
continue;
}
foreach (array_keys($display) as $id) {
$plugin = drupal_container()
->get('plugin.manager.views.display')
->getDefinition($display[$id]['display_plugin']);
if (!empty($plugin[$type])) {
$v = $view
->get('executable')
->cloneView();
if ($v
->setDisplay($id) && $v->display_handler
->isEnabled()) {
$result[] = array(
$v,
$id,
);
}
unset($v);
}
}
}
return $result;
}
function views_get_all_views() {
return entity_get_controller('view')
->load();
}
function views_get_enabled_views() {
$views = views_get_all_views();
return array_filter($views, 'views_view_is_enabled');
}
function views_get_disabled_views() {
$views = views_get_all_views();
return array_filter($views, 'views_view_is_disabled');
}
function views_get_views_as_options($views_only = FALSE, $filter = 'all', $exclude_view = NULL, $optgroup = FALSE, $sort = FALSE) {
switch ($filter) {
case 'all':
case 'disabled':
case 'enabled':
$func = "views_get_{$filter}_views";
$views = $func();
break;
default:
return array();
}
if (empty($exclude_view)) {
$exclude_view_name = '';
$exclude_view_display = '';
}
elseif (is_object($exclude_view)) {
$exclude_view_name = $exclude_view->storage
->id();
$exclude_view_display = $exclude_view->current_display;
}
else {
list($exclude_view_name, $exclude_view_display) = explode(':', "{$exclude_view}:");
}
$options = array();
foreach ($views as $view) {
$id = $view
->id();
if ($views_only && $id != $exclude_view_name) {
$options[$id] = $view
->getHumanName();
}
else {
foreach ($view
->get('display') as $display_id => $display) {
if (!($id == $exclude_view_name && $display_id == $exclude_view_display)) {
if ($optgroup) {
$options[$id][$id . ':' . $display['id']] = t('@view : @display', array(
'@view' => $id,
'@display' => $display['id'],
));
}
else {
$options[$id . ':' . $display['id']] = t('View: @view - Display: @display', array(
'@view' => $id,
'@display' => $display['id'],
));
}
}
}
}
}
if ($sort) {
ksort($options);
}
return $options;
}
function views_view_is_enabled(View $view) {
return $view
->isEnabled();
}
function views_view_is_disabled(View $view) {
return !$view
->isEnabled();
}
function views_enable_view(View $view) {
$view
->enable();
}
function views_disable_view(View $view) {
$view
->disable();
}
function views_get_view($name) {
$view = entity_load('view', $name);
if ($view) {
return $view
->get('executable');
}
}
function views_view_has_form_elements($view) {
foreach ($view->field as $field) {
if (property_exists($field, 'views_form_callback') || method_exists($field, 'views_form')) {
return TRUE;
}
}
$area_handlers = array_merge(array_values($view->header), array_values($view->footer));
$empty = empty($view->result);
foreach ($area_handlers as $area) {
if (method_exists($area, 'views_form') && !$area
->views_form_empty($empty)) {
return TRUE;
}
}
return FALSE;
}
function views_form($form, &$form_state, ViewExecutable $view, $output) {
$form_state['step'] = isset($form_state['step']) ? $form_state['step'] : 'views_form_views_form';
$form_state['cache'] = TRUE;
$form = array();
$query = drupal_get_query_parameters();
$form['#action'] = url($view
->getUrl(), array(
'query' => $query,
));
$form['show_view_elements'] = array(
'#type' => 'value',
'#value' => $form_state['step'] == 'views_form_views_form' ? TRUE : FALSE,
);
$form = $form_state['step']($form, $form_state, $view, $output);
return $form;
}
function views_form_views_form($form, &$form_state, ViewExecutable $view, $output) {
$form['#prefix'] = '<div class="views-form">';
$form['#suffix'] = '</div>';
$form['#theme'] = 'views_form_views_form';
$form['#validate'][] = 'views_form_views_form_validate';
$form['#submit'][] = 'views_form_views_form_submit';
$form['output'] = array(
'#type' => 'markup',
'#markup' => $output,
'#weight' => 50,
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$substitutions = array();
foreach ($view->field as $field_name => $field) {
$form_element_name = $field_name;
if (method_exists($field, 'form_element_name')) {
$form_element_name = $field
->form_element_name();
}
$method_form_element_row_id_exists = FALSE;
if (method_exists($field, 'form_element_row_id')) {
$method_form_element_row_id_exists = TRUE;
}
$has_form = FALSE;
if (property_exists($field, 'views_form_callback')) {
$callback = $field->views_form_callback;
$callback($view, $field, $form, $form_state);
$has_form = TRUE;
}
elseif (method_exists($field, 'views_form')) {
$field
->views_form($form, $form_state);
$has_form = TRUE;
}
if ($has_form) {
foreach ($view->result as $row_id => $row) {
if ($method_form_element_row_id_exists) {
$form_element_row_id = $field
->form_element_row_id($row_id);
}
else {
$form_element_row_id = $row_id;
}
$substitutions[] = array(
'placeholder' => '<!--form-item-' . $form_element_name . '--' . $form_element_row_id . '-->',
'field_name' => $form_element_name,
'row_id' => $form_element_row_id,
);
}
}
}
$area_handlers = array_merge(array_values($view->header), array_values($view->footer));
$empty = empty($view->result);
foreach ($area_handlers as $area) {
if (method_exists($area, 'views_form') && !$area
->views_form_empty($empty)) {
$area
->views_form($form, $form_state);
}
}
$form['#substitutions'] = array(
'#type' => 'value',
'#value' => $substitutions,
);
return $form;
}
function views_form_views_form_validate($form, &$form_state) {
$view = $form_state['build_info']['args'][0];
foreach ($view->field as $field_name => $field) {
if (method_exists($field, 'views_form_validate')) {
$field
->views_form_validate($form, $form_state);
}
}
foreach (array(
'header',
'footer',
) as $area) {
foreach ($view->{$area} as $area_name => $area_handler) {
if (method_exists($area_handler, 'views_form_validate')) {
$area_handler
->views_form_validate($form, $form_state);
}
}
}
}
function views_form_views_form_submit($form, &$form_state) {
$view = $form_state['build_info']['args'][0];
foreach ($view->field as $field_name => $field) {
if (method_exists($field, 'views_form_submit')) {
$field
->views_form_submit($form, $form_state);
}
}
foreach (array(
'header',
'footer',
) as $area) {
foreach ($view->{$area} as $area_name => $area_handler) {
if (method_exists($area_handler, 'views_form_submit')) {
$area_handler
->views_form_submit($form, $form_state);
}
}
}
}
function views_exposed_form($form, &$form_state) {
if ($batch = batch_get() && isset($batch['current_set'])) {
return array(
'#theme' => '',
);
}
$form_state['must_validate'] = TRUE;
$view =& $form_state['view'];
$display =& $form_state['display'];
$form_state['input'] = $view
->getExposedInput();
$form_state['exposed'] = TRUE;
if ($cache = views_exposed_form_cache($view->storage
->get('name'), $view->current_display)) {
return $cache;
}
$form['#info'] = array();
foreach ($view->display_handler->handlers as $type => $value) {
foreach ($view->{$type} as $id => $handler) {
if ($handler
->canExpose() && $handler
->isExposed()) {
if ($handler
->isAGroup()) {
$handler
->group_form($form, $form_state);
$id = $handler->options['group_info']['identifier'];
}
else {
$handler
->buildExposedForm($form, $form_state);
}
if ($info = $handler
->exposedInfo()) {
$form['#info']["{$type}-{$id}"] = $info;
}
}
}
}
$form['submit'] = array(
'#name' => '',
'#type' => 'submit',
'#value' => t('Apply'),
'#id' => drupal_html_id('edit-submit-' . $view->storage
->get('name')),
);
$form['#action'] = url($view->display_handler
->getUrl());
$form['#theme'] = views_theme_functions('views_exposed_form', $view, $display);
$form['#id'] = drupal_clean_css_identifier('views_exposed_form-' . check_plain($view->storage
->get('name')) . '-' . check_plain($display['id']));
if ($view->use_ajax) {
$form['#attached']['library'][] = array(
'system',
'jquery.form',
);
}
$exposed_form_plugin = $form_state['exposed_form_plugin'];
$exposed_form_plugin
->exposed_form_alter($form, $form_state);
views_exposed_form_cache($view->storage
->get('name'), $view->current_display, $form);
return $form;
}
function views_form_views_exposed_form_alter(&$form, &$form_state) {
$form['form_build_id']['#access'] = FALSE;
$form['form_token']['#access'] = FALSE;
$form['form_id']['#access'] = FALSE;
}
function views_exposed_form_validate(&$form, &$form_state) {
foreach (array(
'field',
'filter',
) as $type) {
$handlers =& $form_state['view']->{$type};
foreach ($handlers as $key => $handler) {
$handlers[$key]
->validateExposed($form, $form_state);
}
}
$exposed_form_plugin = $form_state['exposed_form_plugin'];
$exposed_form_plugin
->exposed_form_validate($form, $form_state);
}
function views_exposed_form_submit(&$form, &$form_state) {
foreach (array(
'field',
'filter',
) as $type) {
$handlers =& $form_state['view']->{$type};
foreach ($handlers as $key => $info) {
$handlers[$key]
->submitExposed($form, $form_state);
}
}
$form_state['view']->exposed_data = $form_state['values'];
$form_state['view']->exposed_raw_input = array();
$exclude = array(
'submit',
'form_build_id',
'form_id',
'form_token',
'exposed_form_plugin',
'',
'reset',
);
$exposed_form_plugin = $form_state['exposed_form_plugin'];
$exposed_form_plugin
->exposed_form_submit($form, $form_state, $exclude);
foreach ($form_state['values'] as $key => $value) {
if (!in_array($key, $exclude)) {
$form_state['view']->exposed_raw_input[$key] = $value;
}
}
}
function views_exposed_form_cache($views_name, $display_name, $form_output = NULL) {
$views_exposed =& drupal_static(__FUNCTION__);
if (!empty($form_output)) {
$views_exposed[$views_name][$display_name] = $form_output;
return;
}
return empty($views_exposed[$views_name][$display_name]) ? FALSE : $views_exposed[$views_name][$display_name];
}
function views_theme_functions($hook, ViewExecutable $view, $display = NULL) {
module_load_include('inc', 'views', 'theme/theme');
return _views_theme_functions($hook, $view, $display);
}
function views_views_query_substitutions($view) {
return array(
'***CURRENT_VERSION***' => VERSION,
'***CURRENT_TIME***' => REQUEST_TIME,
'***CURRENT_LANGUAGE***' => language(LANGUAGE_TYPE_CONTENT)->langcode,
'***DEFAULT_LANGUAGE***' => language_default()->langcode,
);
}
function views_query_views_alter(AlterableInterface $query) {
$substitutions = $query
->getMetaData('views_substitutions');
$tables =& $query
->getTables();
$where =& $query
->conditions();
foreach ($tables as $table_name => $table_metadata) {
foreach ($table_metadata['arguments'] as $replacement_key => $value) {
if (isset($substitutions[$value])) {
$tables[$table_name]['arguments'][$replacement_key] = $substitutions[$value];
}
}
}
_views_query_tag_alter_condition($query, $where, $substitutions);
}
function _views_query_tag_alter_condition(AlterableInterface $query, &$conditions, $substitutions) {
foreach ($conditions as $condition_id => &$condition) {
if (is_numeric($condition_id)) {
if (is_string($condition['field'])) {
$condition['field'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['field']);
}
elseif (is_object($condition['field'])) {
$sub_conditions =& $condition['field']
->conditions();
_views_query_tag_alter_condition($query, $sub_conditions, $substitutions);
}
if (is_object($condition['value'])) {
$subquery = $condition['value'];
$subquery
->addMetaData('views_substitutions', $query
->getMetaData('views_substitutions'));
views_query_views_alter($condition['value']);
}
elseif (isset($condition['value'])) {
$condition['value'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['value']);
}
}
}
}
function views_embed_view($name, $display_id = 'default') {
$args = func_get_args();
array_shift($args);
if (count($args)) {
array_shift($args);
}
$view = views_get_view($name);
if (!$view || !$view
->access($display_id)) {
return;
}
return $view
->preview($display_id, $args);
}
function views_get_view_result($name, $display_id = NULL) {
$args = func_get_args();
array_shift($args);
if (count($args)) {
array_shift($args);
}
$view = views_get_view($name);
if (is_object($view)) {
if (is_array($args)) {
$view
->setArguments($args);
}
if (is_string($display_id)) {
$view
->setDisplay($display_id);
}
else {
$view
->initDisplay();
}
$view
->preExecute();
$view
->execute();
return $view->result;
}
else {
return array();
}
}
function views_process_check_options($element, &$form_state) {
if ($element['#type'] == 'checkboxes' || $element['#type'] == 'checkbox') {
$element['#options'] = array_map('check_plain', $element['#options']);
}
return $element;
}
function views_element_validate_tags($element, &$form_state) {
$values = array_map('trim', explode(',', $element['#value']));
foreach ($values as $value) {
if (preg_match("/[^a-z_]/", $value)) {
form_error($element, t('The query tags may only contain lower-case alphabetical characters and underscores.'));
return;
}
}
}
function views_handler_field_custom_pre_render_move_text($form) {
$form['text'] = $form['alter']['text'];
$form['help'] = $form['alter']['help'];
unset($form['alter']['text']);
unset($form['alter']['help']);
return $form;
}
function _field_view_formatter_options($field_type = NULL) {
$options =& drupal_static(__FUNCTION__);
if (!isset($options)) {
$field_types = field_info_field_types();
$options = array();
foreach (field_info_formatter_types() as $name => $formatter) {
foreach ($formatter['field_types'] as $formatter_field_type) {
if (isset($field_types[$formatter_field_type])) {
$options[$formatter_field_type][$name] = $formatter['label'];
}
}
}
}
if ($field_type) {
return !empty($options[$field_type]) ? $options[$field_type] : array();
}
return $options;
}
function views_trim_text($alter, $value) {
if (drupal_strlen($value) > $alter['max_length']) {
$value = drupal_substr($value, 0, $alter['max_length']);
if (!empty($alter['word_boundary'])) {
$regex = "(.*)\\b.+";
if (function_exists('mb_ereg')) {
mb_regex_encoding('UTF-8');
$found = mb_ereg($regex, $value, $matches);
}
else {
$found = preg_match("/{$regex}/us", $value, $matches);
}
if ($found) {
$value = $matches[1];
}
}
$value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value));
if (!empty($alter['ellipsis'])) {
$value .= t('...');
}
}
if (!empty($alter['html'])) {
$value = _filter_htmlcorrector($value);
}
return $value;
}
function _views_array_filter_zero($var) {
return trim($var) != "";
}
function views_array_key_plus($array) {
$keys = array_keys($array);
rsort($keys);
foreach ($keys as $key) {
$array[$key + 1] = $array[$key];
unset($array[$key]);
}
asort($array);
return $array;
}
function views_config_import_delete($name, $new_config, $old_config) {
if (strpos($name, 'views.view.') !== 0) {
return FALSE;
}
list(, , $id) = explode('.', $name);
$view = entity_load('view', $id);
entity_delete($view);
return TRUE;
}