How to populate url arguments using hook_form_alter in Drupal? - drupal

I'am trying to develop a Custom drupal module which will read arguments from the URL and populate the form fields accordingly.
I was successful in doing that for title filed but i was unable to that for body field and a custom cck field, i named field_url
here is my code
function formexample_form_alter(&$form, &$form_state, $form_id) {
// This code gets called for every form Drupal builds; use an if statement
// to respond only to the user login block and user login forms.
if ($form_id == 'bookmark_node_form') {
$form['title']['#value'] = $_GET['x'];
$form['field_url']['#value'] = $_GET['y'];
$form['body']['#default_value'] = $_GET['x'];
}
}
here is my url i am trying to enter http://localhost:8082/acquia-drupal/node/add/bookmark?x=hjsajskajsjasa&y=asasasasas
title field is getting populate with the value of x from the url arguments but other fields don't.

You might look to the Prepopulate module for inspiration, if it doesn't achieve your needs on its own.

Related

How to write a code to get data from one form to another form by using button?

I have created a command button in the CustGroup form action pane.
I have added a new base enum edt field to both the CustGroup and CustTable tables and forms.
When you click on the button the data that was previously changed in the CustGroup table must be reflected in the cust table form.
I have written code in button on click event handler but it's not updating.
What to do, any suggestions?
If I understand your question correctly, you want to transfer a change of a new field in a customer group to all customers that share this customer group.
This kind of mass data update is usually not done by code in a form, because that code is executed on the client tier, which results in a bad performance. Instead, you should create a class that is set to execute on the server tier. If you create a main method for this class, you can easily create an action menu item for it, which let's you easily integrate the call to this class as a button in the CustGroup form.
In the main method you can access the CustGroup record for which the button was clicked via the Args object. This gives you the value of your new field that was changed. With this value, you can then use code similar to the following to update your customers:
public void updateCustomersWithNewCustGroupFieldValue(CustGroup _custGroup)
{
CustTable custTable;
ttsBegin;
while select forUpdate custTable
where custTable.CustGroup == _custGroup.CustGroup
{
custTable.MyNewEnumField = _custGroup.MyNewEnumField;
if (custTable.validateWrite())
{
custTable.update();
}
else
{
error('Please implement some error handling');
}
}
ttsCommit;
}

How to attach a file after a webform has been submitted

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
}

ASP.NET MVC - Preserve POST data after Authorize attribute login redirect

I have a blog post page with comments.
Any user (logged in or not) can see a form at the bottom of the page to post a comment.
When user enters the comment and she is not authorized - the user is redirected to a login/signup page.
After logged in, the user is redirected back to the action, but the POST data, containing the comment body, is lost.
I use the ASP.NET MVC Authorize attribute to require authorization on some actions:
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Create(int blogPostID, string commentBody) {
var comment = new Comment {
Body = commentBody,
BlogPostID = blogPostID,
UserName = User.Identity.Name
}
// persist the comment and redirect to a blog post page with recently added comment
}
How do you solve this problem?
Making user loggin before displaying the comment form is a bad idea here I think.
Thanks.
I would probably just save off the siteId and comment into the Session. Then create another overload for Create that doesn't take any parameters. It checks to see if these variables exist in the session - if so, pass it off to your original Create method.
To do that, you'd have to remove the Authorize attribute and just do the security check yourself. Something like this:
var user = HttpContext.User;
if (!user.Identity.IsAuthenticated)
{
Session["Comment"] = comment;
Session["SiteId"] = siteId;
return RedirectToAction("LogOn", "Account",
new { returnUrl = "/ControllerName/Create"} );
}
Then your overloaded Create:
public ActionResult Create()
{
var comment = (Session["Comment"] ?? "").ToString();
int siteId = 0;
if (Session["siteId"] != null)
siteId = (int)Session["siteId"];
return Create(siteId, comment);
}
Of course, this isn't really all that generic and doesn't handle more complex scenarios, but it's an idea. (hopefully the above code works, I haven't had a chance to test it). It seems like you could maybe do something like this via an action filter but I don't have any sample code for that.
You can use hidden field on your authorization form. Put your user's comment to that field (your initial POST data). After that you still can not use the data on your comment form if authorization form simply redirects to your comments form. So make your authorization form post to comments form, data in hidden field will be posted also, so you can use it.

Running PHP code on certain Drupal nodes

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.

hook__form_alter resetting &$form

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

Resources