I created a content type called custom content type..now I want to override the content type with the form id.I included bartik_theme in template.php and custom-content-type-node-form as my form id and I created custom-content-type-node-form.tpl.php in template in bartik..but am unable to override.I try to use dpm() function to print array structure that also not working.thank you in advance..
I included this in template.php
function bartik_theme() {
return array(
'custom_content_type_node_form' => array(
'arguments' => array('form' => NULL),
'template' => 'templates/custom-content-type-node-form',
'render element' => 'form'
),
);
}
I created custom-content-type-node-form.tpl.php in template folder
<?php
dpm($form);
hide($form['body']);
print drupal_render_children($form['field_custom_image']);
print drupal_render_children($form['title']);
print drupal_render_children($form);
?>
That's because your custom theme implementation is not in the theme hook suggestions array.
Use "template_preprocess_page" hook to add a suggestion to the the hook suggestions array :
function yourtheme_preprocess_page(&$vars) {
//your checks here ...
$vars['theme_hook_suggestions'][] = 'custom_content_type_node_form';
//...
}
However if you want to change form element I suggest to use "hook_form_alter" or "hook_form_FORM_ID_alter" hook.
Related
i would like to have other template for only page (in my case node/348), so i write that i must create other
page--node--348.tpl.php
but now how can add other external css and js file ? where ?
Maybe in template.php ?
Thanks a lot and sorry for my english :)
Make a hook_node_view in one of your custom module, then check the node id is 348 and inject your css and js like this :
function nameOfYourModule_node_view($node, $view_mode, $langcode) {
if ($node->nid == 348) {
$node->content['#attached']['js'][] = array
(
'type' => 'file',
'data' => path_to_theme() . '/js/my_script.js',
'group' => JS_THEME,
'preprocess' => TRUE,
'scope' => 'footer',
'weight' => '999',
);
}
}
You can simply add a condition in "template_preprocess_html()" function in your "template.php" as below:
<?php
function THEME_NAME_preprocess_html(&$variables) {
$arg = arg();
$nid = 348;
if ($arg[1] == $nid) {
drupal_add_css(path_to_theme() . '/css/my_style.css');
drupal_add_js(path_to_theme() . '/js/my_script.js');
}
}
?>
You can use module CSS injector and JS to custom ONLY the node that you want:
https://www.drupal.org/project/css_injector
https://www.drupal.org/project/js_injector
about js, you can create a new js and custom with "id" and "class" of your node.
With css injector you can anable the changes only for the node that you want.
I have a table on my database from where I get some slug texts:
art_and_culture
business_and_financial
auto_and_moto
and display on my website through
<?php echo lang($slug_from_database); ?>
I use this method as LanguageSwticher:
multi-language-support-in-codeigniter
So in My_Controller.php I have in construct:
$this->categories_list = $this->categories_model->entries();
$list_categories['categories'] = $this->categories_list;
$this->data['sidebar_categories'] = $this->load->view('blocks/sidebar_categories', $list_categories, TRUE);
and all the categories are available on all pages from my website. The problem is when I send to the view the categories, the language is not set from the LanguageLoader.php controller witch is initialised in hooks.php (see the link example). If no language no text on echo $slug_from_database. How do you suggest to do?
I found one solution:
Created:
public function LanguageLoader() {
$site_lang = $this->session->userdata('site_lang');
if ($site_lang) {
$this->lang->load('message',$this->session->userdata('site_lang'));
} else {
$this->lang->load('message', $this->config->item('language'));
}
}
in MY_Controller.php
and call it in the constructor: $this->LanguageLoader();
and delete
$hook['post_controller_constructor'] = array(
'class' => 'LanguageLoader',
'function' => 'initialize',
'filename' => 'LanguageLoader.php',
'filepath' => 'hooks'
);
from hooks.php so now I load the language in controller`s construct not with hook.
(Using Adaptive Theme) I want a new and custom user login page. So, put this code in my template.php:
function mythemename_theme() {
$items = array();
// create custom user-login.tpl.php
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'mythemename') . '/templates',
'template' => 'user-login',
'preprocess functions' => array(
'mythemename_preprocess_user_login'
),
);
return $items;
}
Changed mythemename to my custom theme name. And created user-login.tpl.php files and put this code:
<?php
print drupal_render($form['name']);
print drupal_render($form['pass']);
print drupal_render($form['form_build_id']);
print drupal_render($form['form_id']);
print drupal_render($form['actions']);
?>
Lastly, cleared my site cache. But when i click mysite.com/user login page not changed.
Where the error am I doing? How can I solve this problem?
Try putting this in template.php, change mythemename to theme name, and flush the cache.
function mythemename_theme($existing, $type, $theme, $path){
$hooks['user_login']=array(
'render element'=>'form',
'template'=>'templates/user-login',
);
return $hooks;
}
Then in templates/user-login.tpl.php insert:
<?php
print render($form['name']);
print render($form['pass']);
print render($form['form_build_id']);
print render($form['form_id']);
print render($form['actions']);
?>
or
<?php print drupal_render_children($form) ?>
Building a custom adminhtml module. Im using a simple form. it looks like this :
<?php
class Namespace_Modulename_Block_Adminhtml_Modulename_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('modulename_form',array('legend'=>Mage::helper('modulename')->__('Module Data')));
$fieldset->addField('test', 'text', array(
'label' => Mage::helper('modulename')->__('Test'),
'name' => 'test',
));
// I want to add a custom button here. Say an action called "Add Option".
//Clicking this action adds input boxes or dropdowns to the form that are to
//to be included in the post when submitting the form (obviously).
I have been looking for a solution on Stack overflow and have not been able to find something that could help. I have then tried searching for something similar present in the Mage Core.
In the admin panel, if i go to [Catalog->Attributes->Manage Attributes] and click on a standard attribute like "Manufacturer" for example, on the second tab, called "Manage labels/Options" i see the following screen :
There is a button and action which allows me to add options (in the form of textboxes) to the form. Identifying this as something i am trying to replicate, i went into the core to try and figure out what to do. If i open the following files (Magento Enteprise 12.0.2) :
Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options
I see it is empty, but extends Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract
and I have gone through this file, but little makes sense to me. Am I going down the wrong way? How can I achieve something similar, a button and action which adds fields to a admin form?
thanks
You can set custom template for your form, and add there any tags (like button) via html.
You can add renderer for you field
$customField = $fieldset->addField('test', 'text', array(
'label' => Mage::helper('modulename')->__('Test'),
'name' => 'test',
));
$customField->setRenderer($this->getLayout()->createBlock('yuormodule/adminhtml_yourform_edit_renderer_button'));
in Block Class
class Yournamespace_Yourmodule_Block_Adminhtml_Yourform_Edit_Renderer_Button extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface {
public function render(Varien_Data_Form_Element_Abstract $element) {
//You can write html for your button here
$html = '<button></button>';
return $html;
}
}
1- First you must create a container for your Grid like this
public function __construct() {
parent::__construct();
$this->setTemplate('markavip/dataflow/edit/form/mapping.phtml');
}
2- in the Same Container you will add your buttons like
public function _prepareLayout() {
$this->setChild('add_button', $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => Mage::helper('markavip_dataflow')->__('Add Option'),
'class' => 'add',
'id' => 'add_new_option_button'
)));
$this->setChild('delete_button', $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => Mage::helper('markavip_dataflow')->__('Delete'),
'class' => 'delete delete-option'
)));
return parent::_prepareLayout();
}
public function getAddNewButtonHtml() {
return $this->getChildHtml('add_button');
}
public function getDeleteButtonHtml() {
return $this->getChildHtml('delete_button');
}
3- and in the PHTML you will add thes buttons like this :
<?php echo $this->getAddNewButtonHtml() ?>
and after this you will play in JS functions for add and hide
I'm having issues customizing a radio button for a specific form using hook_theme. Below is the piece of code I have on my module; see my comments elaborating the issue I encounter:
// Implementation of hook_form_alter().
function mymodule_form_alter(&$form, $form_state, $form_id){
// e.g form id: commerce_cart_add_to_cart_form_u6onPJSgS7pOgw0Tlo7zHy42LTQzbV913taANkYQKTo
if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) {
// Alter add to cart form
mymodule_commerce_cart_add_to_cart_form_alter($form, $form_state, $form_id);
}
}
function mymodule_commerce_cart_add_to_cart_form_alter(&$form, $form_state, $form_id) {
// Change the field type to radios.
$form['attributes']['field_artwork_ref']['#type'] = 'radios';
// Apply my custom theme for radios.
$form['attributes']['field_artwork_ref']['#theme'] = array('custom_radios');
}
// Implementation of hook_theme().
function mymodule_theme() {
return array(
'custom_radios' => array(
'variables' => array('element' => NULL),
),
);
}
function theme_custom_radios($variables) {
// Custom theme should go here.
// However, $variables are empty, print_r gives me "Array ( [element] => )."
// I am at least expecting to see my radio element here.
print_r($variables);
}
Themes for Drupal 7 form elements need to use the new render array key instead of variables in the theme definition:
function mymodule_theme() {
return array(
'custom_radios' => array(
'render element' => 'element',
),
);
}
Once you've made the change clear Drupal's caches and your code should work (I've just tested the above and it works fine).