Add mailto functionality in admin field - magento

How can i new field in admin filed that contain mailto functionality
$fieldset->addField('email', 'link', array(
'label' => Mage::helper('mumodule')->__('Email'),
"target"=>"_blank",
'mailto' => Mage::registry('mumodule')->getData('email'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
));
using this way i cant add functionality.
is it possible to add new filed with mailto functionality?

You must create your own form field renderer. For this you will need a custom module. If you don't know how to do that here's a good starting point: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_create_an_admin_form_module
Create a new file in app/code/[local/community]/MyCompany/MyModule/Varien/Data/Form/Element/Mailto.php with this content:
class MyCompany_MyModule_Varien_Data_Form_Element_Mailto extends Varien_Data_Form_Element_Abstract {
public function __construct($data) {
parent::__construct($data);
$this->setType('link');
}
public function getElementHtml() {
$html = $this->getBeforeElementHtml();
if ($this->getValue()) {
$html .= ' ';
}
$html .= $this->getAfterElementHtml();
return $html;
}
After that go to your form file and add this to the fieldset:
$fieldset->addType('mailto','MyCompany_MyModule_Varien_Data_Form_Element_Mailto');
$fieldset->addField('email', 'mailto', array(
'label' => Mage::helper('mymodule')->__('Email'),
'name' => 'email',
));
Of course, you should replace MyCompany namespace with the namespace that you already use in the module and MyModule with your module name. Also don't forget to place the file in the code pool where your module already exists: local/community

i achieve in simple way
$fieldset->addField('email', 'link', array(
'label' => Mage::helper('mumodule')->__('Email'),
'target' => '_blank',
'href' => 'mailto:' . urlencode(Mage::registry('mumodule')->getData('email')),
'class' => 'required-entry',
));
mailto: is part of the URL so it should be assigned in the href attribute:

Related

ckeditor in Drupal 8 custom module

I am currently developing a custom module for Drupal 8.
While adding a backend form to get some data from users I tried to get the ckeditor configured to replace my textareas... and failed >.<
here is the form definition:
$form['text'] = array(
'#type' => 'textarea',
'#title' => t('Text'),
'#required' => TRUE,
'#attributes' => array(
'id' => 'text',
'style' => 'max-width: 650px'
),
'#default_value' => $data['text']
);
where do I need to load the ckeditor to replace my textarea?
things I already tried:
$build['#attached'] = array(
'js' => array(
drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js'
drupal_render($build);
and
drupal_load_library("ckeditor", "ckeditor");
but I can't load all the dependencies
Refer to the Drupal 8 form API which has a text_format form type. A check on your #default_value is also recommended.
Navigate to Configuration > Content authoring > Text formats and editors giving you an overview of the current available text formats. The ones listed here can be used in the #format proprty of your form field. (Default fallback currently is basic_html)
Try to change your code to:
$form['text'] = array(
'#type' => 'text_format',
'#title' => t('Text'),
'#required' => TRUE,
'#default_value' => isset($data['text']) ? $data['text'] : '',
'#format' => 'full_html',
);
I had a similar issue but had issues saving the data with above solution. In the end it looked like this:
$form['body'] = [
'#type' => 'text_format',
'#title' => $this->t('Body'),
'#description' => $this->t('Message'),
'#default_value' => $config->get('body'),
'#format' => 'full_html',
];
and in submitForm method I save config like so
/**
* {#inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('my_module.default')
->set('body', $form_state->getValue('body')['value'])
->save();
}
Note the reference to the attached ['value'] array.

How to translate form labels in Zend Framework 2?

I'm not getting it!.. Can please someone explain, how to translate form labels? A simple example would be great.
Thank you in advance!
class Search\Form\CourseSearchForm
...
class CourseSearchForm extends Form {
...
public function __construct(array $cities) {
parent::__construct('courseSearch');
...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Stadt',
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
...
}
}
view script /module/Search/view/search/search/search-form.phtml
<?php echo $this->form()->openTag($form); ?>
<dl>
...
<dt><label><?php echo $form->get('city')->getLabel(); ?></label></dt>
<dd><?php echo $this->formRow($form->get('city'), null, false, false); ?></dd>
...
</dl>
<?php echo $this->form()->closeTag(); ?>
<!-- The formRow(...) is my MyNamespace\Form\View\Helper (extends Zend\Form\View\Helper\FormRow); the fourth argument of it disables the label. -->
The module/Application/config/module.config.php is configured:
return array(
'router' => ...
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => ...
'view_manager' => ...
);
I also edited my view and use the FormLabel view helper:
<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt>
Furthermore I debugged the FormLabel at the place, where the tranlator is used (lines 116-120) -- seems to be OK.
But it's still not working.
EDIT
The (test) items for labels, I added to the de_DE.po file manually, are tranlated. The ZF2 side problem was actually, that I was using $form->get('city')->getLabel() instead of $this->formlabel($form->get('city')) in th view script.
The problem is now, that the labels are not added to the de_DE.po file. But it's not a ZF2 issue anymore, so I've accept Ruben's answer and open a new Poedit question.
Instead of using:
<?php echo $form->get('city')->getLabel(); ?>
You should use the formlabel view helper. This helper automatically uses your translator during rendering if you have inserted it in your ServiceManager. Most likely you will have it in your Application's module module.config.php:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
Once you do use the formlabel view helper:
echo $this->formLabel($form->get('city'));
And of course make sure your translations are in your .po file.
i think your problem is that you label are not detected by poedit (or similar tool), so you have to add them manually to your poedit catalogs (.po)
to make your label strings detected by tools like poedit, your strings need to be used inside a translate() function or _() (other function can be added in Catalog/properties/sources keyword)
as the _() function is not user in ZF2 (today) so a tiny hack is to add a function like this in your index.php (no need to modify anything, this way, in poedit params):
// in index.php
function _($str)
{
return $str;
}
and in your code, just use it when your strings are outside of a translate function
//...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => _('myLabel') , // <------ will be detected by poedit
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
//...
or like this if you prefer
$myLabel = _('any label string'); // <--- added to poedit catalog
//...
'options' => array(
'label' => $myLabel ,
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
#Ruben says right!
Me I use PoEdit to generate my *.mo files and to be sure to get all translations in the file, I create somewhere (in view for example) a file named _lan.phtml with all text to be translated :
<?php echo $this->translate("My label");
... ?>
Of course, Poedit has to be configured to find my keywords. check this to how to configure it
All solutions don't use the power of ZF2. You must configure your poedit correctly :
All things are here :
http://circlical.com/blog/2013/11/5/localizing-your-twig-using-zend-framework-2-applications

Call specific tab on edit form in custom module in magento

I created one custom module in magento.When i click on the grid it moves to edit form where i can see three tabs like tab1,tab2,tab3.By default tab1 is selected.Now i want to add one link on grid and when customer click on that link browser redirect user to the tab3.How can i do that.My tab code as follow :
protected function _beforeToHtml()
{
$this->addTab('form_section', array(
'label' => Mage::helper('mymodule')->__('Information'),
'title' => Mage::helper('mymodule')->__('Information'),
'content' => $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tab_form')->toHtml(),
));
$this->addTab('form_section1', array(
'label' => Mage::helper('mymodule')->__(' Management'),
'title' => Mage::helper('mymodule')->__('Management'),
'content' => $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tab_managment')->toHtml(),
));
$this->addTab('form_section2', array(
'label' => Mage::helper('mymodule')->__('Results'),
'title' => Mage::helper('mymodule')->__('Results'),
'content' => $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tab_result')->toHtml(),
));
return parent::_beforeToHtml();
}
My link code like that one on grid listing page. <a class="viewit" href="http://localhost/project/index.php/mymodule/adminhtml_mymodule/view/id/4/key/83063e416ef7f9cfb7825d01e4519293/">View</a>.My contoller function as:
public function viewAction()
{
$this->loadLayout();
$block = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tab_result');
// $this->_addContent($this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tab_result'))
//->_addLeft($this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tabs'));
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
}
The code at Mage_Adminhtml_Block_Widget_Tabs::addTab suggests that tabs have property active. Try adding it to your addTab call:
$this->addTab('form_section2', array(
'label' => Mage::helper('mymodule')->__('Results'),
'title' => Mage::helper('mymodule')->__('Results'),
'content' => $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_edit_tab_result')->toHtml(),
'active' => true
));
Or you can extend your Grid's row URLs with parameter activeTab set to 'form_section2' (the name of the active tab) and add the following code to the _beforeToHtml function of the Tabs block class:
$param = Mage::app()->getRequest()->get('activeTab');
if (array_key_exists($param, $this->_tabs)) {
$this->_tabs[$param]->setActive();
}

CodeIgniter 2 and Ion Auth - edit own user account

I am using the latest Ion Auth files along with the latest version of CodeIgniter 2.
There is a function called edit_user within the auth.php Controller file. This function is restricted to use only by members within the "Admin" group, and any Admin member can edit any other member using the function via this URL...
/auth/edit_user/id
The problem is that I don't see any Controller function or View that allows a regular (non-Admin) user to edit their own account details.
Would this be a new Controller function I'd need to write (modify edit_user function?) or is this something that Ion Auth should already do? If so, how?
Here is the stock Ion Auth edit_user function contained within the auth.php Controller...
function edit_user($id)
{
$this->data['title'] = "Edit User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
$user = $this->ion_auth->user($id)->row();
//process the phone number
if (isset($user->phone) && !empty($user->phone))
{
$user->phone = explode('-', $user->phone);
}
//validate form input
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
$this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');
if (isset($_POST) && !empty($_POST))
{
// do we have a valid request?
if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
{
show_error('This form post did not pass our security checks.');
}
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'),
);
//update the password if it was posted
if ($this->input->post('password'))
{
$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
$data['password'] = $this->input->post('password');
}
if ($this->form_validation->run() === TRUE)
{
$this->ion_auth->update($user->id, $data);
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', "User Saved");
redirect("auth", 'refresh');
}
}
//display the edit user form
$this->data['csrf'] = $this->_get_csrf_nonce();
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
//pass the user to the view
$this->data['user'] = $user;
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name', $user->first_name),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name', $user->last_name),
);
$this->data['company'] = array(
'name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company', $user->company),
);
$this->data['phone1'] = array(
'name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1', $user->phone[0]),
);
$this->data['phone2'] = array(
'name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
);
$this->data['phone3'] = array(
'name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password'
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password'
);
$this->load->view('auth/edit_user', $this->data);
}
Unless I'm missing something, I ended up modifying the edit_user function within the auth.php Controller as follows.
I changed this line which checks to see that the user is "not logged in" OR "not an admin" before dumping them out...
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
...into this, which check to see that the user is "not logged in" OR ("not an admin" AND "not the user") before dumping them out...
if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id)))
This seems to be working...
Admin can edit all accounts
User can only edit his own account
and somebody not logged in, can't edit any account.
Edit: However, the user also has access to the "groups" setting and could simply put themself into the "admin" group. Not good.
Ion Auth's developer refers to the files he provides as working "examples". Therefore, it's up to the end-developer to edit Ion Auth to suit the needs of the project.
To prevent the user from being able to make himself an "admin" requires a simple change to the edit_user.php view file.
Verifies the user is already an "admin" before creating the checkboxes...
<?php if ($this->ion_auth->is_admin()): ?>
// code that generates Groups checkboxes
<?php endif ?>
Then you'll also need to thoroughly test and adjust as needed. For example, after editing a user profile, you're redirected to the auth view. Since the user doesn't have permission to see the auth view, there is a "must be an admin" error. In the controller file, you'll have to add the appropriate logic to properly redirect the user when they're not an "admin".
No Ion Auth doesn't do this as is - it's pretty light weight. But it's not hard to do and your on the right track, just grab that edit_user method and take out the admin checks and make it so the user can only edit their own account, just alter it so that it only updates user details for the currently logged in user.
Check the ion auth docs, have a crack at it and come back with some code if you have any problems.

magento: add attibute to product, but not show up when editing the product

As per bens comment and answer I updated my script, comments indicate changes
{Magento 1.4.0.1} currently i have an installer script:
$installer = $this;
$installer->startSetup();
//commented out to use factory method
//$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup = Mage::getResourceModel('catalog/setup','core_setup');
if(!$setup->getAttribute('catalog_product','attribute_code')){
$newFields = array(
'attribute_code' => array(
'type' => 'text',
'label' => 'Attribute Label',
//added visible option
'visible' => false,
),
);
$entities = array(
'catalog_product',
);
foreach($newFields as $attributeName => $attributeDefs) {
foreach ($entities as $entity) {
$setup->addAttribute($entity, $attributeName, array(
'type' => $attributeDefs['type'],
'label' => $attributeDefs['label'],
//added visible option
'visible' => $attributeDefs['visible'],
'class' => '',
'required' => false,
));
}
}
}
$installer->endSetup();
It works wonderfully! Except the attribute shows up in the General attribute group when editing the product and I don't want it to show up at all (its a secret ninja attribute) is there something I'm doing wrong? or perhaps something I should be doing to let Magento know not its not supposed to show up?
Using addAttribute() you can set the 'visibleindex tofalse. UsingupdateAttribute()` you should do the following:
$setup->updateAttribute('catalog_product','attr_code','is_visible',false);
Let me know if I'm wrong.

Resources