drupal 7 presist data in form_state with managed_file - forms

I use D7 managed File.
If i have form error than the form lost file info, i know that is there an error a have to reupload the files.
But in the form validator i have the file save in the db so i have the FId of the file (from file_load)
If I can presist value in form state i can load the file from db in form submit and make it presistent.
in form:
$form['fileUpload'] = array(
'#id' => 'fileUploadId',
//'#type' => 'file',
'#title' => t('upload a file: '),
'#size' => 22,
'#type' => 'managed_file',
'#description' => t('upload file: docx doc pdf'),
'#upload_location' => 'public://',
'#upload_validators' => array(
'file_validate_extensions' => array('docx doc pdf'),
// Pass the maximum file size in bytes
'file_validate_size' => array(4*1024*1024),
),
);
i tried:
I pass the &$form_state by reference in form validator , submit, and form
$form_state['values']['FileInfo'] = $form_state['values']['fileUpload'];
If there is form error form_state lost this value
I pass the &$form_state by reference in form validator , submit, and form
$file = file_load($form_state['values']['fileUpload']);
$form_state['values']['FileInfo'] = $form->fid;
form_set_value($element, $value, &$form_state) for persist form_state data
But it's bit tricky to use.

I found the answer.
i create a hidden field in the form :
$form['infoFile'] = array('#type' => 'hidden', '#value' => '');
in the form validator:
$file = file_load($form_state['values']['candidateCvUpload']);
$form['infoFile']['#parents'] = array('infoFile');
form_set_value($form['infoFile'], $file->fid, $form_state);
submit
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);

Related

How to set a default values to input field type in Prestashop backoffice in renderform

In renderform I have one input field and its type is "text". How to set a value to that input field, so at every time when the form is loaded the value should be displayed. I am using Prestashop 1.7.
Sample code:
array(
'type' => 'text',
'label' => $this->l('VENDOR_SERVER_IP'),
'name' => 'serverip',
'size' => 50,
'class' => 'fixed-width-xxl',
'required' => true,
'desc' => $this->l('Please enter your server ip.')
),
You need to use the fields_value property
$helper = new HelperForm();
//...
$helper->fields_value = array(
'serverip' => 'x:x:x:x'
);
You have no option to pass default value of input field in the form array. To provide default value, you have to use fields_value property of form helper.
$hlper = new HelperForm();
$value = 'Your already saved value if any';
if (empty($value)) {
$value = 'your default value';
}
$hlper->field_values = array('YOUR_FORM_INPUT_NAME' => $value);
echo $hlper->generate($your_form_array);

Prestashop helper form 'file' type

