Magento attribute file upload - php

Is it possible to do a attribute what will be file upload (not image)?
I tryed like this:
<?php
$installer = $this;
$installer->startSetup();
$attribute = array(
'type' => 'file',
'label'=> 'Catalog Pdf',
'input' => 'file',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => "",
'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'catalog_pdf', $attribute);
$installer->endSetup();
It's showing file upload in category, but probably something is wrong because after installation subcategories are not showing (ajax problem), and in frontend categories view I get "There has been an error processing your request"

File upload attribute type is not present. So, the sql which run on setup is creating the issue
Try something like:
'type' => 'varchar',
'input' => 'image'
This makes input type as file upload but process it in default image processing
'backend_model' => 'xxx'
Here you can specify where this would be processed

Related

Adding a dropdown menu in Prestashop 1.7 module

I'm so beginner in Prestashop 1.7, I wanted to add a dropdown select section in my banner module to select the way to open the banner link.
but the selected value is never passed to the HTML, the code below IS passed but the one under isn't, can you please assist me?
[enter image description here][1]
array(
'type' => 'text',
'lang' => true,
'label' => $this->trans('Banner description', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_DESC',
'desc' => $this->trans('Please enter a short but meaningful description for the banner.', array(), 'Modules.Banner.Admin')
)
array(
'type' => 'select', //select
'lang' => true,
'label' => $this->trans('Banner tab', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_TAB',
'required'=>'true',
'options' => array(
'query' => array(
array('key' => '_blank', 'name' => 'New tab'),
array('key' => '_self', 'name' => 'Same tab'),
),
'id' => 'key',
'name' => 'name'
),
'desc' => $this->trans('Please select the way to open the link.', array(), 'Modules.Banner.Admin')
)
This is how it looks in the Backoffice:
Here
You not only need to add a new field to your form but also handle saving the data from it.
Take a look at a few examples:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L122
Notice how the module author managed to save each configuration field from the form. This is what you need to do.
If you want to have access to data in your view, you have to pass it:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L244
Maybe after you added a new field, you forgot to handle the saving + passing to the view?

Add New Back Office Field In Prestashop

How can i add a new field in prestashop's back office?
Specific, i want to insert a text field in the BO: Orders->Statuses->Add New Order Status under the status name. Which files i have to modify in order to do that? Can anyone describes the full procedure?
Thanks
I am using Prestashop version 1.6.1.2 and added one text field using following steps. You need to make changes in core files. You have to add field in one table in database and do some changes in class and controller file.
Here are the steps to do the same. I have adde field 'my_custom_field'.
Add one field in order_state table
ALTER TABLE {YOUR_DB_PREFIX}order_state ADD my_custom_field VARCHAR(50) NOT NULL;
Change class file of order state. You need to define your field in file "classes/order/OrderState.php"
After code
public $deleted = 0;
add this code snipet
public $my_custom_field;
After code
'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
add this code snipet
'my_custom_field' => array('type' => self::TYPE_STRING),
open "controllers/admin/AdminStatusesController.php" file and do following changes
in function initOrderStatutsList()
after this code
'name' => array(
'title' => $this->l('Name'),
'width' => 'auto',
'color' => 'color'
),
add this code
'my_custom_field' => array(
'title' => $this->l('My Custom Field'),
'width' => 'auto',
),
in function renderForm()
after this code
array(
'type' => 'text',
'label' => $this->l('Status name'),
'name' => 'name',
'lang' => true,
'required' => true,
'hint' => array(
$this->l('Order status (e.g. \'Pending\').'),
$this->l('Invalid characters: numbers and').' !<>,;?=+()##"{}_$%:'
)
),
add this code
array(
'type' => 'text',
'label' => $this->l('My Custom field'),
'name' => 'my_custom_field',
),
Do changes suggested here. Hope this helps you :)

ZF2 File validators return all messages but need only triggered

I want to get only triggered messages, but I am getting all registred messages.
$inputFilter = $factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => ['message' => 'File has to be valid image.']
),
array(
'name' => '\Zend\Validator\File\Extension',
'options' => ['extension' => 'png,jpg,jpeg', 'message' => 'Image extension has to be png,jpg or jpeg.'],
),
array(
'name' => '\Zend\Validator\File\Size',
'options' => ['max' => '2MB', 'message' => 'Maximum file size for image is 2MB.'],
),
),
));
later in controller:
if(!$filter->isValid()){
var_dump($filter->getMessages());
}
If I try to upload image of 5MB size I am getting all messages:
array(
'image' => array(
'fileIsImageNotReadable' => 'File has to be valid image'
'fileExtensionNotFound' => 'Image extension has to be png,jpg or jpeg'
'fileSizeNotFound' => 'Maximum file size for image is 2MB'
)
);
But expect only "Maximum file size for image is 2MB".
Is there any way to return only triggered messages?
Does that should be default behavior of the getMessages() method?
A possible solution for that is to use the Validator Chains.
In some cases it makes sense to have a validator break the chain if its validation process fails. Zend\Validator\ValidatorChain supports such use cases with the second parameter to the attach() method. By setting $breakChainOnFailure to TRUE, the added validator will break the chain execution upon failure, which avoids running any other validations that are determined to be unnecessary or inappropriate for the situation.
This way, validation stops at the first failure and you'll only have the message where validation fails. You could also set priorities so that your validators would be applied in a particular order. This example given on the documentation uses the method attach. This is not what you need exactly.
In your case, you could just use the break_chain_on_failure key in your validator spec with a value set to true. Something like this :
$inputFilter = $factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => ['message' => 'File has to be valid image.']
'break_chain_on_failure' => true,
),
),
));

