1

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';
Linda
  • 2,227
  • 5
  • 30
  • 40
  • check to see what the $form module really looks like. If you have the devel module installed you can do something like dsm($form) to see that variable. – dkinzer Sep 08 '10 at 16:00

3 Answers3

1

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
  • 4,561
  • 3
  • 30
  • 26
  • 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 Sep 09 '10 at 07:35
  • 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 Sep 13 '10 at 16:17
0

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
  • 32,179
  • 12
  • 66
  • 85
  • drupal_set_message(t('PRISK Project '.$form->title)); did not seem to work. Will update question with how I am calling my function. – Linda Sep 08 '10 at 15:58
0

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']));
Community
  • 1
  • 1
Linda
  • 2,227
  • 5
  • 30
  • 40
  • This is an improper use of the t() function. See John Fiala's answer above for details on using an @title placeholder. – jhedstrom Sep 08 '10 at 16:46