Pass arguments to a view in Drupal 6 via custom module - drupal-6

I'm using Drupal 6 to run a gallery I've created. I need to take a parameter from the AJAX request lets say "food" and pass that argument to a view I've created (Views 2) where "food" is a taxonomy term that I am using to get the data I want in return. Everything is working just fine and in my module's method for loading the view I can load the entire view because in the settings you have 'if no argument get all values', but I can't seem to pass arguments to it. Here is the method...
function ajax_methods_menu()
{
$items = array();
$items['admin/settings/ajax_methods'] = array(
'title' => t('AJAX Methods settings.'),
'description' => t('Define settings for the AJAX Methods'),
'page callback' => 'drupal_get_form',
'page arguments' => array('ajax_methods_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM
);
$items['gateway'] = array(
'title' => 'AJAX Gateway',
'page callback' => 'ajax_methods_get_items',
'type' => MENU_CALLBACK,
'access arguments' => array('access content')
);
return $items;
}
function ajax_methods_get_items($args)
{
$content = views_get_view('All_Images');
return drupal_json(array('status' => 0, 'data' => $content->preview('default')));
exit;
}
In looking at the documentation views_get_view() doesn't seem to allow for arguments although I believe they are being passed to my ajax_methods_get_items() method. Thanks for reading!

Got it figured out, I needed to add
return arg(1);
seems to be working pretty well.

Related

How to make a block visible only for admin and teacher in moodle?

I think there are more than one way to make block invisible for students.
1.
Hide the block
2.
Assign role to block and set permission to block
But these are done by admin by change the settings. I need a way by code. How can I write the code to make the block invisible for student.
For activity I can make invisible the activity by changing db/access.php
'mod/questionbank:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'legacy' => array(
//'guest' => CAP_ALLOW,
//'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'admin' => CAP_ALLOW
)
),
Like this how can I make the block invisible for student by code.
EDIT
according to Davosmith's answer.
I put inside get_content function
if (!has_capability('blocks/blockname:view')) {
return null;
}
in blocks/blockname/block_blockname.php
and in my blocks/blockname/db/access.php contain:
'blocks/blockname:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_BLOCK,
'legacy' => array(
//'guest' => CAP_ALLOW,
//'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
// 'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
But it results in error page saying
Coding error detected, it must be fixed by a programmer: PHP catchable
fatal error
For any block, if get_contents returns null (and editing is off), the block will not be displayed.
So, put the following in the get_content function of your block (but put in a real capability that you define in db/access.php):
if (!has_capability('block/myblock:somecapability', $this->context)) {
return null;
}

Why am I getting a page not found error after creating a new menu item?

In my module file I created a new menu item
function xmlproject_menu()
{
$items = array();
//more items here
$items['system/xml/cfa/initialize/%/%/%/%/%'] = array(
'page callback' => 'xmlproject_initialize_cf',
'page arguments' => array(4, 5, 6, 7, 8,),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function xmlproject_initialize_cf($session_id, $cart_id, $pid, $rid, $partner_id)
{
//some code here
}
I have tried going to admin/build/modules, devel/menu/reset, and admin/settings/performance to clear the cache. I can see the menu item in the database (menu_router).
When I go to http://example.com/system/xml/cfa/initialize/1/2/3/4/5 I am getting "Page not found".
You code seems all dandy, but I suppose your page callback "xmlproject_initialize_cf" should actually return something.
Try this:
function xmlproject_initialize_cf($session_id, $cart_id, $pid, $rid, $partner_id)
{
// Your Code
return 'Hello world!';
}
Is the modules name "xmlproject"?
Does not seems anything wrong with your code.
Just curious, why you have kept the last element of array as 'empty' (a comma after number 8)
'page arguments' => array(4, 5, 6, 7, 8,),
Also, there is additional empty item in the array (extra comma after MENU_CALLBACK)
'type' => MENU_CALLBACK,
As you see in your database number_part column that contains Number of parts in router path,sets to 7(maximum available part),but your parts of menu callback is 9.Which is more than MENU_MAX_PARTS available in drupal 6. this is why you're getting Page not found
Just reduce your menu item size and you are good to go.for example:
$items['initialize/%/%/%/%/%'] = array(
'page callback' => 'xmlproject_initialize_cf',
'page arguments' => array(4, 5, 6, 7, 8),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);

Restrict sonata_type_model_list to 1 context

I am trying to allow users to select from existing images in the system and have context set like the provided example and this works. How can I stop the users from selecting from any other context type?
$form->add('image', 'sonata_type_model_list', array(), array('link_parameters' => array('context' => 'news')))
Not tested but I just stumbled over the parameter: hide_context
$form->add('image', 'sonata_type_model_list', array(), array(
'link_parameters' => array(
'context' => 'news',
'hide_context' => true
)
))
Maybe you want to give it a try?

Changes in module_menu() aren't applying

I have a custom module called tf_partner. I just modified this to include another argument. Inside the module I have:
function tf_partner_menu()
{
//... more code.....
$items['partner/letters/word/replace/%/%/%/%'] = array(
'page callback' => 'tf_partner_replace_image',
'access callback' => TRUE,
'page arguments' => array(4, 5, 6, 7),
'type' => MENU_CALLBACK,
'file' => 'tf_partner_letters.inc.php',
);
//...more code
}
This is what it used to be ...
function tf_partner_menu()
{
//... more code.....
$items['partner/letters/word/replace/%/%/%'] = array(
'page callback' => 'tf_partner_replace_image',
'access callback' => TRUE,
'page arguments' => array(4, 5, 6),
'type' => MENU_CALLBACK,
'file' => 'tf_partner_letters.inc.php',
);
//...more code
}
function tf_partner_replace_image($aid, $letter, $position, $randstr)
{
echo "here"; //doesn't show up
}
The problem is when I try to go to the new url, it doesn't work and goes to /partner/letters/word. I already went to admin/build/modules, but still getting the problem. I checked the database (menu_router table) and it looks correct.
EDIT: Also, I created a new function similar to this and a new menu item. I started with 1 argument and added one at a time. It allowed me up to 3. When I added the 4th one, it didn't work anymore.
clear cache on admin/settings/performance
or by emptying cache_{name} tables in database.
code looks fine, I guess menu just didn't get rebuilt.

Drupal Profile alter: hook_form_alter

After reviewing some posts here and elsewhere, I still can't seem to manually add a select field to the profile. (I need the select list to be populated with a SQL query, not supported with core profile module.)
So far, I am trying two different ways: hook form alter ($form_id == 'user-register' & hook user ($op == 'register') -- but I can't even get the field to appear in the registration form.
function accountselect_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'register'){
$fields['account_select'] = array(
'#type' => 'fieldset',
'#title' => t('Your Security Question')
);
$fields['account_select']['account_name'] = array(
'#type' => 'select',
'#default_value' => 'Select',
'#description' => t('Select a verification question in case you forget your password'),
'#options' => array(t('Select One'),
t('Where you attended Elementry School'), t('Your Best Man'))
);
return $fields;
}
Here is the hook form alter attempt
function accountselect_form_alter(&$form, $form_state, $form_id){
if($form_id == 'user-register') {
$form['account_select']['account_name'] = array(
'#type' => 'select',
'#title' => t('Account'),
'#description' => t('Enter the account to which the contact belongs.'),
'#options' => array(t('Account1'),t('account2'), t('account3')),
'#default_value' => $edit ['Account']
);
}
return $fields;
}
Sorry Guys, the code here is correct. I did a little debugging when the module was first enabled. I thought I had successfully fixed the problem, but what really happened is that the module became disabled. So, no matter what was in there, it would have had no effect....
No worries, I've punched myself in the face for the stupid question....

Resources