get configuration scope selector for custom module - php

I'm working on a module where I need to save the records based on the scope of website and store. For that I need to have the select box as its in the system->configuration.
How can I get that select box in my form and save the website/store value in database so that it can be displayed in the specific store/website? Any suggestions on it?
Now I'm trying it breaking into two fields -> website and store. Now, how can I change the options in store based on the website selected?

Finally, I'd written my own code for this.
$scope = array('default' => 'default');
foreach (Mage::app()->getWebsites() as $website) {
$scope['website_' . $website->getCode()] = $website->getName();
foreach ($website->getGroups() as $group) {
$stores = array();
foreach ($group->getStores() as $store) {
$stores[] = array(
'label' => $store->getName(),
'value' => 'store_' . $store->getCode()
);
}
$scope[] = array(
'label' => $website->getName(),
'value' => $stores
);
}
}
$fieldset->addField('website', 'select', array(
'label' => Mage::helper('designer')->__('Website'),
'name' => 'website',
'values' => $scope
));
Thanks to this post by Marius

Related

Prestashop form input not visible when using HelperForm

I'm developing an prestashop module and it's just a beginning.
The problem is in the configuration page of my module.
I append some text boxes and the label and description are visible, but the textbox is not visible.
I have looked many other examples but haven't found yet any differences and what is wrong.
Mystery is that if I change the type to date or file, it's visible.
// This function called in getContent() of main module php file.
public function displayForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->trans('*'),
),
// Here is the textbox.
'input' => array(
array(
'type' => 'text',
'label' => $this->trans('Link:'),
'name' => 'LINK_PRODUCT',
'desc' => $this->trans('Please input the link...'),
'lang' => true,
'required' => true
)
),
'submit' => array(
'title' => $this->trans('Save'),
'class' => 'btn btn-default pull-right'
)
)
);
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->title = $this->displayName;
$helper->show_toolbar = false;
$helper->submit_action = 'submit';
$helper->fields_value['LINK_PRODUCT'] = Configuration::get('LINK_PRODUCT');
return $helper->generateForm(array($fields_form));
}
I would like to make the textbox visible, how should I go about doing this?
You want to have multilingual so you need to define which language is a default. Add this code to your helper definition
$helper->default_form_language = $this->context->language->id;
and replace
$helper->fields_value['LINK_PRODUCT'] = Configuration::get('LINK_PRODUCT');
with
$helper->tpl_vars = array(
'fields_value' => array('LINK_PRODUCT' => Configuration::get('LINK_PRODUCT')),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
to define all available languages and values of your LINK_PRODUCT variable.
And also, don't forget that you work with multilingual field and you need to have an array with values for your variable. So during extracting and saving you need to treat them as values for all languages. For example, if you have three languages available you need to get three values. The best way to get them is
$values = [];
foreach ($this->context->controller->getLanguages() as $language) {
$values[$language['id_lang']] = Configuration::get('LINK_PRODUCT', $language['id_lang']);
}
$helper->tpl_vars = array(
'fields_value' => array('LINK_PRODUCT' => $values),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
and when you will save it use similar combination but with updateValue

PHP/WordPress Get Users and Load into Plugin

This is my first question on StackOverflow! I'm a PHP and WP developer beginner, and I'm doing my best to learn and pull information together, but I'm stumped.
I'm using Ninja Forms for WordPress. I want to return a full list of users and have them display in the drop down selection. The code below is based off this: Foreach loop inside array.
$blogusers = get_users( array( 'fields' => array( 'user_email') ) );
$args = array(
'name' => 'Select User',
'edit_options' => array(
array(
'type' => 'select',
'name' => 'select_users_from_group',
'label' => 'Select a User',
'options' => array()
),
),
'display_function' => 'select_users_from_group',
'edit_function' => 'select_users_from_group_edit',
'sidebar' => 'template_fields'
);
foreach($blogusers as $key=>$user) {
$args['edit_options'][0]['options'][] = array(
// 'name' => $user=>user_email,;
);
}
if( function_exists( 'ninja_forms_register_field' ) ) {
ninja_forms_register_field('select_users_from_group', $args);
}
I know my foreach loop is no good, but I don't know what to do to make it work. It fails when I remove the comment.
Eventually, I'd like for another field to return a user_meta value based on the user selected here, but I have no idea where to even begin on that.
Any help is appreciated. Not looking for you to write it for me. I'd rather learn how it works. Thanks in advance!

Zend2 Form - set attributes to options in select element

I wonder how to add some attributes to select's options in Zend 2. My form element looks like this:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'brand_name',
'options' => array(
'label' => 'Choose brand',
'value_options' => $this->getBrands(),
),
));
So it's populate select looking similar to this:
<select name="brand_name"><option value="1">aaaa</option></select>
But I want to achieve something like this:
<select name="brand_name"><option value="1" class="test">aaaa</option></select>
And the classes should be different, depends on particular option. Is it possible? Why am I trying this? I need to add jquery plugin - chained which needs classes for options.
I've found a solution, array of parent select value_options should look like this:
foreach ($data as $item) {
$models[] = array(
'label' => $item->model_name,
'attributes' => array('class' => $item->id_brand_list),
'value' => $item->id);
}
If your data were array that you preferred getting from sql or db, you can code like:
public function getOptionsForDataSelect()
{
$dbAdapter = $this->_adapter;
$sql = "SELECT * FROM db_name ORDER BY db_name ASC";
$statement = $dbAdapter->query($sql);
$result = $statement->execute();
$selectData = array();
foreach ($result as $res) {
$selectData[] = array(
'label' => $res['label_data'],
'attributes' => array(
'data-key' => $res['data_id']
),
'value' => $res['value_id']
);
// $selectData[$res['id']] = $res['name'];
}
return $selectData;
}

