hook_services_resource is not working - drupal-7

EXAMPLE : this is my sample for service resource hook, i'm using drupal 7. I have enabled the services module and ctools module. the end point is working fine, but when i browse the endpoint/commerce_rest/retrieve url is not working
It returns the following exception,
exception 'ServicesException' with message 'Could not find resource retrieve.' in /var/www/webui/drupal/sites/all/modules/services/includes/services.runtime.inc:359\nStack trace:\n#0 /var/www/webui/drupal/sites/all/modules/services/servers/rest_server/includes/RESTServer.inc(83): services_error('Could not find ...', 404)\n#1 /var/www/webui/drupal/sites/all/modules/services/servers/rest_server/includes/RESTServer.inc(36): RESTServer->getController()\n#2 /var/www/webui/drupal/sites/all/modules/services/servers/rest_server/rest_server.module(40): RESTServer->handle()\n#3 [internal function]: rest_server_server()\n#4 /var/www/webui/drupal/sites/all/modules/services/services.module(218): call_user_func('rest_server_ser...')\n#5 [internal function]: services_endpoint_callback('commerce_rest', 'retrieve')\n#6 /var/www/webui/drupal/includes/menu.inc(516): call_user_func_array('services_endpoi...', Array)\n#7 /var/www/webui/drupal/index.php(33): menu_execute_active_handler()\n#8 {main}
function test_module_services_resources() {
$resource = array(
'commerce_rest' => array(
'retrieve' => array(
'file' => array('type' => 'inc', 'module' => 'search_service'),
'callback' => 'test_module_search_service_results',
'access callback' => 'search_access_callback',
'access arguments' => array('view'),
'access arguments append' => TRUE,
),
)
)
);
return $resource;
}
this hook is not invoked, when i browse the url
suggestion will be appreciable..

Hi You are missing the file name in the file argument
function test_module_services_resources() {
$resource = array(
'commerce_rest' => array(
'retrieve' => array(
'file' => array('type' => 'inc', 'module' => 'search_service', **'name' => "you are missing this"**),
'callback' => 'test_module_search_service_results',
'access callback' => 'search_access_callback',
'access arguments' => array('view'),
'access arguments append' => TRUE,
),
)
)
);
return $resource;
}

Related

Cakephp: Spurious login message

I do not have a users login page in Cakephp(I have another login page and associated controller that works fine)
Every so often, especially if I have not used the app for a while I get a message stating that my users/login page is missing.
I have searched within the text of all files in my subdirectory, but am at a loss as to where this message originates.
Any ideas?
You must be logged in to view this page.
Missing Controller
Error: UsersController could not be found.
Error: Create the class UsersController below in file: app\Controller\UsersController.php
<?php
class UsersController extends AppController {
}
Notice: If you want to customize this error message, create app\View\Errors\missing_controller.ctp
**Stack Trace**
APP\webroot\index.php line 111 → Dispatcher->dispatch(CakeRequest, CakeResponse)
$Dispatcher->dispatch(
new CakeRequest(),
new CakeResponse()
);
object(CakeRequest) {
params => array(
'plugin' => null,
'controller' => 'users',
'action' => 'login',
'named' => array(),
'pass' => array()
)
data => array()
query => array()
url => 'users/login'
base => '/checks'
webroot => '/checks/'
here => '/checks/users/login'
[protected] _detectors => array(
'get' => array(
'env' => 'REQUEST_METHOD',
'value' => 'GET'
),
'post' => array(
'env' => 'REQUEST_METHOD',
'value' => 'POST'
),
'put' => array(
'env' => 'REQUEST_METHOD',
'value' => 'PUT'
),
'delete' => array(
'env' => 'REQUEST_METHOD',
'value' => 'DELETE'
),
'head' => array(
'env' => 'REQUEST_METHOD',
'value' => 'HEAD'
),
'options' => array(
'env' => 'REQUEST_METHOD',
'value' => 'OPTIONS'
),
'ssl' => array(
'env' => 'HTTPS',
'value' => (int) 1
),
'ajax' => array(
'env' => 'HTTP_X_REQUESTED_WITH',
'value' => 'XMLHttpRequest'
),
'flash' => array(
'env' => 'HTTP_USER_AGENT',
'pattern' => '/^(Shockwave|Adobe) Flash/'
),
'mobile' => array(
'env' => 'HTTP_USER_AGENT',
'options' => array(
[maximum depth reached]
)
),
'requested' => array(
'param' => 'requested',
'value' => (int) 1
)
)
[protected] _input => ''
}
Appcontroller code follows:
App::uses('Controller', 'Controller');
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* #package app.Controller
* #link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller {
// added the debug toolkit
// sessions support
// authorization for login and logut redirect
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'instructors', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'instructors', 'action' => 'login'),
'authError' => 'You must be logged in to view this page.',
'loginError' => 'Invalid Username or Password entered, please try again.'
));
// only allow the login controllers only
public function beforeFilter() {
$this->Auth->allow('login','index');
//allow access to currently logged in user to all programs
$this->set('logged_in',$this->Auth->loggedIn());
$this->set('current_user',$this->Auth->user());
}
public function isAuthorized($user) {
return true;
}
}
This sounds like a routing issue to me. There are a number of things which I would check.
1) Routing
Make sure that you have a route for /checks/users/login, which either resolves to your UsersController::login() method or redirects.
2) Auth setup
I would make sure that your login url specified in your Auth config specifically removes and prefix routing and plugin routing.
<?php
// If you were using 'admin' prefix routing
'loginRedirect' => array('controller' => 'instructors', 'action' => 'index', 'admin' => false, 'plugin' => false)
3) Login method
Double check that you have a UsersController::login() method defined in your controller.
So in summary, I believe the issue is caused because you are in /checks routing, and you are trying to redirect the user to your login, but the /checks routing it sticking, meaning you're actually trying to redirect to /checks/users/login which doesn't exist, which is why a 404 is being generated.

