Drupal 7 Form API - Custom Select Theme - php

I have been searching all day but unable to find any answers - I am sure I am doing it right as worked fine in Drupal 6 and should work fine in Drupal 7.
I want to give a custom theme function to my select element in my form
$form['field_name'] = array(
'#type' => 'select',
'#title' => t('Title Here'),
'#theme' => 'custom_select',
'#options' => $values,
);
I have the theme hook right to declare the new custom theme function but my problem is when using that custom theme function as above I get an empty $variables array which just reads
Array([element] => null)
can anyone see what I may be doing wrong? cleared cache, done everything I can think of - any ideas why Drupal is not passing the element data to the theme function? thanks

well finally figured this one out incase any one else has the problem - make sure you set render element in hook_theme and not variables!
before
function hook_theme(){
return array(
'select_currency' => array(
'variables' => array('element' => null),
'file' => 'module_name.theme.inc',
));
}
after
function hook_theme(){
return array(
'select_currency' => array(
'render element' => 'element',
'file' => 'module_name.theme.inc',
));
}
I was pulling my hair out until I remembered the render element!

Related

categoryChoiceTree in prestashop module configuration page

I'm developing a prestashop module and I'm trying to show a category tree in my backoffice configuration page.
I'm trying to follow this instructions below but I don't know exactly where to add this code.
It should be inside main module's php? or inside a separate .php file and call it from the main one (don't know how to do it either).
As much time I'm spending trying to figure out, how to implement the code in the link above, the more I think I'm losing my time.
I see that "use" files, and this JS, " /admin-dev/themes/new-theme/js/components/form/choice-tree.js " are not in any prestashop folders.
Well, you should invest some time and learn Symfony since this is what you need to build backend modules for Prestashop 1.7.
As a pointer, you need to create a form class extending the CommonAbstractType, add a build form method. e.g. :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->context = Context::getContext();
$parents = [
['id_category' => 2, 'name' => 'Home', 'children' => $this->getSubCategories(1, true, 2)]
];
$builder->add('category', CategoryChoiceTreeType::class, [
'choices_tree' => $parents,
'choice_value' => 'id_category',
'choice_children' => 'children',
'choice_label' => 'name',
'disabled_values' => $disabledCategories,
'label' => 'Choose a category'
])
then add methods for retrieving the data to populate the form fields.
Then use this class in your controller and display the form:
$form = $this->createForm(YourFormForm::class);
Also add a processForm to process data.
As mentioned, this is not a copy/paste situation you need to understand the Symfony workflow.
The only way that I found to "paint" the categorytree in my configuration page is adding this code to the inputs form array:
Can anyone tell me how to retrieve users selection data to my database?
It does not work as any other form field.
array(
'type' => 'categories',
'label' => $this->l('Destination Category'),
'desc' => $this->l('Select ONE Category'),
'name' => 'CATEGORY_CATEGORY_TO',
'tree' => [
// 'selected_categories' => [],
'disabled_categories' => null,
'use_search' => false,
'use_checkbox' => false,
'id' => 'id_category_tree',
],
'required' => true
),
Well, it is SOLVED!!!! Finally it was very simple, but you must get the correct info for you particular case.
#Robertino's answer might be the best implementation, I don't know, but it became impossible to solve for me,
I uses this code below, and called $categoryTree from the form input. This input must be type=> categories_select
Thanks for your time, and for the help of another post from this forum.
$root = Category::getRootCategory();
//Generating the tree
$tree = new HelperTreeCategories('categories_1'); //The string in param is the ID used by the generated tree
$tree->setUseCheckBox(false)
->setAttribute('is_category_filter', $root->id)
->setRootCategory($root->id)
->setSelectedCategories(array((int)Configuration::get('CATEGORY_1'))) //if you wanted to be pre-carged
->setInputName('CATEGORY_1'); //Set the name of input. The option "name" of $fields_form doesn't seem to work with "categories_select" type
$categoryTree = $tree->render();
And the Form:
array(
'type' => 'categories_select',
'label' => $this->l('Category'),
'desc' => $this->l('Select Category '),
'name' => 'CATEGORY_1', //No ho podem treure si no, no passa la variable al configuration
'category_tree' => $categoryTree, //This is the category_tree called in form.tpl
'required' => true

WordPress theme behaving bad

Hope you all are doing great since last few days i am facing an issue with my WordPress site this is a plugin or theme i am not sure but the file is in theme which throws error of undefined function
because of this error i am not able to open my customize menu from wordpress admin panel
and once i commented the code line where this code exists it start working but widgets does not work properly like if i enable social icons in widgets i wont be able to disable them and save settings here is the code i have commented.
$jobcareer_opt_array = array(
'name' => esc_html__('Title', 'jobcareer'),
'desc' => '',
'hint_text' => '',
'echo' => true,
'field_params' => array(
'std' => esc_attr($jobcareer_widget_title),
/*'id' => cs_allow_special_char($this-
>get_field_id('title')),*/
'classes' => '',
/*'cust_id' => cs_allow_special_char($this-
>get_field_name('title')),*/
/*'cust_name' => cs_allow_special_char($this-
>get_field_name('title')),*/
'return' => true,
'required' => false
),
);
May be there is some version conflict between the theme and Codestar plugin. You can ask your theme's developer for that. Or as a temporary solution just remove cs_allow_special_char function and use its parameter insteaad. For example replace
cs_allow_special_char($this->get_field_name('title'))
with
$this->get_field_name('title')
and it should work then.
I checked the plugin's source, that function doesn't do any critical operation:
function cs_allow_special_char($input = ''){
$output = $input;
return $output;
}

Drupal form submit action changing at random times

I have created a custom Drupal module/form that when submitted the action takes you to another page and posts data with it. Pretty standard stuff.
Randomly, the form action changes at random (not set) intervals during the day that are not correlating to anything immediately obvious - for example cron runs. It only happens maybe once or twice a day, so if anyone has any idea what might cause this or point me in the right direction.
Anything anyone feels needs adding, let me know.
The is the custom module code:
/**
* Test form declaration
*/
function test_form($form, &$form_state){
$form['#attributes'] = array('id' => "test-form");
$form['search-field'] = array(
'#type' => 'textfield',
'#title' => t('<span class="highlighted">Test</span>'),
'#attributes' => array(
'class' => array('form-control form-text'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Search',
'#attributes' => array(
'class' => array('btn btn-default text-hide'),
),
);
return $form;
}
/**
* search form submit
*/
function test_form_submit($form, &$form_state){
$searchQuery = $form_state['values']['search-field'];
$query = array();
if($search <> ''){
$query = array(
'field_geofield_distance[distance]' => '100',
'field_geofield_distance[unit]' => '3959',
'field_geofield_distance[origin]' => $search
);
}
drupal_goto('test-page', array('query' => $query));
}
If your form creates data (let's say a node) possibly you have a redirection on rules when creating a node that with specific values redirects to another page.
Look for rules that are activated when content creation occur or when submission occur.
Also you could have some redirection magic in the top of Drupal, like a wrong configuration of an .htacess file. If your form does the same thing always it should behave the same always. And why it's redirecting to page of old site?
Finally, if the new site have code from the old site the check submission, creation and form altering hooks.
Hope that helps.

Drupal : Create variable from custom .module to custom .tpl.php

I create a custom module and I want use the complete_url of my future website in the template who used/created by my module.
I tried several ways and searched Google but I don't find a solution.
However this issue seems very simply, that became me crazy x)
So, I must create a variable in my .module and add her when I return the array in the main_socialtag_theme function. Where/How can I do that ?
My .module:
function main_socialtag_block_info(){
$block['main_socialtag']=array(
'info' => t('Main socialtag'),
'cache' => DRUPAL_NO_CACHE,
);
return $block;
}
function main_socialtag_theme(){
return array(
'main_socialtag_block' => array(
'template' => 'theme/main_socialtag_block',
'variables' => array(),
),
);
}
function main_socialtag_block_view($delta=''){
if ($delta == 'main_socialtag'){
return array(
'subject' => '',
'content' => array(
'#theme' => 'main_socialtag_block',
)
);
}
}
Thanks for help, and sorry for my bad english writing !
If you are looking for a way to add, modify and call variables. Check variable_get and variable_set.
To add or modify a variable value:
variable_set("my_variable_name", "value");
To retrieve the value:
$myVal = variable_get("my_variable_name", "");
For hook_theme Implementation, kindly check this question.

Drupal 7 Field api checkbox not saving

I'm trying to get a Drupal 7 Field working programmatically and it's not going well.
I simply want to create a module that creates a Field with a checkbox. The module is working, the field is working but the checkbox will not save.
Here is my hook_field_widget_form:
<?php
function add_to_basket_field_widget_form(&$form,&$form_state,$field,$instance,$langcode,$items,$delta,$element)
{
$element += array(
'#type' => 'checkbox',
'#title' => t('Add to basket?'),
'#default_value' => isset($item['add_to_basket_cfield']) ? $item['add_to_basket_cfield'] : '',
);
return $element;
}
?>
When I run it as part of my module it works but wont save a tick or no tick.
I've tried everything I can think of to get this working but after 3 days with nothing to show it's time to ask the experts
Thanks
hope this helpful for you
$form[$group]['ex_account'] = array(
'#type' => 'checkbox',
'#title' => t('your title'),
'#default_value' => variable_get('ex_account', 1) ? 1 : 0,
'#description' => t('desc.'),
);
return system_settings_form($form); or return $form;
Try the example module and look at the field_example portion. Make sure that your custom field has a schema described in the .install file so that the value you are putting in your custom field actually saves.

Categories