View source
<?php
class LinkitViewsPlugin extends LinkitPlugin {
var $table_name;
function __construct($plugin, $profile) {
parent::__construct($plugin, $profile);
$this->table_name = 'linkit_tmp_view_table_' . rand(0, 1000) . rand(0, 1000);
}
function create_tmp_table() {
$temp_table = 'CREATE TEMPORARY TABLE {' . $this->table_name . '} (human_name VARCHAR(255) NOT NULL, display_title VARCHAR(65) NOT NULL, path VARCHAR(255) NOT NULL)';
return db_query($temp_table);
}
function populate_tmp_table() {
$query = db_select('views_view', 'w')
->condition('wd.display_plugin', 'page')
->fields('wd', array(
'display_options',
'display_title',
))
->fields('w', array(
'human_name',
));
$query
->join('views_display', 'wd', 'w.vid = wd.vid');
$result = $query
->execute();
foreach ($result as $view) {
$optinos = unserialize($view->display_options);
$fields = array(
'display_title' => $view->display_title,
'human_name' => $view->human_name,
'path' => $optinos['path'],
);
db_insert($this->table_name)
->fields($fields)
->execute();
}
}
function autocomplete_callback() {
$this
->create_tmp_table();
$this
->populate_tmp_table();
$matches = array();
$query = db_select($this->table_name, 'tmp')
->fields('tmp', array(
'path',
'display_title',
'human_name',
))
->condition('tmp.display_title', '%' . db_like($this->serach_string) . '%', 'LIKE')
->addTag('linkit_views_autocomplete')
->execute();
foreach ($query as $view) {
$matches[] = array(
'title' => $view->display_title,
'path' => base_path() . $view->path,
'description' => t('View: %view', array(
'%view' => $view->human_name,
)),
'group' => t('View pages'),
);
}
return $matches;
}
}