CakeDC Search and Tag plugin - search for multiple tags

Hopefully I'm missing something simple here. I'm using the CakeDC Search and Tags plugin for my cake (2.3.4) app.
Along with generic search by field functionality I want the user to be able to search by tags. I've almost got this working but the search will only display results if you search for a single tag not multiples. For example, if I add an article with the following tags - black, white, red. The article will only show in the search results if I search for a single tag (say, black) and not all 3, or even 2... What am I missing?
Heres my code:
Article.php model
class Article extends AppModel {
public $actsAs = array(
'Upload.Upload' => array(
'screenshot1' => array (
'fields' => array (
'dir' => 'dir'
),
'thumbnailMethod' => 'php',
'thumbnailSizes' => array(
'xvga' => '1024x768',
'vga' => '640x480',
'thumb' => '80x80'
),
),
),
'Search.Searchable',
'Tags.Taggable'
);
// Search plugin filters
public $filterArgs = array(
'title' => array('type' => 'like'),
'environment' => array('type' => 'like'),
'description' => array('type' => 'like'),
'error' => array('type' => 'like'),
'cause' => array('type' => 'like'),
'resolution' => array('type' => 'like'),
'live' => array('type' => 'value'),
'synced' => array('type' => 'value'),
'tags' => array('type' => 'subquery', 'method' => 'findByTags', 'field' => 'Article.id'),
array('name' => 'search', 'type' => 'query', 'method' => 'filterQuery'),
);
// This is the OR query that runs when the user uses the client side signle field search
public function filterQuery($data = array()) {
if(empty($data['search'])) { // search is the name of my search field
return array();
}
$query = '%'.$data['search'].'%';
return array(
'OR' => array(
'Article.title LIKE' => $query,
'Article.description LIKE' => $query,
'Article.error LIKE' => $query,
)
);
}
// Find by tags method
public function findByTags($data = array()) {
$this->Tagged->Behaviors->attach('Containable', array('autoFields' => false));
$this->Tagged->Behaviors->attach('Search.Searchable');
$query = $this->Tagged->getQuery('all', array(
'conditions' => array('Tag.name' => $data['tags']),
'fields' => array('foreign_key'),
'contain' => array('Tag')
));
return $query;
}
public $hasAndBelongsToMany = array(
'Category' => array(
'className' => 'Category',
'joinTable' => 'articles_categories',
'foreignKey' => 'article_id',
'associationForeignKey' => 'category_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Tag' => array('with' => 'Tagged')
);
Controller method
public function admin_advancedSearch() {
// Disable validation for this action
$this->Article->validate = array();
// For search plugin
$this->Prg->commonProcess();
// Set searched for details
$this->set('searchedFor', $this->passedArgs);
// If passed args are all empty
if ($this->passedArgs) {
if (empty($this->passedArgs['title']) AND
empty($this->passedArgs['environment']) AND
empty($this->passedArgs['description']) AND
empty($this->passedArgs['error']) AND
empty($this->passedArgs['cause']) AND
empty($this->passedArgs['resolution']) AND
empty($this->passedArgs['live']) AND
empty($this->passedArgs['synced']) AND
empty($this->passedArgs['tags'])) {
$this->Session->setFlash('Please enter at least one search criteria', 'flash_failure');
// Set this var for checks in view
$this->set('emptySeach', true);
} else {
$paginateConditions = $this->Article->parseCriteria($this->passedArgs);
$this->paginate = array('conditions' => array(
$paginateConditions),
'limit' => 10,
'order' => array('Article.modified' => 'DESC'));
$this->set('articles', $this->paginate());
// Count number of results
$count = 0;
foreach ($this->paginate() as $result) {
$count++;
}
$this->set('resultsCount', $count);
// Search was not empty - set flag for view
$this->set('emptySeach', false);
}
}
// Set layout
$this->layout = 'admin';
//debug($this->passedArgs);
}
All plugins are loaded successfully from bootstrap, searches work fine without tags. Searches with tags only work if one tag is entered....
*EDIT *
If I debug $query from the findByTags method I get this:
'SELECT `Tagged`.`foreign_key` FROM `knowledgebase`.`tagged` AS `Tagged` LEFT JOIN `knowledgebase`.`tags` AS `Tag` ON (`Tagged`.`tag_id` = `Tag`.`id`) WHERE `Tag`.`name` = 'kia, rio, red''
So it looks like its trying to find a single tag with all the searched for tags in its name. How can I make the WHERE part an IN?
For example:
WHERE `Tag`.`name` IN ('kia', 'rio', 'red')
Thanks
Use the "method" https://github.com/cakedc/search#behavior-and-model-configuration key her to pass the search args to a customized method.
The example here shows you exactly how this works https://github.com/cakedc/search#full-example-for-modelcontroller-configuration-with-overriding
public $filterArgs = array(
'some_related_table_id' => array('type' => 'value'),
'search'=> array('type' => 'like', 'encode' => true, 'before' => false, 'after' => false, 'field' => array('ThisModel.name', 'OtherModel.name')),
'name'=> array('type' => 'query', 'method' => 'searchNameCondition')
);
public function searchNameCondition($data = array()) {
$filter = $data['name'];
$cond = array(
'OR' => array(
$this->alias . '.name LIKE' => '' . $this->formatLike($filter) . '',
$this->alias . '.invoice_number LIKE' => '' . $this->formatLike($filter) . '',
));
return $cond;
}
Inside your custom method explode() the tags and make them an array so that CakePHP is using them as IN() or better (in can become slow) make it a chain of AND or OR.

how to create submenus in drupal 7

I have a module that adds a link to main menu. When I click on that link the requested page gets loaded (a .js and .html file).
My main menu looks like this:
My link
My code looks like this:
<?php
/**
* Implements hook_menu()
*/
function kl_menu(){
$items = array();
$items['simple_link'] = array(
'title' => t('my link'),
'page callback' => 'build_page',
'access arguments' => array('access content'),
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
/*
* build_page
*/
function build_page() {
drupal_add_js(drupal_get_path('module', 'kl') . '/mypage.js', 'file');
return ( file_get_contents( drupal_get_path('module', 'kl').'/mypage.html') );
}
Now I would like add a submenu instead of a simple plain link so that my main menu would look like this:
My submenu
my sublink1
my sublink2
I would like that when I click on "my submenu" then this sub menu expands displaying more links. Then when I reclick on my submenu I would like it to collapse.
I am pretty new to drupal php etc.
How can I acheive that. I am using garland theme.
Thanks
baba
/**
* Implements hook_menu().
*/
function kl_menu() {
$items['simple_link'] = array(
'title' => t('my link'),
'page callback' => 'kl_build_page',
'access arguments' => array('access content'),
'menu_name' => 'main-menu',
'type' => MENU_NORMAL_ITEM,
);
$items['simple_link/my_sublink_1'] = array(
'title' => t('my sub link 1'),
'page callback' => 'mymodule_sub_page_1',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['simple_link/my_sublink_2'] = array(
'title' => t('my sub link 2'),
'page callback' => 'mymodule_sub_page_1',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_theme().
*/
function kl_theme() {
$template_path = drupal_get_path('module', 'kl') . '/templates';
return array(
// File would be <module path>/templates/kl-build-page.tpl.php
'kl_build_page' => array(
'path' => $template_path,
'template' => 'kl-build-page')
),
// File would be <module path>/templates/kl-sub-page-1.tpl.php
'sub_page_1' => array(
'path' => $template_path,
'template' => 'kl-sub-page-1')
),
// File would be <module path>/templates/kl-sub-page-2.tpl.php
'sub_page_2' => array(
'path' => $template_path,
'template' => 'kl-sub-page-2')
),
);
}
/**
* Callback for main build page.
*/
function kl_build_page() {
drupal_add_js(drupal_get_path('module', 'kl') . '/mypage.js', 'file');
return theme('kl_build_page');
}
/**
* Page callback for sub page 1
*/
function kl_sub_page_1() {
return theme('kl_sub_page_1');
}
/**
* Page callback for sub page 2
*/
function kl_sub_page_2() {
return theme('kl_sub_page_2');
}

Drupal custom service (services module)

I have registered a service, when I call it, the call itself works, but the data returned is "false". I don't know what I am doing wrong, and I can't find info about it. Can anyone help me out please ?
function services_sso_server_extension_services_resources() {
return array(
'mia' => array(
'actions' => array(
'retrieve' => array(
//'help' => 'retrieves the corresponding user on the server',
'file' => array('type' => 'inc', 'module' => 'services_sso_server_extension', 'name' => 'resources/extension'),
'callback' => '_sso_server_retrieve',
'args' => array(
array(
'name' => 'id',
'type' => 'int',
'description' => 'The id of the note to get',
'source' => array('path' => '0'),
'optional' => TRUE,
),
),
'access callback' => '_sso_server_access',
'file' => array('type' => 'inc', 'module' => 'services_sso_server_extension', 'name' => 'resources/extension'),
'access arguments' => array('view'),
'access arguments append' => TRUE,
),
'action' => array(
//'help' => 'retrieves the corresponding user on the server',
'file' => array('type' => 'inc', 'module' => 'services_sso_server_extension', 'name' => 'resources/extension'),
'callback' => '_sso_server_get_action',
'args' => array(
array(
'name' => 'clientsid',
'type' => 'varchar',
'description' => 'The sid of the client to which the communication is bound',
'source' => array('path' => '0'),
'optional' => TRUE,
),
),
'access callback' => '_sso_server_access',
'file' => array('type' => 'inc', 'module' => 'services_sso_server_extension', 'name' => 'resources/extension'),
'access arguments' => array('view'),
'access arguments append' => TRUE,
),
),
),
);
}
I use this code somewhere else to call it:
function services_checkuser() {
global $user;
$sid = $user->sid;
$options = array(
'headers' => array(
'Cookie' => $_SESSION['gsid'],
'Accept' => 'application/json',
'clientsid' => $sid
),
'method' => 'POST',
);
$response = drupal_http_request('http://sso.server:8080/usercheck/mia/action', $options);
$data = json_decode($response->data);
}
Ok I found out how to post the data:
$data = array('gsid' => $gsid);
$options = array(
'headers' => array(
'Cookie' => $_SESSION['broker_sid'],
'Accept' => 'application/json',
),
'method' => 'POST',
'data' => http_build_query($data)
);

Taxonomy term page called by ajax link

I am testing to load taxonomy/term/1 page into a wrapper 'ajax-content', but can not figure out if this is the right thing:
drupal_add_library('system', 'drupal.ajax');
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
$link = array(
'#type' => 'link',
'#title' => t('Some Taxonomy'),
'#href' => 'taxonomy/term/1', //'taxonomy/term/1/nojs',
'#attributes' => array('class' => array('use-ajax')),
'#ajax' => array(
'callback' => 'taxonomy_term_page',
'wrapper' => 'ajax-content',
'method' => 'replace',
'effect' => 'fade',
),
);
print "<div id='ajax-content'></div>" . drupal_render($link);
This always returns a 200 error.
Any hint would be very much appreciated.
Thanks

Resources