How to populate image field value with drupal_execute.
for ex my content type (test) has two additional fields
1. photo (image filed),
2. phid (text field)
for phid $form_state['values']['field_phid'][0]['value'] ='14'; . how to populate photo which is image field type
If the file is already uploaded to Drupal and has a file ID (fid) then you can just do
$form_state['values']['field_image_filed'][0]['fid'] = 17; //where 17 is the Drupal file ID of the file you want input
If the file isn't already uploaded it's a lot trickier. You'll first need to programmatically create the file. I can't walk you through it off-hand but a good place to look for a template as to how it should be done is the file_service_save() function in the Services module's file_service.inc:
http://drupalcode.org/viewvc/drupal/contributions/modules/services/services/file_service/file_service.inc?revision=1.1.2.7.2.3&view=markup&pathrev=DRUPAL-6--2-2
To be clear: I'm not saying you'll use file_service_save() to accomplish the upload, but that that code shows you what needs to be done. It will show you how to save the file to the server using file_save_data(), record the file to the Drupal "files" table, then call hook_file_insert to notify other modules that a file's been saved.
i found the solution as below . i dont know pros and cons but it works fine for me.
$image = "*******/test.jpg";
$field = content_fields('field_img', 'img_test');
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$files_path = filefield_widget_file_path($field);
$form_state['values']['field_img'][]= field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);
Related
As far as I see there is only one type of media field which can hold every type of media (image, video, pdf etc.) Is there a way to restrict the media type, so that the field only accepts images and no other filetypes? Because when I allow the field to hold multiple files the array will hold images, video and files. I search for a solution how I can restrict the data types for this field.
You can deal with that by updating the upload function of the upload plugin.
Please for that you will have to check how the extensions folder work.
📚Documentation here: https://strapi.io/documentation/3.0.0-beta.x/concepts/concepts.html#extensions
And after you will have to find the path of the file you want to update.
It will be this one https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-upload/controllers/Upload.js
So you will have to create an empty file at the same path in the extension folder.
It will be ./extensions/upload/folder/Upload.js
module.exports = {
};
In this file you will have to create the function you want to update/override.
It will be this function https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-upload/controllers/Upload.js#L12
So you have to copy this function and past it in your extensions file.
When it's done you can modify the function as you want.
Here in the function https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-upload/controllers/Upload.js#L27 you can see the var files contain all your files, you can simply add your code to test the type of your files.
Using Laravel Mediable and I'm trying to figure out the best way to delete an individual file. Say there's a list of files when I'm viewing the parent Model, and I click a delete icon to make an ajax request to delete file (which should remove both the corresponding Media object and the physical file...)
This works:
$path_parts = pathinfo($request->filename);
$attachment = Media::where('directory', $folder)
->where('filename', $path_parts['filename'])
->where('extension', $path_parts['extension'])
->first();
$attachment->delete();
but this deletes only the database row and not the physical file itself:
$attachment = Media::where('id', $request->fileid);
$attachment->delete();
I'd prefer deleting the file via the id because its unique but wondering what I'm missing...
You must delete the file also using this code
File::delete('path/to/'.$request->filename);
=updated=
The problem on the second block is just need to add ->first() at the end of the where clause
I am trying to make a joomla plugin, but I have few questions that I haven't found any answer.
What the plugin has to do: add a new field in register form(let's say Cell Number), and on form submit insert that cell number in database.
My documentation is this tutorial.
Questions:
How do you add a new field in register form? xml file is done, but I am not sure how to write the php code...(please help). What this code do?
$form->setFieldAttribute('something', 'required', $this->params->get('profile-require_something') == 2, 'profile5');
How do I get the cell number variable from that form? $jinput = JFactory::getApplication()->input; ?
Pleas help me with few tips. Thanks!
Just copy the code that is there. What will happen is that the php will automatically loop through all the fields in your xml.
$form->setFieldAttribute('something', 'required', $this->params->get('profile-require_something') == 2, 'profile5');
Is taking the field with the name something and changing it to be required if the parameter called profile-require_something is set to 2 . profile5 is the name of the xml file and the php file for the example plugin. It's the actual name of the plugin. You can have many profile plugins if you want but each needs its own name.
To get the a value you would do something like
$jinput->getString('cell_number', '');
Here is my BrandController.php
https://gist.github.com/a958926883b9e7cc68f7#file-brandcontroller-php-L53
I've gone through all my files of my custom module, and compared them to the one given from the custom module maker, and I couldn't find much differences.
Are you attempting to upload multiple files? If you're using multiple fileupload elements with the same name you'll get an array of items.
So when the following line is called,
//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
It will have the value
["name"]=>array(2) {
[0]=>string(9)"file0.txt"
[1]=>string(9)"file1.txt"
}
you'll need to update the code to loop through each $_FILES['filename']['name'] and upload and save the files separately.
You may unknowingly uploaded multiple files. If you that is not your intention, you may check your in your HTML and check the name attribute of the tag. It must not be an array (like this).
<input type="file" name="my_files[]" />
If you only see Array() in your database, it means you are indeed uploading a multiple files. You can process them by using loops.
If you are really sure that you are uploading 1 image, you may follow #Palanikumar's suggestion. Use a print_r() and display the $_FILES and paste it here. IF you don't want to use that, You can use
json_encode($the-data-you-are-going-to-insert-to-the-database);
If you don't know where to put the print_r() function, you may put it after line 56 of this file.
https://gist.github.com/desbest/a958926883b9e7cc68f7#file-brandcontroller-php-L53
if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
print_r($_FILES);
die;
If saveAction() is being called inside an ajax function you need to log the ajax response. Assuming you are using jquery..
$ajaxResponse = $.POST({...});
console.log($ajaxResponse.responseText);
Then, you you can view it inside a browser's console. If nothing appears, you may use a non-async request
$ajaxResponse = $.POST({
// your options,
// your another option,
async: FALSE
});
Usually file upload will return in array format. So that each uploaded file will have the information like name, type, size, temporary name, error. You can get the file information using print function (print_r($_FILES)). So if you want to display name of the file you have to use something like this $_FILES['filename']['name']
Use print function and debugging tool then save file information using loops.
For more info please check here.
You aren't setting the enctype of the form so the image will never be sent. updated the code to
$form = new Varien_Data_Form(array( 'enctype' => 'multipart/form-data'));
I have written a custom module to import users from a csv file into a drupal 7 database.
The csv file has a field for the users avatar that references an image in a directory.
The issue I'm having is with attaching the images to the user. So the image gets saved correctly in the pictures directory, the record is added the file_managed table, and the file id is added to the user's record. However when I go to edit the user via the drupal interface, the picture does not appear on that page.
The code I've got is below, any help would be awesome!
$userobj = user_load(1);
$file_temp = file_get_contents('/avatars/'.$importfile);
$file_temp = file_save_data($file_temp, 'public://pictures/' . $filename, FILE_EXISTS_RENAME);
$userobj->picture->fid = $file_temp->fid;
$userobj->status = 1;
user_save((object) array('uid' => $record->uid), (array) $userobj);
best way to get file path in d7 is
$path = file_default_scheme() . '://' ;
the path should now look something like public://
i had a mare saving in the users pictures folder but found it was a file permissions thing and had to chmod the folder