Drupal 7.31 - Problems with file type field of node

I have created some custom page on frontend for particular type of node modyfications. Here's my page callback:
function vintranet_talk_edit_entry_page_callback($sNid) {
module_load_include('inc', 'node', 'node.pages');
$oNode = node_load($sNid);
return drupal_get_form('page_node_form', $oNode);
}
My node has one field with file attachments.
Config:
'vintranet_talk_attachments' => array(
'field_name' => 'vintranet_talk_attachments',
'label' => t('Attachments'),
'type' => 'file',
'cardinality' => -1,
),
Instance:
'vintranet_talk_attachments' => array(
'field_name' => 'vintranet_talk_attachments',
'label' => t('Attachments'),
'entity_type' => 'node',
'bundle' => 'intranet_talk_page',
'widget' => array(
'type' => 'file_mfw',
),
'settings' => array(
'max_filesize' => 10,
'file_directory' => 'intranet/talk',
'file_extensions' => 'jpg, png, gif, pdf, zip, doc, rtf, xdoc, rar',
'description_field' => 1,
),
'display' => array(
'default' => array(
'type' => 'file_table',
),
),
),
My first problem is, when I want to upload JPG file after module installation, system sends me this message:
So ok... I'm going to check that particular field settings in Structure and I see this:
Why the hell it is saved like this?!
Okaaaaay.... so I'm changing this form field value on jpg, png, gif, pdf, zip, doc, rtf, xdoc, rar, saving and trying to upload the image one more time...
....clickin "Upload" button.... aaaaandd....
....yup.... that's my 2nd problem :/. Have no idea why it's not working. On the backend, in other hand, the "Upload" button works perfectly. Am I missing something?
(working on Drupal 7.31 version)
Menu node add path:
array(
'mynode/add/path' => array(
'title' => 'Title - new entry',
'page callback' => 'vintranet_talk_add_entry_page_callback',
'file' => 'vintranet_talk.pages.inc',
'access arguments' => array('vintranet_talk_add_entry'),
),
);
Answer for my problems:
function hook_menu_alter(&$aItems) {
$sNodePath = drupal_get_path('module', 'node');
$aItems['file/ajax']['file path'] = $sNodePath;
$aItems['file/ajax']['file'] = 'node.pages.inc';
$aItems['system/ajax']['file path'] = $sNodePath;
$aItems['system/ajax']['file'] = 'node.pages.inc';
}

ZF2 File Upload: File is empty

I have a problem with uploading Images in a Zend Framework 2 Application and i think i have a configuration error somewhere in my code.
This is my current Code. I created the Form and Filter like the ZfcUser Form.
Form extends ProvidesEventsForm(ZfcBase)
$file = new Element\File('image');
$file->setLabelAttributes(array('class' => 'control-label col-sm-4'));
$file->setLabel('image');
$this->add($file);
Filter extends ProvidesEventsInputFilter(ZfcBase)
$this->add(
array(
'type' => 'Zend\InputFilter\FileInput',
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => 'File\UploadFile',
),
),
'filters' => array(
array(
'name' => 'File\RenameUpload',
'options' => array(
'target' => './public/img/uploads',
'randomize' => true,
),
),
),
)
);
In the validation process the method FileInput::isValid() is called - but the Value is always null and i have no clue why it is.
The HTML-Form is set to multipart/form-data and the Server configuration is also no problem.
The file i used for testing is only 80KB.
Anyone an idea, what is wrong?
Finally i found a solution for the Problem.
Before the Post information added to the form, they have to merged with the file information:
$files = $this->getRequest()->getFiles()->toArray();
$post = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$files
);
$form->setData($post);

Categories