Taxonomy term page called by ajax link - drupal-7

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

Related

Auth->login() function not working properly

Here's the component declaration in AppController.php>
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'code'),
'scope' => array('activated' => true),
),
),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'members', 'action' => 'dashboard', 'admin' => true),
'authError' => 'No Permission',
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'userScope' => array('User.activated' => true),
),
The login form:
<?= $this->Form->create('User', array('url' => '/users/login', 'class' => 'form-inline'));?>
<div class="form-group">
<?= $this->Form->input('User.email', array(
'div' => false,
'label' => false,
'placeholder' => 'е-пошта',
'class' => 'form-control',
'required' => true,
));?>
<?= $this->Form->input('User.code', array(
'div' => false,
'label' => false,
'placeholder' => 'сериски број',
'class' => 'form-control',
'required' => true,
));?>
<?= $this->Form->button('<i class="fa fa-user"></i>', array('type' => 'submit', 'class' => 'btn btn-primary', 'escape' => false));?>
</div>
<?= $this->Form->end();?>
And a snippet of the login function:
// ...
if($this->request->is('post')) {
if($this->Auth->login()) {
if(isset($this->request->data['User']['token']) && $this->request->data['User']['token']) {
$token = substr(md5(time()), 0, 32);
$this->User->id = $this->Auth->user('id');
$this->User->saveField('token', $token);
$this->Cookie->write('remember_me', $token, false, '1 week');
}
return $this->redirect($this->Auth->loginRedirect);
}
// ...
Now, when I use $this->Auth->login($this->request->data) or $this->Auth->login($this->request->data['User']), it works, but when I use only $this->Auth->login() it doesn't. I can do a workaround by logging in with $this->request->data and then putting the rest of the user data manually to be available afterwards, but I want to know why this happens. Any ideas?
EDIT
So, as Karthik Keyan mentioned hashing, i figured this was the problem. CakePHP was automatically hashing the password (code field) and I didn't want it to. So I made a custom hasher class named NoPasswordHasher as follows:
App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class NoPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
return $password;
}
public function check($password, $hashedPassword) {
return $password == $hashedPassword;
}
}
and used it in the Auth component:
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'code'),
'scope' => array('activated' => true),
'passwordHasher' => 'No',
),
),
It works now. Thank you.
Tell what type of errors can be display for you.Please check can you store the password in HASH (salt) format.

Drupal Submit handler is not called properly in custom module

I have this code for calling a form and than submit it..
<?php
// hook_menu
function pricepackages_menu()
{
$items = array();
$items['membership/packages'] = array(
'title' => t('Manage Membership Packages'),
'page callback' => 'drupal_get_form',
'page arguments' => array('pricepackages_form'),
//'access callback' => TRUE,
'access arguments' => array('access administration pages'),
);
return $items;
}
// FORM SHOW HOOK
function pricepackages_form($form, &$form_state)
{
$form = array();
$form['packagename'] = array(
'#type' => 'textfield',
'#title' => 'Package Name',
//'attribute' => array('class' => 'package'),
'#required' => TRUE,
);
$form['packageDescp'] = array(
'#type' => 'textfield',
'#title' => 'Package Short Description',
//'attribute' => array('class' => 'package'),
'#required' => FALSE,
);
$form['price'] = array(
'#type' => 'textfield',
'#title' => 'Package Price',
//'attribute' => array('class' => 'package'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['submit'][] = array('package_get_form'=> array());
return $form;
}
function package_get_form($form, &$form_state)
{
drupal_set_message('working');
?>
<script language="javascript">
alert("aaa");
</script>
<?php
return;
}
?>
but this one is not wokring proerly and form is not being submitted on the specific form...
neither its showing alert or message...
please help me...
This part is not correct:
$form['submit'][] = array('package_get_form'=> array());
To add a submit callback, simply write:
$form['submit'][] = 'package_get_form';
You don't even need this line since the form API provides a default callback appending "_submit" to the form id/callback. For your case:
pricepackages_form_submit()

form submission in Drupal

i have made a custom module for form in drupal using dupal API,and made a table in my database
What i want is to save the value of drop down box instead of it's index into database...my .module file is:
<?php
function form_test_menu() {
$items['formtest'] = array(
'title' => 'Form Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_form'),
'access callback' => TRUE,
);
return $items;
}
function form_test_form($form,&$form_submit) {
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['college'] = array(
'#title' => t('College/University'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['education'] = array(
'#title' => t('Education'),
'#type' => 'select',
'#description' => 'Select your higher education .',
'#options' => array(t('--- SELECT ---'), t('B.tech'), t('MCA'), t('MBA'),t('Graduate')),
'#required' => TRUE,
);
$form['percentage'] = array(
'#title' => t('Percentage/CGPA'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['application'] = array(
'#title' => t('Job Applied'),
'#type' => 'select',
'#options' => array(t('---select---'),t('Web Developer'),t('Web Designer'),t('SEO'),t('Marketing')),
'#required' => TRUE,
);
$form['submit'] = array(
'#value' => 'Submit',
'#type' => 'submit',
);
return $form;
}
function form_test_form_submit($form, $form_state) {
global $user;
// Here u can insert Your custom form values into your custom table.
db_insert('drupal')
->fields(array(
'name' => $form_state['values']['name'],
'college' => $form_state['values']['college'],
'education' => $form_state['values']['education'],
'percentage' => $form_state['values']['percentage'],
'application' => $form_state['values']['application'],
))->execute();
drupal_set_message("successfully saved data");
}
?>
All are in one file(.module)....any help will be appreciated.
Thank you.
The solution is to pass options array from one function to another. To achieve that use the second argument of "form_test_form" and "form_test_form_submit" functions.
function form_test_form($form,&$form_submit) {
$form_submit['myoptions'] = array(' select', 'option1' .....
}
Then
function form_test_form_submit($form,&$form_state) {
$myoptions = $form_state['myoptions']; etc ...
}

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.

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)
);

Resources