I m working on prestasshop and I created a helper form inside a controller (for back office). My question is how to upload a document by using the type:'file' from the helper form. Here is the code:
public function __construct()
{
$this->context = Context::getContext();
$this->table = 'games';
$this->className = 'Games';
$this->lang = true;
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),
'confirm' => $this->l('Delete selected items?')));
$this->multishop_context = Shop::CONTEXT_ALL;
$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'games'
);
$this->fields_list = array(
'id_game' => array(
'title' => $this->l('ID'),
'width' => 25
)
);
$this->identifier = 'id_game';
parent::__construct();
}
public function renderForm()
{
if (!($obj = $this->loadObject(true)))
return;
$games_list = Activity::getGamesList();
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => $this->l('Game'),
'image' => '../img/admin/tab-payment.gif'
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Game:'),
'desc' => $this->l('Choose a Game'),
'name' => 'id_games',
'required' => true,
'options' => array(
'query' => $games_list,
'id' => 'id_game',
'name' => 'name'
)
),
array(
'type' => 'text',
'label' => $this->l('Game Title:'),
'name' => 'name',
'size' => 64,
'required' => true,
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}'
),
array(
'type' => 'file',
'label' => $this->l('Photo:'),
'name' => 'uploadedfile',
'id' => 'uploadedfile',
'display_image' => false,
'required' => false,
'desc' => $this->l('Upload your document')
)
)
);
$this->fields_form['submit'] = array(
'title' => $this->l(' Save '),
'class' => 'button'
);
return AdminController::renderForm();
}
Now how can I upload the document?
Do I have to create a column in the table of the db (games table) for storing the file or something related?
Thanks in advance
I assume this AdminController for your model. Now a model obviously can't hold a file in table column. What you can do is hold path to the uploaded file. That's what you can save.
You should look in AdminController class (which you extended). When you submit a form, one of two method are executed:
processAdd()
processUpdate()
Now investigate the flow logic in these methods. Other methods are called from within this methods, such as:
$this->beforeAdd($this->object); -> calls $this->_childValidation();
$this->validateRules();
$this->afterUpdate($object);
As you can see, there these are the methods where you can do you custom stuff. If you look up these functions in AdminController class, the're empty. They are purposely added so people can override them and put their custom logic there.
So, using these functions, you can validate your uploaded file fields (even though it isnt in the model itself), if it validates you can then assign path to the object; and then in beforeAdd method you can actually move the uploaded file to the desired location (because both child validation and default validation has passed).
The way I've done it:
protected function _childValidation()
{
// Check upload errors, file type, writing permissions
// Use $this->errors['file'] if there is an error;
protected function beforeAdd($object)
{
// create filename and filepath
// assign these fields to object;
protected function afterAdd($object)
{
// move the file
If you allow the file field to be updated, you'll need to to these steps for Update methods as well.
you can get uploaded file using $_FILES['uploadedfile'] in both the functions processAdd() and processUpdate(), you can check all the conditions there and before calling $this->object->save(); to save the form data you can write the code to upload the file in the desired location like
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
since you can't save the file in database you need to save only the name of the file or location on the database
Hope that helps

Drupal 7: Show fields on a form based on another field and a database lookup

I have a form I created in a custom module using the Form API. It is a fairly basic form with only 4 fields. It basically signs a user up for a job alert system. We are basing it only by email address with a few search parameters. We want people to be able to setup a search agent quickly and anonymously meaning they will NOT be creating a Drupal user account as we don't want them to have to deal with a password etc. They will just put in their email address, check off a few preferences and we will save the data.
Now the issue I need to deal with is allowing the user to edit their preferences later on and/or unsubscribe. Again this is not high security and it doesn't need to be. What I would like to do is initially ask ONLY for their email address in the form and allow them to submit it. I would then check the database to see if we already have an entry for that email address and if so, display the pre-filled form for them to edit or unsubsribe, other wise just show them the blank form. So I am just trying to figure out the best way to go about this. I'm thinking I just have one form with all of the fields including email address, but somehow only display the other fields besides the email address after a successful call to the database. I'm just tripping up on how to accomplish this.
EDIT:
I'm wondering if I can use Drupal's AJAX functionality to accomplish this? I tried this, but I couldn't get it to work. I put an Ajax attribute on my submit button with a wrapper ID and a callback function. I created a form element in my form with blank markup and used a prefix and suffix that created a wrapper div with the ID I used in my AJAX parameter. Then I am thinking in my callback function I can do the database lookup and then return the form elements I need either pre-filled or not into the wrapper div that was created, but when I do this, the form does submit via AJAX and I get the spinning wheel, but no matter what I return in my callback, it does not appear in my output wrapper div. Am I going about this the right way? I also made sure I have $form_state['rebuild'] = TRUE; on my original form.
Here is what I tried and it didn't work.
/**
* Implements hook_form().
*/
function _vista_form($form, &$form_state) {
$form = array();
$form_state['rebuild'] = TRUE;
$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#required' => TRUE,
);
$form['render_area'] = array(
'#type' => 'markup',
'#markup' => '',
'#prefix' => '<div id="job-agent-form">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array('class' => array('submit')),
'#ajax' => array(
'callback' => '_display_form',
'wrapper' => 'job-agent-form',
'method' => 'replace',
'effect' => 'fade',
),
return $form;
}
function _display_form($form, &$form_state) {
// there are other form elements that would go here also, I just added two for example
$type_options = array(
'VISTA-HealthCare-Partners-Government' => 'Vista Healthcare Partners',
'International' => 'International Locum Tenens',
'Permanent' => 'Permanent Physician',
'US-Locum-Tenens' => 'US Locum Tenens',
);
$form['job_type'] = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => 'Type of Job',
'#options' => $type_options,
'#empty_option' => 'Choose a placement type',
'#empty_value' => 'all',
//'#default_value' => $type_selected,
);
$form['active'] = array(
'#type' => 'checkbox',
'#title' => 'Subscribe/Unsubscribe',
'#default_value' => 1,
);
return $form;
}
I would go for creating all fields in the form than use hook_form_alter() to hide the unneeded ones with ['#access'] = FALSE.

drupal form api file upload

I have a form with file attachment. Once i load the file and submit the form, if validation errors occur the form is loaded again, but the file i had uploaded is not rendered and I have to load it again.
I tried using file_save_upload but it doesnot seem to work.
$file_attach_set= file_save_upload('file_attachment1', array());
//$file_attach_setII = $form_state['values']['attc'];
$contextid = 150;
if(empty($file_attach_setII)){
$form['file_attachment' . $i] = array(
'#type' => 'file',
"#title" =>'kik'
'#default_value'=> $file_attach_set->fid,
//'#title_display' => $file_attach_set->uri,
''
);
}
The file element doesn't have a #default_value property. Try using the managed_file type which has this property.
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#file
$form['file_attachment' . $i] = array(
'#type' => 'managed_file',
'#title' =>'kik',
'#default_value'=> array(
'fid' => $file_attach_set->fid,
),
);

drupal questions - how to print a form from another module?

I'm trying to include a module form in a panel and I've tried using drupal_get_form(), but not sure I'm using it correctly.
In the organic groups module, there's a function to render an og_broadcast_form. It's called within a page_callback in og.module:
// Broadcast tab on group node.
$items['node/%node/broadcast'] = array(
'title' => 'Broadcast',
'page callback' => 'drupal_get_form',
'page arguments' => array('og_broadcast_form', 1),
'access callback' => 'og_broadcast_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'file' => 'og.pages.inc',
'weight' => 7
);
And in og.pages.inc, the function is:
function og_broadcast_form($form_state, $node) {
drupal_set_title(t('Send message to %group', array('%group' => $node->title)));
if (!empty($form_state['post'])) {
drupal_set_message(t('Your message will be sent to all members of this group.'));
}
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 70,
'#maxlength' => 250,
'#description' => t('Enter a subject for your message.'),
'#required' => TRUE,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#rows' => 5,
'#cols' => 90,
'#description' => t('Enter a body for your message.'),
'#required' => TRUE
);
$form['send'] = array('#type' => 'submit', '#value' => t('Send message'));
$form['gid'] = array('#type' => 'value', '#value' => $node->nid);
return $form;
}
I've tried a number of variations of drupal_get_form:
print drupal_get_form('og_broadcast_form', NULL, arg(1)); //where arg 1 is the node id from the url
print drupal_get_form('og_broadcast_form');
print drupal_get_form('og_broadcast_form', &$form_state, arg(1));
print drupal_get_form('og_broadcast_form', $n); //where $n is node_load(arg(1));
print drupal_get_form('og_broadcast_form', &$form_state, $n);
etc., etc... Is there a way to accomplish what I'm trying to do here?
FYI.. your problem is that you're attempting to load a form located in another modules include file.
The function is located in og.pages.inc and you'll need to make a call to:
module_load_include('inc', 'og', 'og.pages');
This must be done before you can grab the form.
If drupal_get_form is given the name of a function as it's first argument, that will be both the form_id and a function to be called to generate the $form array.
On line 3 of the function code, we have $args = func_get_args();, this is used by drupal_get_form to collect any or all additional arguments you may want to pass to your form-building function.
You should be using drupal_get_form('og_broadcast_form', node_load(arg(1)));.
Are you sure you should be using print and not return? I have recently learned they do very different things in the theming system. I have used drupal_get_form in this way to populate the contents of a block successfully, but at no point did I print to the screen myself.
EDIT: The full node object and not the nid because %node in a menu path uses a wildcard loader to pass the node_load(arg(1)) on to whatever function is being called.
drupal_get_form gets the form for you. have you tried print drupal_render(drupal_get_form('whatever'))) ?
drupal_get_form() only accepts one argument, the $form_id.
http://api.drupal.org/api/function/drupal_get_form/6
Run a hook_form_alter() and var_dump($form_id). That will give you the $form_id, and when you pass that to drupal_get_form(), it should return the rendered form.