I am doing some access to content using hook_form_alter as there are problems using hook_access with content types defined outside of your module. If a user does not have access I am setting a message at the top of the page and I don't want to output the form. I have the following code.
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'cmyformm':
dsm($form);
$from = null;
dsm($form);
drupal_set_message('You do not have access to this page');
break;
}
}
The dsm however is returning the same information for $form even if I set it to null. How do I not display the form but a message?
I don't quite understand the question, but for starters you are setting $fROm to null and not $fORm. Setting for to null or just doing unset($form); should do the trick.
Access control shouldn't be done at the form level. Access control should be implemented with hook_access or in the router's access arguments. If you must do this at the form level, use user_access() along with the defined roles that you have.
In addition to the typo as pointed out by #zeroFIG, you are really doing this access check in the wrong way. I have used the node_example module with sucess on all node types - none were defined in the same module as the hook_access. Have a look here: http://api.drupal.org/api/examples/node_example--node_example.module/6
Related
I'd like to send a file to everyone who gets my webform. This should be a hidden file that the person filling in the form doesn't need to attach but the receiver gets. Its for a job advert that someone fills in a request and then they get sent back a 'Thanks' message and they are asked to fill in a word document about equal opportunities.
I guess its probably doable with webform rules but I'm not getting any luck with that.
Write a custom module with hook_form_alter implementation, and add a custom submission callback to handle the file attachment.
This way you can handle form fields and values, before the webform values getting saved in database or getting sent via email.
Code sample:
function MODULE_form_alter(&$form, &$form_state, $form_id) {
if($form_id === 'YOUR_FORM_ID') {
// code as needed in here :)
// add another submission callback
$form['submit'][] = 'YOUR_NEW_FORM_SUBMISSION_CALLBACK';
}
}
function YOUR_NEW_FORM_SUBMISSION_CALLBACK($form, &$form_state) {
// code as needed here
}
My Drupal 6 installation has the php filter disabled so I can't use <?php ... ?> in the node itself.
I have a case where I need to run a little bit of PHP code on a small number of pages. Is there a way in Drupal 6 to create a module that will match a URL pattern and then before showing the page execute a function?
Specifically, on a few pages I need to process some data and then send an HTTP header. I know that I can create a custom .tpl file for these pages but putting application logic like this in a .tpl file feels like a hack.
If you want to do this specifically for node pages then you'd be better off implementing hook_nodeapi(). This would mean you don't have to perform a match based on the URL and you can add your header in the most 'structured' manner possible:
function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'view') {
drupal_set_header('some header');
}
}
If you need to do it for non-node pages then you'll want to implement hook_init() instead:
function MYMODULE_init() {
if ($_GET['q'] == 'node/1') { // or whatever path
drupal_set_header('some header');
}
}
Both of the hooks are invoked well before the headers are sent to the client so either way will work.
I'd like to attach autocomplete to a particular list of fields in Drupal 7. The fields have FIELD_CARDINALITY_UNLIMITED, so there could be anywhere from 1 to whatever. I'm using the following code:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if (array_key_exists('mymodule', $form)) {
$indices = array_filter(
array_keys($form['mymodule']['und']),
function($item) {
return is_numeric($item);
}
);
foreach($indices as $index) {
$form['mymodule']['und'][$index]['value']['#autocomplete_path'] = 'api/node/title';
}
}
}
...however, my autocomplete behavior is not being attached. I've used the exact same code in a similar situation - the only difference is that I was adding the autocomplete to a field that had a cardinality of 1 rather than unlimited. That doesn't seem like it should change anything. I've verified that the autocomplete is attaching by doing a debug($form['mymodule']) after the assignment statement, and it is definitely there. I have also debugged the exact array path I am trying to get in each iteration of the foreach loop, and it is definitely the correct form value.
EDIT: Is it possible that the issue is with more than one module altering this form using hook_form_alter()? I'm performing the exact same operation as above (but on a single field) in a different module, on the same form.
EDIT2: I've noticed that if I put a debug statement inside the foreach loop, I see the autocomplete value is set on the proper value each iteration. If I place the debug statement outside the foreach loop, the autocomplete path is no longer set. Somehow, either during the course of iteration, or after iteration, it looks like my changes are being destroyed? I tested this by assuming $index to be 0, and writing a hard-coded statement to attach autocomplete - this allowed auto complete to work correctly. To be clear, I am seeing something like the following:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if (array_key_exists('mymodule', $form)) {
$indices = array_filter(
array_keys($form['mymodule']['und']),
function($item) {
return is_numeric($item);
}
);
foreach($indices as $index) {
$form['mymodule']['und'][$index]['value']['#autocomplete_path'] = 'api/node/title';
// Debug statements here show that the value '#autocomplete_path' is set properly
debug($form)['mymodule']['und'][$index]['value']);
}
// Now, the '#autocomplete_path' key does not exist
debug($form)['mymodule']['und'][0]['value']);
// This will make autocomplete attach correctly
$form['mymodule']['und'][0]['value']['#autocomplete_path'] = 'api/node/title';
}
}
You've spelt it #autcomplete_path...it should be #autocomplete_path :)
If you're defining the field (and widget) yourself then you should just add the autocomplete in your module's implementation of hook_field_widget_form() rather than altering the form.
If you're not defining the widget yourself, take a look at hook_field_widget_form_alter() and hook_field_widget_WIDGET_TYPE_form_alter() which will let you alter the widget form for a specific field.
Try this:
1) change ['mymodule']['und'][$index]['value'] in your code to the id of your text form input example
$form['search_form_block']
['#autocomplete_path']='yourcall_back_function_which_returns_data';
I think the mistake is your are trying to work to replace the value of the the field but you have to change the value of the format widget. In this case the input field.
2) Also make sure 'api/node/title' call back works using x debug.
Let me know if it worked.
Cheers,
vishal
I resolved the problem by manually enumerating my indices rather than programmatically doing so, e.g. $form['mymodule']['und'][0]... - this appears to be a PHP issue related to scoping of variables in foreach rather than a Drupal problem.
In Drupal 6 you could use code similar to the following one:
function example_user($op, &$edit, &$account, $category = NULL) {
switch($op) {
case 'load':
$account->fb_id ='xyz'
break;
}
}
In Drupal 7, the documentation for hook_user_load() states the following:
Due to the static cache in user_load_multiple() you should not use this hook to modify the user properties returned by the {users} table itself since this may result in unreliable results when loading from cache.
Why do I get users and not just a user?
Is it ok to add properties to this?
http://api.drupal.org/api/drupal/modules--user--user.api.php/function/hook_user_load/7
You get an array of user objects because the hook is called from user_load_multiple(), which generally calls DrupalDefaultEntityController::load(), which then calls DrupalDefaultEntityController::attachLoad().
It is fine to add custom properties, but not to override the default properties that are loaded from the {users} table; as reported from the documentation, in that case you could get some problem when loading the user object from the cache, which is what the entity API normally does.
Edit
The basic question here is "when does the $op parameter get defined as 'search'"?
I am trying to create a custom search in an implementation of hook_search(). I have been looking through the Drupal documentation for the method here: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_search/6
I know the method is running because I can slip a die('killed inside of implementation of hook_search()') into the top of the function and see the output.
In the following code, the script is never killed so that I can see the output search caught inside of my_search(). This leads me to believe that the 'search' case of the switch statement is never firing. Does anybody know where I might go from here?
/**
* Implementation of hook_search()
*/
function my_search($op = 'search', $keys = NULL) {
switch($op)
{
case 'search':
die('search caught inside of my_search()');
break;
}
}
First things first.
Assuming your module is called 'my', try to go to URL /search/my/whatever - probably you will see access forbidden page (assuming you do not have anything more in your code besides what you have pasted in your question).
That's because you do not return anything when search module calls your hook with $op = 'name' (see _search_menu() in search.module). You need to return "a translated name defining the type of items that are searched for with this module ('content', 'users', ...)" - see http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_search/6 And access forbidden gone.
Once this is done, search will call your hook again (actually, there are quite a few calls, you can for example drupal_set_message($op) in your hook to see them all), and one of those calls will be with $op = "search" as well (coming from search_data() in search.module).