Populate Webform hidden field with title of referring node - drupal-7

Drupal 7
I'm having a similar problem to one that's been presented previously but so far I've not been able to make any of the suggestions work.
I have 'Product' pages of content type 'Software Products'. I want to place a link on the product pages pointing to a Webform 'Request Information' I want to populate a (hidden) field on the form with the product name which is also the title of the referring product page.
I have tried the following but this just results in the title of the form being shown - not the referring page.
<?php
/**
* Implementation of hook_form_alter().
*/
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
switch($form_id) {
case 'webform_client_form_10': // the id of the form
{$current_object = menu_get_object();
$product_title = $current_object->title;
$form['submitted']['product']['#default_value'] = $product_title; }
return $form;
}
}
I would appreciate any pointers - I'm new to Drupal

That's quite a messy way round of doing what you need to, you should just put the product nid in the URL as part of the query string in the link from your product page and then load it up from the webform.
In your node template/preprocess:
$webform_path = 'node/10'; // Or whatever the webform's nid is
$link = l('Request Information', $webform_path, array(
'query' => array(
'product_nid' => $product_node->nid
)
));
echo $link;
Then in your form alter:
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'webform_client_form_10' && isset($_GET['product_nid']) && is_numeric($_GET['product_nid'])) {
$product_node = node_load($_GET['product_nid']);
if ($product_node) {
$product_title = $product_node->title;
$form['submitted']['product']['#default_value'] = $product_title;
}
}
}
Note that you don't return the form from the hook_form_alter function, the $form variable is passed in by reference so changes are stored that way.

Related

Detect change in date field suitecrm

I'm trying to detect when the date has changed inside a regular "date" field
Have seen a few forum posts about how this can be done with text fields and dropdown, but it doesn't work for date fields.
Also tried:
'displayParams' =>
array (
'javascript' => 'onchange="checkStatusOption(this)"',
),
In the editviewdefs.php, but it didn't work (works for text fields though)
The closest I've got it actually just tracking every click on on the screen and then checking the before and after state of the date field, but it's obviously not a very elegant solution
Here's the code from the extended editview
function display() {
parent::display();
$js = <<<EOT
<script type="text/javascript" language="JavaScript">
// Capture initial state
calendar_before = document.getElementById("contract_date_c").value;
// Wait for any click to take place anywhere on the screen
$(document).click(function() {
// Capture state after we clicked somewhere
calendar_after = document.getElementById("contract_date_c").value;
// Compare the before and after
if(calendar_before != calendar_after) {
// Change detected
alert("Something's changed eh?" + calendar_before +" "+ calendar_after);
}
// Set the new state of the before_calendar
calendar_before = document.getElementById("contract_date_c").value;
});
}
</script>
EOT;
// now I output the javascript
echo $js;
}
UPDATE:
I also tried the suggested solution
1) Created a file custom/modules/un_inventory/contract_date_c_change.js and put the following inside:
function yourCustomFunction(formElement){
console.log(formElement);
}
2) Included a reference to that file in the metadata file (made sure it loads it):
array (
'file' => 'custom/modules/un_inventory/contract_date_c_change.js',
),
3) Attached the updateCallback to the field:
array (
'name' => 'contract_date_c',
'label' => 'LBL_CONTRACT_DATE',
// Checks if this field got filled up and shows hidden form field
'displayParams' =>
array (
'updateCallback' => 'yourCustomFunction();',
),
),
But nothing happens when I change that date field
Check this out (just tested in SuiteCRM 7.11) for a datetime field, for other fields take a look at this answer to another SO question
First, include your custom JS in the editviewdefs.php (example for Accounts module)
'includes' =>
array (
0 =>
array (
'file' => 'modules/Accounts/Account.js',
'file' => 'custom/modules/Accounts/myCustomFile.js',
),
),
Create the custom JS file custom/modules/Accounts/myCustomFile.js .
function yourCustomFunction(formElement){
console.log(formElement);
}
Then update the field you want to monitor for changes (contractsigned_c in the example) using the following code in the editviewdefs.php:
array (
'name' => 'contractsigned_c',
'label' => 'LBL_CONTRACTSIGNED',
'displayParams' =>
array (
'updateCallback' => 'yourCustomFunction(this)',
),
),
Now do a Repair and Rebuild inside the Admin/Repair section and voilĂ  it should work :)
You can add the JS function on the function display() if you want, its the samething, the function will be called right after native combo update. It will look like this combo_contractsigned_c.update(); yourCustomFunction(this)
use this . it is working
YAHOO.util.Event.addListener('your_field_name', 'change', function(){
// your code here
});

