views:

24

answers:

3

I am using form_alter to edit the submit function when editing content. In my custom function I wish to edit a custom message to the screen with the title name. I thought a way I could do this is something as follows

function mymodule_myfunction(&$form) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

The title is not being joined to the 'Some text'

I am calling my function by using the following line in my form_alter:

$form['#submit'][] = 'mymodule_myfunction';
A: 

Try changing the signature of your

function mymodule_myfunction(&$form) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

To:

function mymodule_myfunction($form, &$form_state) {
    drupal_set_message(t('Some text ' . $form['#node']->title));
}

Also try installing the devel module so you can do things like

dsm($form);
dsm($form_state);

And see exactly what you are dealing with.

Also, if all you want to do is give a message when a new node of type 'X' is created a better way is to use hook_nodeapi;

It could look something like this;

function modulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

if ($op == 'insert' && $node->type == 'my node  type') {
  drupal_set_message($node-title . ' is cool.');
}
}
DKinzer
drupal_set_message(t('PRISK Project '.$form->title)); did not seem to work. Will update question with how I am calling my function.
Linda
A: 

DKinzer recommended using dsm($form)to see the variables. The Node title is not populated. It can be found in the Post array. The following line allowed me to do what I wanted.

drupal_set_message(t('Some Text '.$form['#post']['title']));
Linda
This is an improper use of the t() function. See John Fiala's answer above for details on using an @title placeholder.
jhedstrom
+1  A: 

All submit functions get two parameters passed to them: $form, which is the final form array after all of the adjustments for hook_form_alter and the like, and $form_state which among other values contains the submitted values, which have been cleaned and checked for ranges. (For instance, if you have three items in a select box, the data in $form_state['values'] already has made sure that the value for that input is one of the three legal values.)

Generally, you shouldn't use $form['#post'] - it's not part of the published way to get at values, and an update to the core to handle some problem with FAPI could conceivably break your code.

Try this:

function mymodule_myfunction($form, &$form_state) {
  drupal_set_message(t('Some Message @title'), 
  array('@title' => $form_state['values']['title'])));
}

Note the corrected use of the t() function - the intent of that function is to allow other users to translate text, and so by using 'Some Message @title' the translator knows more about what is going on. Additionally you get the advantage that text fed through the t function in this way also is fed through check_plain(), which prevents someone from doing something malicious with the input.

John Fiala
What is using the @ sign called. I am not a native php developer so if I know the correct term I can do some more reading up on the subject.
Linda
Well, check out the link I made out of the t() function above - that links to the api page for t(), which states that we call them "placeholders".
John Fiala

related questions