ckeditor in Drupal 8 custom module - ckeditor

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.

Related

Unable to understand Install script (sql script) in Magento

I am adding attribute by updating sql script, like this,
$installer = $this;
$installer->startSetup();
$installer->addAttribute('customer_address', 'group_id', array(
'label' => 'Address group',
'visible' => true,
'required' => false,
'type' => 'int',
'input' => 'select',
'source' => 'address_group/address_attribute_source_group',
'user_defined' => 1,
'position' => 100
));
.
.
.
$installer->endSetup();
I am unable to understand what is meant by following line, I am unable to find any explanation about it
'source' => 'address_group/address_attribute_source_group',
I am unable to comment on your post. Trying to understand if you have copied this code from somewhere. From your code I understand that you want to add a "Customer Address Attribute" named as "customer_address"
'source' => 'address_group/address_attribute_source_group'
The implication of the above is the path. You should have a folder/file path as below:
/app/code/local/Address/Group/Model/Address/Attribute/Source/Group.php
Group.php:
class Address_Group_Model_Address_Attribute_Source_Group ...
Since, this attribute is of TYPE => "SELECT", you should be having the options array in this file "Group.php"
Options Array should be something very similar to:
public function toOptionsArray() {
return array(
array(
'label' => '',
'value' => ''
),
array(
'label' => Yes,
'value' => 1
),
array(
'label' => No,
'value' => 0
)
);
}
Let me know if you got it!
Happy to Help!
Happy Coding...
It points to the class that provides options for the attribute. As attribute uses select input it requires options to be provided. This class is created by calling Mage::getModel() and passing the value of source to it. To find the class you need to find node models/address_group in config.xml files of the available modules. This will provide class prefix. Next what comes after slash is added to that prefix in order to create class name. So in this case it will resolve to something like Company_AddressGroupModule_Model_Address_Attribute_Source_Group. This class need to implement toOptionsArray method that returns an array in the following format:
array(
array('value' => 'option_value', 'label' => 'option_label'),
...
);

ReferenceError: MediabrowserUtility is not defined in magento wysiwyg

my question that i am using wysiwyg editor in magento admin side i checked every page every thing is right no error in code when click on the insert image they generate the error and can not works fine so help me anyone they face this type of error. error is
"ReferenceError: MediabrowserUtility is not defined"
thanks
$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig(array('tab_id' => 'form_section'));
$wysiwygConfig["files_browser_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index');
$wysiwygConfig["directives_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["directives_url_quoted"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["widget_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index');
$wysiwygConfig["files_browser_window_width"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_width');
$wysiwygConfig["files_browser_window_height"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_height');
$plugins = $wysiwygConfig->getData("plugins");
$plugins[0]["options"]["url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin');
$plugins[0]["options"]["onclick"]["subject"] = "MagentovariablePlugin.loadChooser('".Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin')."', '{{html_id}}');";
$plugins = $wysiwygConfig->setData("plugins",$plugins);
$fieldset->addField('question_details', 'editor', array(
'name' => 'content',
'label' => Mage::helper('supportportal')->__('Details'),
'title' => Mage::helper('supportportal')->__('Details'),
'style' => 'width:600px; height:300px;',
'config' => $wysiwygConfig,
'wysiwyg' => true,
'required' => true,
));
The error is due to a missing configuration setting which is assigned to the WYSWIG addField() array.
If you open your Form.php simply add the following above the WYSIWYG field,
$configSettings = array( 'add_widgets' => false, 'add_variables' => false, 'add_images' => false, 'files_browser_window_url'=> $this->getBaseUrl().'admin/cms_wysiwyg_images/index/'));
Then modify your WYSIWYG form field (see highlighted below),
$fieldset->addField('post_content', 'editor', array(
'name' => 'post_content',
'label' => Mage::helper('faqs')->__('Answer'),
'title' => Mage::helper('faqs')->__('Answer'),
'style' => 'width:700px; height:500px;',
'wysiwyg' => true,
'config' => $configSettings
));
for detail you can see this link and also read some comments on that
hope this will sure help you

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

Drupal AJAX Replace

I'm creating a custom user settings page. I have one field: zip_code that get's it's initial value from a custom user field. I have a custom function that pulls external data using the value of the zip_code.
I currently have the default value of the field set to the custom user field (if it's available). This is working as designed; however, I want to give the user the ability to change their zip code via an Ajax callback. This would replace the already populated radio buttons with new ones. I can't seem to wrap my head around this. Here's my code:
function settings_shopping_form($form, &$form_state) {
include_once "external.inc";
// Get user fields
global $user;
$user_fields = user_load($user->uid);
$zipcode = $user_fields->zip_code['und']['0']['value'];
if(isset($zipcode)) {
$form['zip_code'] = array(
'#title' => t('Zip Code'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $zipcode,
'#ajax' => array(
'callback' => 'settings_form_callback',
'wrapper' => 'textfields',
),
);
$storename = getmystorename($zipcode);
if(count($storename) > 0) {
$form['textfields'] = array(
'#prefix' => '<div id="textfields">',
'#suffix' => '</div>',
'#type' => 'fieldset' );
$form['textfields']['stores'] = array(
'#type' => 'radios',
'#title' => t('Choose your store:'),
'#options' => $storename,
'#default_value' => $storename[1], );
} else {
$form['textfields']['incorrect'] = array(
'#title' => t('Sorry, there are no stores available near you. Check back later'),
'#type' => 'fieldset', );
}
}
My callback function is very simple:
function settings_form_callback($form, $form_state) {
return $form['textfields'];
}
To reiterate: I want to add the ability to replace the populated radio buttons with new buttons generated by the getmystorename function when the zip_code field is changed.
I ended up taking an example from the examples module (love it!):
$defaults = !empty($form_state['values']['zip_code']) ? $form_state['values']['zip_code'] : $zipcode;
$storename = getmystorename($defaults);
I put this before the start of my form so that the values load before the form builder.

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