How do you set a scenario when doing a restful call in Yii2 to return certain fields

Im currently making a Yii2 RESTful system with AngularJs.
In my database i've got several columns that i want to be able to return when doing a particular call from a certain point in my system.
The problem i'm having is how do i return only a handful of fields eg(id, title and stub) from the restful call in another part of my system so that it ignores other fields in the table.
I would ideally like it to work in a similar way to how a Models rules work with scenarios in yii.
There are two methods, I think:
1. use params
// returns all fields as declared in fields()
http://localhost/users
// only returns field id and email, provided they are declared in fields()
http://localhost/users?fields=id,email
// returns all fields in fields() and field profile if it is in extraFields()
http://localhost/users?expand=profile
// only returns field id, email and profile, provided they are in fields() and extraFields()
http://localhost/users?fields=id,email&expand=profile
2. overriding model's fields()
// explicitly list every field, best used when you want to make sure the changes
// in your DB table or model attributes do not cause your field changes (to keep API backward compatibility).
public function fields()
{
return [
// field name is the same as the attribute name
'id',
// field name is "email", the corresponding attribute name is "email_address"
'email' => 'email_address',
// field name is "name", its value is defined by a PHP callback
'name' => function () {
return $this->first_name . ' ' . $this->last_name;
},
];
}
// filter out some fields, best used when you want to inherit the parent implementation
// and blacklist some sensitive fields.
public function fields()
{
$fields = parent::fields();
// remove fields that contain sensitive information
unset($fields['auth_key'], $fields['password_hash'], $fields['password_reset_token']);
return $fields;
}
more detail, refer to https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-resources.md
You may use scenarios method inside your model for this, but you will have to extend a bit toArray method in order to make it work properly:
public function scenarios()
{
return array_merge(parent::scenarios(), [
'simple_info' => [
'email',
'name',
],
'login' => [
'id',
'email',
'name',
'auth_token',
],
]);
}
public function toArray(array $fields = array(), array $expand = array(), $recursive = true)
{
$scenarios = $this->scenarios();
$scenario = $this->getScenario();
if (!empty($scenarios[$scenario])) {
$data = parent::toArray($fields, $expand, $recursive);
return array_intersect_key($data, array_flip($scenarios[$scenario]));
}
return parent::toArray($fields, $expand, $recursive);
}
After this you may simply do something like this:
$model = new LoginForm();
if ($model->load(Yii::$app->request->post(), '') && $model->login()) {
$user = $model->getUser();
// Lets change scenario to login in order to get `auth_token` for authorization
$user->setScenario('login');
$user->generateAuthKey();
$user->save(FALSE);
return $user;
} else {
return $model;
}
As a side note (expanding on the answer from #Ganiks), if you are manually returning the list of Models, you will need to return them as a DataProvider (rather than simply as an array of Models) for the fields parameter to have an effect.
For example, if you do something like this...
class UserController extends yii\rest\Controller
{
public function actionIndex()
{
return User::find()->all(); // Not what you want
}
// ...
}
... then the fields parameter will not have the desired effect. However, if you instead do this...
class UserController extends yii\rest\Controller
{
public function actionIndex()
{
return new ActiveDataProvider([
'query' => User::find(),
'pagination' => false,
]);
}
// ...
}
... then the returned fields will only be those you specified in the fields parameter.

Cannot render submit button

I've customized the edit profile view using template.php and user-profile-form.php
All shows up correctly but the Submit (and Delete) button..
I'm using Adaptive Theme and I've modified like this :
template.php
function adaptivetheme_theme(&$existing, $type, $theme, $path) {
return array(
'user_profile_form' => array(
'template' => 'templates/user-profile-form',
'render element' => 'form',
),
);
}
function adaptivetheme_preprocess_user_profile_form(&$vars) {
$vars['form']['account']['name']['#description'] = t('blabla');
$vars['form']['submit']['#value'] = t('Save profile');
$vars['form']['delete']['#value'] = t('Delete account');
$vars['account'] = drupal_render($vars['form']['account']);
$vars['theme_select'] = drupal_render($vars['form']['theme_select']);
$vars['picture'] = drupal_render($vars['form']['picture']);
$vars['signature_settings'] = drupal_render($vars['form']['signature_settings']);
$vars['contact'] = drupal_render($vars['form']['contact']);
$vars['timezone'] = drupal_render($vars['form']['timezone']);
$vars['submit'] = drupal_render($vars['form']['submit']);
$vars['delete'] = drupal_render($vars['form']['delete']);
}
then in the user-profile-form.tpl.php :
<div id="user-profile-form">
<?php echo $account; ?>
<?php echo $timezone; ?>
<?php echo $submit; ?>
<?php echo $delete; ?>
</div>
The edit form of the account shows correctly. I've tried adding/removing variables successfully (ie the $timezone) but the submit/delete are missing.
I don't know what's wrong..
I've tried to change the name of the variables 'submit' and 'delete' but still no button shows up. Of course I've cleared the cache every times needed (and not).
I have no JS hiding the buttons neither..
I render this form through a custom block in a Panel :
<?
module_load_include('inc', 'user', 'user.pages');
global $user;
print drupal_render(drupal_get_form('user_profile_form', $user));
?>
Maybe a problem with Panels ???
Any idea is appreciated :)
Thx for reading
Erwan
I forgot the "[action]".. :
$vars['submit'] = drupal_render($vars['form']['actions']['submit']);
$vars['cancel'] = drupal_render($vars['form']['actions']['cancel']);
And 'Delete' button didn't show up at first because it's called "cancel", and its #access param was sent to FALSE. Thx DPM ;)
Now, the problem is that when I trigger the submit button, the form is not sent, it justs reload he page. I will update if I manage to solve that too.
The page is only reloading because you forget to render the hidden form elements. To do so in your template preprocess you can use something like that :
function THEME_preprocess_user_profile_form(&$variables) {
$hidden = array();
foreach(element_children($variables['form']) as $key)
{
$type = $variables['form'][$key]['#type'];
if($type == "hidden" || $type == "token"){
$hidden[] = $variables['form'][$key];
}
}
$variables['hidden'] = $hidden;
//Dont forget to report your variables like you already did ...
}
Then when it s done render the $hidden variable in your template file
<?php print render($hidden);?>
And there you go !

