Drupal - Set default value in hook_form_alter? - forms

Trying to prepopulate some of my form fields, and am using hook_form_alter(). I've tried a couple of different ways, but in both cases, the fields still come up empty. I'm assuming that I need to set default_value and not value because if the user changes what's in the field, I want that to update correctly. Is that right?
Here's what I've been trying:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'user_profile_form') {
if(arg(0) == 'user' && arg(1)) {
$user = user_load(arg(1));
$form['profile_company_site']= array('#default_value' => $user->profile_company_site);
$form['profile_blog_url']= array('#default_value' => $user->profile_blog_url);
$form['profile_my_website_url']= array('#default_value' => $user->profile_my_website_url);
$form['profile_first_name']= array('#default_value' => $user->profile_first_name);
$form['profile_last_name']= array('#default_value' => $user->profile_last_name);
}
}
}
I also tried it this way:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'user_profile_form') {
if(arg(0) == 'user' && arg(1)) {
$user = user_load(arg(1));
$form['profile_company_site'][#default_value'] = $user->profile_company_site);
$form['profile_blog_url'][#default_value'] = $user->profile_blog_url);
$form['profile_my_website_url']['#default_value'] = $user->profile_my_website_url);
$form['profile_first_name']['#default_value'] = $user->profile_first_name);
$form['profile_last_name']['#default_value'] = $user->profile_last_name);
}
}
}

Both seem to be in the correct format, but your first one will overwrite all the other items you set for the field. So you are better going off with the second and adding them piece meal.
On the second one, you are missing some single quotes on a couple.
$form['profile_company_site']['#default_value'] = $user->profile_company_site);
Are you sure you are getting into loop?

You are missing one array level. The profile form fields will not be at the top level in the $form array, but in a subarray keyed by the category name. So if you assigned your fields a category of 'example category', your code should look like this:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'user_profile_form') {
if(arg(0) == 'user' && arg(1)) {
$user = user_load(arg(1));
$form['example category']['profile_company_site']['#default_value'] = $user->profile_company_site);
$form['example category']['profile_blog_url']['#default_value'] = $user->profile_blog_url);
$form['example category']['profile_my_website_url']['#default_value'] = $user->profile_my_website_url);
$form['example category']['profile_first_name']['#default_value'] = $user->profile_first_name);
$form['example category']['profile_last_name']['#default_value'] = $user->profile_last_name);
}
}
}
You should use a debugger (or at least a var_dump()) to inspect the form array you want to manipulate - saves a lot of time.

Related

Drupal 8 views ui : filter on custom field

I m pretty new to D8 and I m trying to do the following thing :
I have a content type : movie.
In that content type I have a custom field api_id wich is simply an integer.
When I am on a movie page I want to display under the content a block with movies with the same api_id.
I have managed to create block for the same movies from the same author but I can't figure out how to to filter on api_id (I have played so much with contextual filters ...)
Any ideas ? Thx
Ok , I manage to do what I want with hook_views_query_alter() :
function my_module_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query)
{
if($view->id() == 'my_view' && $view->current_display == 'my_block'){
$movie= Node::load($view->args[0]);
if(is_object($movie)) {
foreach ($query->where as &$condition_group) {
foreach ($condition_group['conditions'] as &$condition) {
if ($condition['field'] == 'node__field_id_movie.field_id_movie_value') {
$condition = array(
'field' => 'node__field_id_movie.field_id_movie_value',
'value' => $movie->get('field_id_movie')->value,
'operator' => '=',
);
}
}
}
}
}
}
To do that you have to first create a view with a simple filter on the field you want to override the filter.

How to remove "-Year" option in views date exposed filter dropdown?

Can't alter it's values with hook_form_alter because element of type date_select doesn't have #options array.
['#options'] array available in form validation callback. To overwrite form element make sure you passing $form argument by reference:
function MY_MODULE_form_views_exposed_form_alter(&$form, &$form_state) {
$view = $form_state['view'];
if ($view->name == 'press_center' && $view->current_display == 'page') {
$form['#validate'][] = 'MY_MODULE_date_filter_validate';
}
}
function MY_MODULE_date_filter_validate(&$form, &$form_state) {
$form['year_month']['value']['month']['#options'] = array(1=>'Jan',2=>'Feb',3=>'Mar');
}

Is this a valid way to check if db_row exists?

I am working with Zend and I needed to check whether a row in the DB already exists (A simple solution to get rid of the duplicate key error I was getting). I tried several things but nothing seemed to work... (for example the Zend_Validate_Db_NoRecordExists method)
So I wrote the following the code and I was wondering if this is a valid way to do it, or if I should do things differently:
In the model:
$where = $condition = array(
'user_id = ' . $user_id,
'page_id = ' . $page_id
);
$check = $this->fetchRow($where);
if(count($check) > 0) {
return null;
}else{
// Here I create a new row, fill it with data, save and return it.
}
And then in my view:
if($this->result != null) { /* do stuff */ }else{ /* do other stuff */ }
It does work but it does seem to take more time (duh, because of the extra query) and I am a bit unsure whether I should stick with this..
Any recommendation is welcome :)
Assuming you have coded your function in your controller
$row = $this->fetchRow($where); //If no row is found then $row is null .
if(!$row)
{
$row = $dbTb->createNew($insert); //$insert an associative array where it keys map cols of table
$row->save();
$this->view->row_not_found = true;
}
return $row;
In your view you can do this
if($this->row_not_found)
{
}else {
}