Adding a custom filter to views in Drupal 7

im using Drupal 7 and I want to add a new filter in views.
I have a custom table "clicks" with two fields; nid and clicks_left.
The filter should just contain a checkbox "Only display nodes with clicks left". So the filter should join node and clicks on nid..
I have read like thousands of pages of custom filters but can't get it to work =)
Please, could someone show me a working example so I understand?
I have come so far that the filter is displayed under filters but what do I need to add to do the join and get the checkbox? The relevant code below:
FILE clicks_views.inc:
function clicks_views_data() {
$data = array();
$data['clicks']['clicks_filter'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'field' => 'clicks_left',
'handler' => 'clicks_handler_filter',
),
);
return $data;
}
FILE clicks_handler_filter.inc:
<?php
class clicks_handler_filter extends views_handler_filter {
???
};
I know both functions are wrong ;)
Ok, I've found a solution. For anyone who needs it:
In clicks.module
function clicks_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'clicks') . '/includes'
);
}
In clicks.views.inc
function clicks_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'clicks') . '/includes', // path to view files
),
'handlers' => array(
// register our custom filter, with the class/file name and parent class
'clicks_handler_filter' => array(
'parent' => 'views_handler_filter',
)
),
);
}
function clicks_views_data() {
$data = array();
if(module_exists('clicks')) {
$data['node']['clicks'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'handler' => 'clicks_handler_filter',
),
);
}
return $data;
}
In clicks_handler_filter.inc
class clicks_handler_filter extends views_handler_filter {
function admin_summary() { }
function operator_form() { }
function query() {
$table = $this->ensure_my_table();
$join = new views_join();
$join->construct('clicks', $this->table_alias, 'nid', 'nid');
$this->query->ensure_table('clicks', $this->relationship, $join);
$this->query->add_where($this->options['group'], "clicks.clicks_left", 0, ">");
}
}
This gives me a possibility to add a filter "clicks" that if enabled hides all results that doesn't have clicks left (clicks_left > 0)
Actually, if your values in your tables clicks are numeric you don't need to create your own handler, you can use the default from Views views_handler_filter_numeric.
You can see all handlers that already exists in the Views handlers.

How to include drupal form elements in a data table

