drupal admin.inc, loop list_modules as form settings - arrays

I need to loop the values of this
$blocks = array();
$blocks['list_modules']
with this:
$form['advanced']['custom_acc'] = array(
'#type' => 'select',
'#title' => t('module title'),
'#options' => array(
0 => t('No control'),
1 => t('Controlled'),
),
);
so it should become something like this
while (list(, $value) = each($blocks)) {
echo "
$form['advanced']['custom_->blocks array value'] = array(
'#type' => 'select',
'#title' => t('->blocks array value'),
'#options' => array(
0 => t('No control'),
1 => t('Controlled'),
),
);
";
}
but of course this is not working, got any ideas? I guess I have to use an foreach, I hope you can see what Im trying to achieve,
thanks in advance!

fixed it myself :)
$blocks = array();
$blocks = module_list();
foreach ($blocks as $key => $title) {
$form['advanced'][$title] = array(
'#type' => 'select',
'#title' => t($title),
'#options' => array(
0 => t('No control'),
1 => t('Controlled'),
),
);
}

Related

How to get rid of null associated model in cakephp 2.x

I am trying to get the item types that Order.Item.ItemType.show_type = 1. I have written the query but I want to show only Items that their ItemType.show_type = 1 not all items.
$brief = $this->Order->find('first', array(
'fields' => array(
'Order.*'
),
'conditions' => array(
'Order.order_id' => $orderId,
),
'contain' => array(
'Item' => array(
'fields' => array(
'Item.*', 'CHAR(64 + Item.num) AS letter'
),
'conditions' => array(
'Item.deleted' => 0,
),
'ItemType' => array(
'conditions' => array(
'ItemType.show_type' => 1
),
)
),
)
));
The query shouldn't show Item id = 25741
Associations:
// Order
public $hasMany = array(
'BriefInstalment' => array(
'foreignKey' => 'order_id'
)
);
// Item Model
public $belongsTo = array(
'Order',
'ItemType' => array(
'type' => 'inner'
)
);
// ItemType Model
public $hasMany = array('Item');
Print:
array(
'Order' => array(
'order_id' => '67817',
'service' => '',
),
'Item' => array(
(int) 0 => array(
'id' => '25741',
'order_id' => '67817',
'num' => '2',
'item_type_id' => '8',
'name' => '3-5 titles active',
'deleted' => false,
'ItemType' => array(), // <= how to remove this empty model
'Item' => array(
(int) 0 => array(
'letter' => 'B'
)
)
),
(int) 1 => array(
'id' => '25742',
'order_id' => '67817',
'num' => '3',
'item_type_id' => '2',
'name' => '1,000 pro active',
'deleted' => false,
'ItemType' => array(
'id' => '2',
'name' => 'Part Instalment',
'show_type' => true,
'deleted' => false
),
'Item' => array(
(int) 0 => array(
'letter' => 'C'
)
)
)
)
)
This could not be done using Countaible behaviour, but iwth the joins method, set the recursive to -1
$brief = $this->Order->find('first', array(
'recursive' => -1,
'fields' => array(
'Order.*'
),
'conditions' => array(
'Order.order_id' => $orderId,
),
'joins' => array(
array(
'table' => 'items',
'alias' => 'Item',
'type' => 'inner',
'conditions' => array(
'Order.id = Item.order_id'
)
),
array(
'table' => 'item_types',
'alias' => 'ItemType',
'type' => 'inner',
'conditions' => array(
'ItemType.id = Item.item_type_id'
)
),
)
));
You have to check if the table names are correct and also the foreign keys names.
Another solution would be to go through your results, and unset the empty ones
foreach($brief as $k => $v){
foreach($v['Item'] as $kk => $vv){
if(empty($vv['ItemType'])){
unset($brief[$k]['Item'][$kk];
}
}
}
debug($brief);

how to get the submitted value of tableselect in drupal 7

function test($form, &$form_state){
$form = array();
$header = array(.............);
$values = array(.............);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => $IsCheckbox,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state){
$selected = $form_state['values']['table'];
drupal_set_message($selected) // displays array index (0,1,2 etc)
return;
}
How to get the selected table row values in Drupal form. Need assistance on the issue. Any help would be appreciated.
What you get in your $selected is the index of $rows that you have selected in your table. To get the values in $rows you need to use the index that you have in $selected.
I created an easy example how to do it here:
function test($form, &$form_state)
{
$form = array();
$header = array(
'first_name' => t('First Name'),
'last_name' => t('Last Name'),
);
$rows = array(
// These are the index you get in submit function. The index could be some unique $key in database.
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => true,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state)
{
// This function should not be duplicated like this but It was easier to do.
$rows = array(
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$names = array();
// Remove the names that has not been checked
$selected_names = array_filter($form_state['values']['table']);
// Iterate over the indexes that was selected to get the data from original array
foreach ($selected_names as $index ) {
array_push($names, $rows[$index]);
}
foreach($names as $name) {
drupal_set_message($name['first_name'] . ' ' . $name['last_name']);
}
}

How to assign mysql functions on join fields in Cakephp

I want to get last_deadline and the count of instalments of all instalments but obviously this query will show me 1 order and the last_deadline.
$orders = $this->find('all', array(
'fields' => array(
'Order.order_id', 'Order.summary', 'Order.fee',
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline'
),
'conditions' => $conditions,
'joins' => array(
array(
'table' => $this->getTableName('default', 'brief_instalments'),
'alias' => 'BriefInstalment',
'type' => 'RIGHT',
'conditions' => array(
'Order.order_id = BriefInstalment.order_id',
'BriefInstalment.deleted' => 0,
),
'order' => 'BriefInstalment.deadline ASC',
)
),
'order' => 'BriefInstalment.deadline ASC'
));
I have tried 'contain' and doesn't work.
'contain' => array(
'BriefInstalment' => array(
'fields' => 'BriefInstalment.id',
'fields' => array(
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline', 'COUNT(`BriefInstalment`.`id`) AS total_instalments'
),
'conditions' => array(
'BriefInstalment.deleted' => 0
)
)
),
By the way I don't want to use loop to get last_intalments and cout brief_instalments. e.g.
// Determine deadlines
foreach ($orders as $i => $order) {
$deadline = $this->BriefInstalment->getLastDeadline($order['Order']['order_id']);
$orders[$i] += array(
...
'last-deadline' => $deadline,
'total-instalments' => count($order['BriefInstalment'])
);
}
The reason is it decrease the speed of loading.
Any help plz
Here is how to create custom query
$conditionsSubQuery['"User2"."status"'] = 'B';
$db = $this->User->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('"User2"."id"'),
'table' => $db->fullTableName($this->User),
'alias' => 'User2',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->User
);
$subQuery = ' "User"."id" NOT IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$this->User->find('all', compact('conditions'));
Reference:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries

pagination weird behavior with search filter in cakephp

i wish to have a search form with one textbox, 2 dropdowns and a radio button (in cakephp). But this is to be done with pagination. Search and pagination work fine separately but not together. when i select the filter criteria and click on search button, the results are displyed fine on 1st page, but if i click on any of the pagination links, the filter criteria are lost and pagination data is displayed without any filter. How do i solve this?
controller code:
private function _index() {
$this->genre_list();
$this->language_list();
$conditions = array();
debug($this->postConditions($this->data)); die;
if (!empty($this->request->data['Artist'])) {
foreach ($this->request->data['Artist'] as $name => $record) {
if (isset($record) && !empty($record)) {
if ($name == 'name') {
$conditions = array(
$this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
);
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
};
$this->paginate = array(
'contain' => array (
'User' => array(
'City',// => array('name'),
'State',// => array('name'),
'Country',// => array('name'),
),
'Genre',// => array('name'),
'Language',// => array('language'),
),
'limit' => 3,
'conditions' => $conditions
);
$data = $this->paginate($this->modelClass);
//~ debug($data);
$this->set(compact('data'));
}
view:
<?php
echo $this->Form->create('Search', array(
'type' => 'file',
'inputDefaults' => array(
'format' => array(
'label', 'between', 'input', 'error', 'after'
)
),
'class' => 'form-horizontal'
));
echo $this->Form->input('Artist.name', array(
'div' => 'control-group',
'label' => array(
'class' => 'control-label',
'text' => 'Artist Name'
),
'between' => '<div class="controls">',
'after' => '</div>',
'placeholder' => 'Name',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
echo $this->Form->input('Artist.genre_id', array(
'div' => 'control-group',
'empty' => 'All',
'label' => array(
'class' => 'control-label',
'text' => 'Genre'
),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
echo $this->Form->input('Artist.language_id', array(
'div' => 'control-group',
'empty' => 'All',
'label' => array(
'class' => 'control-label',
'text' => 'Select Lanuage'
),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
?>
<?php echo $this->element('pagination');
edited code with session
private function _index() {
$this->genre_list();
$this->language_list();
$conditions = array();
//~ foreach($this->request->params['named'] as $key => $record) {
//~ debug($this->request->params['named']);
//~ $this->request->data['Search'][$key] = $record;
//~ }
if (!empty($this->request->params['named']['page'])) {
// use session data for conditions
$conditions = (array)$this->Session->read('_indexConditions');
} else {
// delete session data
$this->Session->delete('_indexConditions');
}
$conditions = array();
if (!empty($this->request->data)) {
// new search! use the data to make conditions,
// like you did, and save the conditions
//~ if (!empty($this->request->data['Artist'])) {
foreach ($this->request->data['Search'] as $name => $record) {
if (isset($record) && !empty($record)) {
if ($name == 'name') {
$conditions = array(
$this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
);
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
//~ }
$this->Session->write('_indexConditions', $conditions);
}
$this->paginate = array(
'contain' => array (
'User' => array(
'City',
'State',
'Country',
),
'Genre',
'Language',
),
'limit' => 3,
'conditions' => $conditions
);
$data = $this->paginate('Artist');
$this->set(compact('data'));
}
The answer is pretty simple: save the search conditions in the session or cookie and use those conditions if new ones aren't sent.
For simplicity's sake, I've omitted much of your code. Something like this should work.
private function _index() {
// check if this is a pagination request without data
if (!empty($this->request->params['named']['page']) {
// use session data for conditions
$conditions = (array)$this->Session->read('_indexConditions');
} else {
// delete session data
$this->Session->delete('_indexConditions');
}
$conditions = array();
if (!empty($this->request->data)) {
// new search! use the data to make conditions,
// like you did, and save the conditions
$this->Session->write('_indexConditions', $conditions);
}
$this->paginate = array(
'conditions' => $conditions
);
$data = $this->paginate($this->modelClass);
$this->set(compact('data'));
}
It should be wrapped in a component. On one of my projects I have a component that automatically does this and replaces the controller data for a more automated process.
got the solution without using session
in controller:
if (!empty($this->request->data) || $this->request->params['named']) {
foreach($this->request->params['named'] as $key => $record) {
if($key!= 'page')
$this->request->data['Search'][$key] = $record;
}
foreach ($this->request->data['Search'] as $name => $record) {
if (isset($record) && !empty($record)) {
$this->request->params['named'][$name] = $record;
if ($name == 'name') {
$conditions[$this->{$this->modelClass}->User. 'User.name LIKE'] = '%' . $record . '%';
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
}
and in view provide the controller and action explicitly:
echo $this->Form->create('Search', array(
'type' => 'file',
'inputDefaults' => array(
'format' => array(
'label', 'between', 'input', 'error', 'after'
)
),
'url' => array(
'controller' => 'artists',
'action' => 'index',
),
'class' => 'form-horizontal'
));
solves the problem.
In 2.x, you can set your form to use GET instead of POST.
Change the type of your form from file to GET (BTW, why you use file??)
echo $this->Form->create('Search', array(
'type' => 'get',
And of course, instead of using the data array from Request
$this->request->data['Search']['name']
you will use the query array like this:
if (!empty($this->request->query['name'])) {

drupal #default_value not applied in select option in drupal_render

I try to print a drupal 'select option' element in a form .I think drupal_render not applying #default_value.every thing is ok except #default_value not applied.
where is the problem?anybody know how i can do this?
do #default_value accept string value?
this is pseudo of my codes:
function test_menu(){
$items=array();
$items['admin/config/regional/test']=array(
'title' => 'test',
'description' => t('test'),
'page callback' =>'drupal_get_form',
'page arguments' => array('test_function'),
);
$items[]=array();
return $items;
}
function test_function(){
$header = array
(
'test1' => t('test1'),
'test2'=> t('test2'),
);
$a=(1,2,3);
$$options=array();
foreach($a as $i=>$v)
{
$f['type'] = array(
'#type' => 'select',
'#options' => array(1,2,3,4),
'#default_value'=>1,
);
$options += array($name=>array( 'test1' => $v,
'test2'=> drupal_render($f['type']) ,
}
$form['table'] = array
(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE
//'#empty' => t('No users found'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
I test textfield but its also not work and not accept #default_value in drupal_render
$f['test3']=array(
'#type'=>'textfield',
'#title'=>'test3',
'#default_value' =>'aaa',
);
I suppose this is beacuse using drupal_render .anybody have a solution?
In Drupal_render 's used in drupal_get_form , #default_value not set use must use #value instaed of it.
$f['type'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array('1','2','3','4')),
'#value'=> '1',
);
The following code doesn`t work:
$form['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
$form['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
return $form;
But then i did the following:
$form['group'] = array('#tree' => TRUE);
$form['group']['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
$form['group']['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
return $form;
And default values now works.
I have the same problem. finally, i found one method. As you said, the default_value does not work. So make the default_value fixed as 0. and change the options array, put the default value on the top.
If you look at the example from Drupal's Form API, you'll see that the #options setting takes a key-value pair array and in the #default_value, you should specify the key of the default value, not the string value.
Also, according to documentation for the #options setting, #options is expecting String values. So your select should be more like:
$f['type'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array('1','2','3','4')),
'#default_value'=> '1',
);

Resources