Symfony: exclude empty values from form save

I have a many to many relation between Product and Properties. I'm using embedRelation() in my Product form to edit a Product and it's Properties. Properties includes images which causes my issue. Every time I save the form the updated_at column is updated for file properties even when no file is uploaded.
Therefore, I want to exclude empty properties when saving my form.
I'm using Symfony 1.4 and Doctrine 1.2.
I'm thinking something like this in my ProductForm.class.php, but I need some input on how to make this work.
Thanks
class ProductForm extends BaseProductForm
{
public function configure()
{
unset($this['created_at'], $this['updated_at'], $this['id'], $this['slug']);
$this->embedRelation('ProductProperties');
}
public function saveEmbeddedForms($con = null, $forms = null)
{
if (null === $forms)
{
$properties = $this->getValue('ProductProperties');
$forms = $this->embeddedForms;
foreach($properties as $p)
{
// If property value is empty, unset from $forms['ProductProperties']
}
}
}
}
I ended up avoiding Symfony's forms and saving models instead of saving forms. It can be easier when playing with embedded forms. http://arialdomartini.wordpress.com/2011/04/01/how-to-kill-symfony%E2%80%99s-forms-and-live-well/
Solved it by checking if posted value is a file, and if both filename and value_delete is null I unset from the array. It might not be best practice, but it works for now.
Solution based on http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms
class ProductPropertyValidatorSchema extends sfValidatorSchema
{
protected function configure($options = array(), $messages = array())
{
// N0thing to configure
}
protected function doClean($values)
{
$errorSchema = new sfValidatorErrorSchema($this);
foreach($values as $key => $value)
{
$errorSchemaLocal = new sfValidatorErrorSchema($this);
if(array_key_exists('value_delete', $values))
{
if(!$value && !$values['value_delete'])
{
unset($values[$key]);
}
}
// Some error for this embedded-form
if (count($errorSchemaLocal))
{
$errorSchema->addError($errorSchemaLocal, (string) $key);
}
}
// Throws the error for the main form
if (count($errorSchema))
{
throw new sfValidatorErrorSchema($this, $errorSchema);
}
return $values;
}
}

Drupal 6: Modifying uid of a submitted node

I have a situation where I want a set of users (employees) to be able to create a node, but to replace the uid (user ID) with that of the users profile currently displayed.
In other words, I have a block that that calls a form for a content type. If an employee (uid = 20) goes to a clients page (uid =105), and fills out the form, I want the uid associated with the form to be the client's(105), not the employee's.
I'm using arg(1) to grab the Client's uid - here is what I have..
<?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$form['#submit'][] = 'addSR_submit_function';
}
}
function addSR_submit_function($form, $form_state) {
$account = user_load(arg(1));
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}
?>
The form is loading in the block, but when submitted, is still showing the employee uid. I don't want to use hook_form_alter because I don't want to modify the actual form, because clients can fill out the form directly, in this case, I don't want to modify the form at all.
I'm also ashamed that I'm putting this in a block, but I couldn't think of a way to put this in a module, so any suggestions on that would also be appreciated...
To create your form in a block, you could use the formblock module. Especially if you are not used to use the Drupal API. Then all that's left if to add your own submit handler to the form. This is a piece of code that is run, when the form is submitted. You only want to do this on clients pages so you would do that using the hook_form_alter function.
/**
* Hooks are placed in your module and are named modulename_hookname().
* So if a made a module that I called pony (the folder would then be called
* pony and it would need a pony.info and pony.module file I would create this function
*/
function pony_form_service_request_node_form_alter(&$form, $form_state) {
// Only affect the form, if it is submitted on the client/id url
if (arg(0) == 'client' && is_numeric(arg(1))) {
$form['#submit'][] = 'pony_my_own_submit_function';
}
}
function pony_my_own_submit_function($form, &$form_state) {
$account = user_load(arg(1));
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}
The idea behind this code, is to only alter the form when the condition is met - that it is submitted on a client page. I guessed that the arg(0) would be client so if it's something else you would need to change that of cause. We only need to add a submit function, since what we want is to change the values if the form has passed validation.
Then if that is the case our 2nd function is run, which does that actual alteration of the values.
PHP blocks are bad. You can put them in a module.
function hook_block($op, $delta = 0) {
// Fill in $op = 'list';
if ($op == 'view' && $delta = 'whatever') {
$account = user_load(arg(1));
$node = array('uid' => $account->uid, 'name' => $account->name, 'type' => 'service_request', 'language' => '', '_service_request_client' => $account->uid);
$output = drupal_get_form('service_request_node_form', $node);
// Return properly formatted array.
}
}
Additionally, you want a form_alter just to enforce the values. It's ugly but it works.
function hook_form_service_request_node_form_alter(&$form, $form_state) {
if (isset($form_state['node']['_service_request_client'])) {
$form['buttons']['submit']['#submit'] = array('yourmodule_node_form_submit', 'node_form_submit');
}
}
function yourmodule_node_form_submit($form, &$form_state) {
$account = user_load($form_state['node']['_service_request_cilent'])l
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}