Drupal 7 hook_form_FORM_ID_alter() Submit

I am developing a custom module and used hook_form_FORM_ID_alter method. I have provided option same as in block's configuration visibility settings for specific pages. I have also created a database table for my module. I am not getting any idea how the options selected by user should be stored in my table when submitted and also how the option selected for a particular menu link should be retained. I have added this functionality on menu item edit form.
Something like this is what you're looking for:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
$form['my_val'] = array(
'#type' => 'textfield',
'#title' => 'Some Text'
);
$form['#submit'][] = 'MYMODULE_my_form_submit';
}
function MYMODULE_my_form_submit(&$form, &$form_state) {
$val = $form_state['values']['my_val'];
db_insert('my_table')->fields(array('val' => $val))->execute();
}
That's obviously a very basic example but it shows you how to add a submit handler to an existing form, how to add an extra field, and how to then get the data for that field in the submit handler.

on update it inserts another row

I check for data below and if data is there in the form field it updates the table. Problem is that if I click the button twice (its ajax) it wll insert a second row. How can I prevent it.
thanks
function update() {
if (!empty($this->data)) {
$this->Test->saveAll($this->data['Test'])
}
}
The problem is the form doesn't have the id field so cakephp insert a new row.
The easy way is check if exists another record with the same values before save.
You can check in function beforeSave() of the Model instead of the Controller action.
http://book.cakephp.org/#!/view/1052/beforeSave
You should take a two-pronged approach to preventing this:
First, Disable the submit button on submit. For example in jQuery:
$('#form_id').submit(function(event) {
event.preventDefault();
$('input[type=submit]', this).attr('disabled', 'disabled');
// submit form via ajax...
}
Secondly, perform rational check server-side to prevent duplicate saves. For example, two identical records submitted within a minute or so would be considered an error. I'd probably do this by creating a custom validation rule on one of the fields that's always required. In your model:
var $validate = array(
'myfield' => array(
'rule' => 'duplicateCheck',
'message' => 'Duplicate record'
)
);
function duplicateCheck() {
$conditions = array(
'Model.field1' => $this->data['Model']['field1'],
...
'Model.created >' => date("Y-m-d H:i:s", time() - 60)
);
return !$this->hasAny($conditions);
}
Then in your controller, I would redirect the user to the existing record if the save fails and it was because of that specific validation rule. This let's the user move along without caring that anything went wrong.
function add() {
if(isset($this->data)) {
if($this->Model->save($this->data)) {
...
} else {
if(isset($this->Model->validationErrors['myfield'])) {
// find the existing record and redirect user to view it
}
}
}
}

Resources