I have a data table which is populated with data unrelated to drupal content (from a third party system). The data relates to photos which must be approved / flagged as inappropriate.
So I'm writing a Drupal admin module which should moderate this content. So far, I have built a table using theme('table',...) which shows 1 photo per row, along with some other meta data. I now want to include some form buttons in the table and build some Ajax actions which are triggered by those buttons.
This article looks relevant http://drupal.org/node/112358 but I'm concerned this isn't the Drupal 6 way of doing things.
Can anyone offer some advice on how best to approach this problem - preferably using core modules / form override functions. Drupal version is 6.14.
Custom theme functions allow you to render content in a completely custom way. You can also create a custom template for your content, this could include the buttons.
hook_theme() will allow you to create your own content type to add to the theme function so you could have theme('moderatedimages').
A workaround would be to put the HTML for the moderate buttons into the table data so that it is output by theme table. This would save you having to write your own theme function.
For the AJAX calls you will need to build your own menu callback using hook_menu() and a custom function. (Code snippets are from this tutorial.)
<?php
function mymodule_products_menu() {
$items = array();
$items['products/get'] = array(
'title' => 'mymodule callback',
'page callback' => 'mymodule_myfunction',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
This will call the function mymodule_myfunction(). One thing to remember about this function is that you want to print the result and exit. You may want to use drupal_json() to encode the response.
Why are you using the standard theme_table() function? Just make your own theme function that includes the form elements you need. More on theme functions here and here.
I had the same problem. I found the solution in drupal-directory/modules/menu/menu.admin.inc. The Form-API and Themes will be used only!
Implementation:
-write your query with scheme-api (I think you did it already for your theme('table',...) )
-for each row create a form like this:
$form[$row->id]['column_name_1'] = array(...here description of the column -> checkbox, textfield...);
$form[$row->id]['column_name_2'] = array(...);
-write your own theme-funktion, witch use hook_theme_table (you habe already did it, you need only to change some cells into checkboxes or other form-elements.)
I write a submit-function now, but it doesn't work :-) For more information see the menu-modul.
Hier my code:
/**
* Form for editing the event types.
*
* #ingroup forms
*/
function mymodule_event_type_overview_form() {
$vid = variable_get('mymodule_category_vocabulary', '');
$sql = "SELECT term_data.tid AS tid,
term_data.name AS tname,
term_data.vid AS vid,
term_site_config.color AS color,
term_site_config.site_enabled AS site_enabled,
term_site_config.site_shown AS site_shown
FROM {term_data} term_data
INNER JOIN {term_site_config} term_site_config
ON term_data.tid = term_site_config.tid
WHERE term_data.vid = %d";
$result = db_query(db_rewrite_sql($sql), $vid);
$form = array(); while ($term = db_fetch_object($result)) {
$form[$term->tid]['tname'] = array(
'#type' => 'value',
'#value' => $term->tname,
);
$form[$term->tid]['color'] = array(
'#type' => 'value',
'#value' => $term->color,
);
$form[$term->tid]['enabled'] = array(
'#type' => 'checkbox',
'#default_value' => $term->site_enabled,
);
$form[$term->tid]['shown'] = array(
'#type' => 'checkbox',
'#default_value' => $term->site_shown,
);
// Build a list of operations.
$operations = array();
$operations['delete'] = l(t('delete'), 'admin/settings/mymodule/eventtype/'. $term->tid .'/delete');
$operations['edit'] = l(t('edit'), 'admin/settings/mymodule/eventtype/'. $term->tid .'/edit');
$form[$term->tid]['operations'] = array();
foreach ($operations as $op => $value) {
$form[$term->tid]['operations'][$op] = array('#value' => $value);
} }
if (element_children($form)) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset to defaults'),
);
if (!empty($_POST) && form_get_errors()) {
drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
} }else {
$form['empty'] = array('#value' => t('There are no event types yet.')); }
return $form; }
/**
* Theme the event type overview form into a table.
*
* #ingroup themeable
*/
function theme_mymodule_event_type_overview_form($form) {
$header = array(
t('Event type'),
t('Color'),
array('data' => t('Enabled'), 'class' => 'checkbox'),
array('data' => t('Shown'), 'class' => 'checkbox'),
array('data' => t('Operations'), 'colspan' => '2'), );
$rows = array(); foreach (element_children($form) as $id) {
$element = &$form[$id];
if (isset($element['tname'])) {
$row = array(
t($element['tname']['#value']),
t($element['color']['#value']),
array('data' => drupal_render($element['enabled']), 'class' => 'checkbox'),
array('data' => drupal_render($element['shown']), 'class' => 'checkbox'),
array('data' => drupal_render($element['operations']['delete'])),
array('data' => drupal_render($element['operations']['edit'])),
);
$rows[] = $row; } } $output = ''; if ($rows) {
$output .= theme('table', $header, $rows); }
$output .= drupal_render($form);
return $output;
}
You will get the html-code of the form if you call drupal_get_form('mymodule_event_type_overview_form');
and don't forget co write following function in mymodule.module:
/**
* Implementation of hook_theme().
*/ function mymodule_theme() {
return array(
'mymodule_event_type_overview_form' => array(
'arguments' => array(),
),
);
}
Have fun :-)
Katja

Categories