Can you give best practices in order to change the default directory of uploaded files (web/uploads/gallery) ?
It would be great if you can use for example some form data (album slug if you are uploading photos into an album) or just simple thing like the year or the month because currently all uploaded files are going in a same directory which is hard to maintain..
Thanks
This question was answered a few times in the bug tracker. Basically you create a custom namer and include all your dynamic data to the file path. Sub-directories will be created automatically.
namespace AppBundle\Uploader\Naming;
use Oneup\UploaderBundle\Uploader\File\FileInterface;
use Oneup\UploaderBundle\Uploader\Naming\NamerInterface;
class CatNamer implements NamerInterface
{
public function name(FileInterface $file)
{
$product = //...
return sprintf('%d/%s, $product->getId(), grumpycat.jpg');
}
}
This way you can create your own folder structure.
Related
I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur
I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper
If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle
Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}
This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)
Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.
In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");
Good Day,
I am doing a hook_form_alter and followed a tutorial where you create a directory in your /sites/all/modules directory and place the module.info and mymodule.module files within. I've called that directory mymodule so the path will be /sites/all/modules/mymodule.
The problem I'm having is trying to enable the module. It does not show up in the list of Modules when I check. It's clearly in my directory.
My module (my_module.module) looks like this and is going to be used to modify the form where you add the product (basically taking out the SKU, list price and commission):
<?php
// $Id$
/**
* #file
* Module to hold my customizations to Ubercart
*/
/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter($form_id, &$form) {
if ($form_id == 'product_node_form') {
$form['base']['model']['#required'] = FALSE;
$form['base']['prices']['list_price']['#required'] = FALSE;
$form['base']['prices']['cost']['#required'] = FALSE;
$form['base']['prices']['sell_price']['#required'] = FALSE;
$form['base']['prices']['shippable']['#required'] = FALSE;
}
}
And the info file, my_module.info, looks like this:
; $Id$
name = My module
description = My customizations to Ubercart
package = Custom
core = 7.x
Why would it not show up in the modules list and how do I make Drupal see the module so I can enable it?
I don't have a category called Custom and nothing is showing up.
So far, i've gone into my php.ini file and put in safe_mode=off to see if that would help (because someone said this sometimes causes Drupal to ignore new modules for some reason) but that didn't help.
I created both files on the server so I don't think there would be any issues with something like linefeeds (I use GoDaddy's FTP File Manager and it comes with an editor.)
What could the problem be because I'm stumped?
Thanks for looking.
Make sure to name the .info and .module files with the same name.
E.g. mymodule.info and mymodule.module.
Note that, the name should be alpha-numeric (underscore is allowed) and starts with a letter.
In your hook implementation, replace the word hook with your module's name
function mymodule_form_alter($form, &$form_state, $form_id)
{
// code goes here...
}
I discovered something that works. After reading about people having issues with linefeeds and not knowing if this was creating the problem, I decided to try something.
I went to a module directory and found a .info file. In my case, I went to a directory that had Commerce in it and copied the Commerce.info file to my custom module's directory.
Then, what I did was take out the stuff that Drupal added after it installed the Commerce module (you can do the same for whatever .info file you copy from) and modified the information at the start, removing any dependency lines or anything that doesn't matter. In other words, make it like the .info file you are trying to use but DON'T COPY from the .info you've been trying to enable!
Save it this file (in my case, it's still commerce.info.)
Now delete the .info file you were trying to enable originally.
Rename the new .info file to the name you wanted (commerce.info => mymodule.info.)
Go to your Modules page and refresh it.
Voila...there it is. Enable and save configuration.
I have 2 different project. One is supposed to upload images (admin) and the other is supposed to show them.
I was writing something like "/Contents/images/image path"... But wait! I will I upload the images from the application into that address?
Any help and suggestions please.
If you have two applications that will interact with the same files, it's probably better to have an ImageController with an action that allows you to upload/download the image rather than storing them directly as content. That way both applications can reference the same file location or images stored in a database and manipulate them. Your download action would simply use a FileContentResult to deliver the bytes from the file. You can derive the content type from the file extension.
Example using a database. Note that I assume that the database table contains the content type as determined at upload time. You could also use a hybrid approach that stores the image metadata in a database and loads the actual file from a file store.
public class ImageController : Controller
{
public ActionResult Get( int id )
{
var context = new MyDataContext();
var image = context.Images.SingleOrDefault( i => i.ID == id );
if (image != null)
{
return File( image.Content, image.ContentType );
}
// or you could return a placeholder image here if appropriate.
throw new HttpException( 404, "The image does not exist" );
}
}
An alternative would be to incorporate your administrative interface in an area of the same application rather than in a separate project. This way you could reuse the content/images directory if you wanted. I find that when you have dynamic images the database or a hybrid approach works better from a programming perspective since it's more consistent with the rest of your data model.
you could try like this..
Let's assume that all of your images are in Project A and you want to use the same images in Project B.
Open Project B with Visual Studio. In the Solution Explorer, right click your Project Name and select "Add Existing Item...".
Browse to the physical location on disc where your images in Project A are stored and select the files that you want to import.
You'll then be able to access those images from project A in Project B.
My problem is almost like that of http://elrte.org/redmine/boards/2/topics/734?r=3250 which didn't get any solution.
I am using elfinder on my wordpress site in which on every user register I create a folder under the files directory on username and when that user comes to the interface where elfinder shows I want elfinder to show that user his user's folder as the root directory instead of the actual root set.
I found out through code that the root directory is set in connectors/php/connector.php file that passes it to elFinder.class.php file
I even found out a way to do that hardcoded... which is if you go into elFinder.class.php file and append the username with root in __contruct function everything works fine
public function __construct($options=array()) {
foreach ($this->_options as $k=>$v) {
if (isset($options[$k])) {
$this->_options[$k] = is_array($this->_options[$k])
? array_merge($this->_options[$k], $options[$k])
: $options[$k];
}
}
if (substr($this->_options['root'], 1) == DIRECTORY_SEPARATOR) {
$this>_options['root'] = substr($this->_options['root'], 0, 1);
} $this>_options['root'] .= 'username';
But the problem is that I am unable to find a way through which I can access username in elFinder way... It feels like there will be some simple way that my mind just ain't clicking to.. any help in this regard will be greatly appreciate.
Even if I can't access username in this function and somehow pass the username to this function or connector.php which calls its construct???
Modifying elFinder.class.php is a wrong way, what you need is to get your WordPress instance inside connector.php and generate correct root option for current user, the username/path you should get from WordPress.
I'm in the progress of creating a bulk upload function for a Drupal site. Using flash I'm able to upload the files to a specific url that then handles the files. What I want to do, is not just to upload the files, but create a node of a specific type with the file saved to a filefield that has been setup with CCK. Since these are audio files, it's important that filefield handles the files, so addition meta data can be provided with the getid3 module.
Now I've looked through some of the code as I wasn't able to find an API documentation, but it's not clear at all how I should handle this. Ideally I could just pass the file to a function and just use the data returned when saving the node, but I haven't been able to find that function.
If any one has experience with this I would apreciate some pointers on how to approach this matter.
I had to do something similar some weeks ago and ended up adapting some functionality from the Remote File module, especially the remote_file_cck_attach_file() function. It uses the field_file_save_file() function from the filefield module, which might be the function you're looking for.
In my case, the files are fetched from several remote locations and stored temporarily using file_save_data(). Attaching them to a CCK filefield happens on hook_nodeapi() presave, using the following:
public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) {
// Grab the filefield definition
$field = content_fields($fieldname, $node->type);
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$fieldFileDirectory = filefield_widget_file_path($field);
// This path does not necessarily exist already, so make sure it is available
self::verifyPath($fieldFileDirectory);
$file = field_file_save_file($filepath, $validators, $fieldFileDirectory);
// Is the CCK field array already available in the node object?
if (!is_array($node->$fieldname)) {
// No, add a stub
$node->$fieldname=array();
}
$node->{$fieldname}[$index] = $file;
}
$filepath is the path to the file that should be attached, $fieldname is the internal name of the filefield instance to use within the node and $index would be the 0 based index of the attached file in case of multiple field entries.
The function ended up within a utility class, hence the class syntax for the verifyPath() call. The call just ensures that the target directory is available:
public static function verifyPath($path) {
if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
throw new RuntimeException('The path "' . $path . '" is not valid (not creatable, not writeable?).');
}
}
That did it for me - everything else happens on node saving automatically.
I have not used the getid3 module yet, so I have no idea if it would play along with this way of doing it. Also, I had no need to add additional information/attributes to the filefield, so maybe you'd have to put some more information into the field array than just the file returned by field_file_save_file(). Anyways, hope this helps and good luck.
I have done something whith imagefield which worked, I think the structure has to be right otherwise it won't work. It took a lot of trial and error. This is is what I populated the imagefield with.
$image['data'] =array(
'title' => $media_reference_attributes->getNamedItem("source")->value,
'description' => $description,
'alt' => "",);
$image['width'] = $width;
$image['height'] = $height;
$image['mimetype'] = $mime_type
$image['uid'] = 1;
$image['status'] = 1;
$image['fid'] = $fid;
$image['filesize'] = $file->filesize;
$image['nid'] = $id;
$image['filename'] = $url;
$image['timestamp'] = $file->timestamp;
$image['filepath'] = $file_path;
Hope this is of some help.
You might want to look at Image FUpload if you need a look at integrating the flash upload.
To push the files on to another server while still handling them through Drupal sounds a little like the CDN space, maybe look at the behavior in the CDN or CDN2 projects?
If you find a clear solution please come back and post it!