How to insert into database Drupal Custom Fields - drupal-7

I retrieve content types with the following code
$form['ct'] = array(
'#type' => 'checkboxes',
'#options' => node_type_get_names(),
'#title' => t('Hangi tür içerikler hakkında bilgi almak istersiniz?'),
);
And It gives the following output
http://i.stack.imgur.com/TOfS6.png
And when I press Create new Account button, the POST Array is so :
http://i.stack.imgur.com/BtxhC.png
My Question is How can I insert into database these values and read?

There are two ways of saving this data and associates with user ID ($user->uid).
Adding a new field for users from 'admin/config/people/accounts/fields'. Say the new field name is 'ct' and it is a list type field. In hook form_alter you can assign this options to 'ct'. Drupal will take care of saving the data.
You can create separate table to keep this information and use db_insert function in hook hook_user_update. To retrieve this information you need to use db_query in hook_user_load.

Related

Drupal7: real time link selected field to a text field in Drupal7

I'm creating a webform to build up a subscription form, on Drupal 7.65
Goal
What I need to do is: to select a role from a list, and automatically to display the associated name of that role, in a text field.
As I said, the name should be displayed into a not modifiable text field just below it.
Suppose valid, the following list (key => value)
Field: Department
business_manager|Business Manager
hr_consultant|Human Resources
training_developer|Training Developer
and from the time going on, the associated names, are respectively
Options can appear into text field hr_business_partner
Steve Abc
Gertrude Def
Sven Hgj Klm
Thus when the trainee selects "Human Resources", the name of "Gertrude Def" should appear into the text field below the select one.
I've attached a mokup to better understand what I do need.
IMPORTANT
I can't put the names into the list as value, because the association can change but old records should keep the previously registered associations
You can use hook_form_alter() and add a new select field with the paired key value list you need to the webform. And then use javascript to update which field value gets shown in the HR Business Partner field on change, which by the way would also need to be added via your hook_form_alter. You could use a taxonomy to maintain a list of Departments/Business partners which would populate your department and business partners.
Write some javascript to dynamically update your original fields not added through the form_alter, on change. I would suggest making two textfields in your webform components which will hold the value from your form alter added fields. So that these values selected by the user gets saved in your form.
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "webform_client_form_####"){
$form['#attached']['js'] = array(drupal_get_path('module','MODULENAME') . '/js/webform.js');
$form['hr_dept'] = array(
"#type" => "select",
"#options" => array("business_manager"=>"Business Manager", "hr_consultant"=>"Human Resources"),
);
$partners = taxonomy_get_tree(#); //the VID of the taxonomy
$list = array("0"=>"None"); //first option
foreach($partners as $tid => $partner){
$list[$partner->tid] = $partner->name;
}
$form['hr_partner'] = array(
'#type' => 'select',
'#options' => $list,
);
}
}
In your javascript file, /js/webform.js you can include all your logic to check for which value is selected on the Department field and then display the correct value in the Partners fields. At the same time, updating the original fields you've added as textfields in the webform components UI.

How to add multiple images to edit form using MongoDB in Sonata?

I'm trying to find out how I can have the edit action of a Document (Entity) that is using an array of Object IDs to store the ID of an image stored using GridFS. This is a one-to-many but using an array inside the Document, instead of a middle table.
So far I was able to add the following to my edit action:
Here is the code I've used in the NewsAdmin configureFormFields():
$formMapper
->with('Images Manager')
->add(
'cover_images',
CollectionType::class,
['required' => true, 'label' => 'Cover Images'],
[ 'admin_code' => 'admin.file' ]
)
I have a controller and an action (#Route("/image/{id}", name="show_image")) that handles the display of images in the frontend using MongoDB GridFS. It uses the IDs stored in this array:

Auto Increment number on SuiteCRM Accounts

I tried several answers from suitecrm forum And here. And I can't implement them to the present version of Suite CRM.
Here is the one I tried last and stuck for further clarification.
add a new file in custom/Extension/modules/yourmodule/Ext/Vardefs e.g. autoincrement.php with the following :
<?php
$dictionary['YOUR_MODULE']['fields']['NAME_OF_AUTO_INC_FIELD'] = array(
'name' => 'NAME_OF_AUTO_INC_FIELD',
'vname' => 'LBL_LABEL_OF_AUTO_INC_FIELD',
'type' => 'int',
'readonly' => true,
'len' => 11,
'auto_increment' => true,
'disable_num_format' => true,
);
?>
and also add unique index for the field in that file
<?php
$dictionary['YOUR_MODULE']['indices']['NAME_FOR_INDEX'] = array(
'name' => 'NAME_FOR_INDEX',
'type' => 'unique',
'fields' => array('NAME_OF_AUTO_INC_FIELD'),
);
?>
Run a Quick Rebuild and Repair in Admin -> Repair and execute the changes.
after that it shows an empty text box. There it iterates automatically, when new account saved. But I want to show the next auto increment number here in this Accounts page itself.
Instead of default value, I want to show the next auto value in the new Account form.
This isn't out the box behaviour - you'll need to add some customisations to do this.
I would create a new vardef auto_inc_preview which is a function type field. This can then be used to grab the largest number from the DB and display this + 1.
A possible issue with this would be that the number a user sees may not be the id that gets generated - for example if two or more people create an account at the same time.

Saving a model with no data in CakePHP (just create id in db)

My CakePHP Cart model/table has only one field: id.
It hasMany LineItems.
I am not having success saving the Cart model alone:
$this->Cart->create();
$this->Cart->save();
Or, by passing it a $data array structured as follows and using saveAssociated():
$data = array(
'Cart' => array(),
'LineItem' => array(
array(
'item_id' => $item_id,
'qty' => $qty,
'price_option_id' => $price_option_id
)
)
);
If I add a useless_field to the Cart table/model, and pass some data in it saves. So obviously the problem lies in my having a model with a table with just a single id field and not passing in any other data to save. It won't create what it must be assuming is an 'empty' record.
I have passed 'validate' => false into the saveAssociated call but it doesn't make a difference (and there are no validations for this model to ignore).
Is there a way to do this? Am I missing something? Please enlighten me!
CakePHP tables require created and modified fields, or for you to define your own fields (ie you can call them whatever you want but they do the same thing).
In this instance you would use
$this->Cart->set($data);
$this->Cart->saveAll();

How to diaplay other tables attributes in single view form and also insert values in corresponding tables

I have two tables QBQuestion(Questionid,Question,OptionId) and Option(OptionId,Option). I want to display option form on view form of QBQuestion? I want to create multiple choice question. i.e.for single question we can add multiple options.For such purpose i want to cretae option field with add button si that when we click add button,we can insert more options and also want to display that all inserted options in table using grid.
So what should i do? please help me....
1) Add relations in the model for this two stuff.
public function relations() {
return array(
'valOptions' => array(self::BELONGS_TO, 'Option', 'OptionId'),
);
}
2) Use lazy loading in CGridView.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => new CActiveDataProvider('QBQuestion'),
'columns' => array(
'Questionid',
'Question',
'valOptions.Option',
),
));
I think that's what you need.

Resources