Drupal Html array. How to reference a changing radio selection within a fieldset - html

I cannot make this Drupal html array fire the visible property on change of radio option. I have moved everything inside of the fieldset, not sure if this makes any difference.
Does anyone know why it isn't firing?
function services_formation_founders($form, &$form_state) {
$form = array();
$form['#tree'] = TRUE;
$form['description'] = array(
'#type' => 'item',
'#title' => t('Founders form'),
);
$form['founder']['add_officer'] = array(
'#type' => 'fieldset',
'#title' => t('Add Founder'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(TRUE,
),
),
);
$form['founder']['add_officer']['founder_type'] = array(
'#type' => 'radios',
'#options' => array(
'individual' => t('Individual'),
'corporate' => t('Corporation'),
),
'#default_value'=>'individual',
'#title' => t('What type of Founder?')
);
if (empty($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
$form['founder']['add_officer']['individual'] = array(
'#type' => 'textfield',
'#title' => t('Individual'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "individual"),
),
),
);
$form['founder']['add_officer']['corporation'] = array(
'#type' => 'textfield',
'#title' => t('Corporation'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "corporate"),
),
),
);
return $form;
}

Place $ sign at line no 2
$form = array();

Related

Dynamic value graph creation in yii2 using GoogleChart?

Here I wanna draw line chart graph by dynamic value.but in my case for every value of array created different different graph...Please help me I am first time do this task.Thanks in Advances
<?php
$modelEmployee=Employee::find()->select(['id','sales','expenses'])->all();
$arr = array('id'=>array(),
'sales'=>array(),
'expenses'=>array());
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
print_r($arr);
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => array(
array('Year', 'Sales', 'Expenses'),
array($arr['id'],$arr['sales'],$arr['expenses']),
),
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
?>
first of all the multiple graphs are because you are doing echo inside for loop so it will take only one value and create graph from that.
you have to create an array of values and pass it to the graph widget as following
$graph_data = [];
$graph_data[] = array('Year', 'Sales', 'Expenses');
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
$graph_data[] = array($arr['id'],$arr['sales'],$arr['expenses']); //add the values you require as set in the order of Year, Sales , Expenses
} //loop ends here
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $graph_data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
Try this
action
$model=Employee::find()->select(['id','sales','expenses'])->all();
$data[]=["id","sales","expenses"];
foreach ($model as $item) {
$data[]=[(string) $item['id'],(int) $item['sales'],(int) $item['expenses']];
}
return $this->render('test',['data'=>$data]);
view
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));

yii 1 relation not working in CGridView

I am trying to get relation where companies table have primary key companyID and division table have Foreign key companyID , what I need in where clause is WHERE companies.companyID = division.companies
relation in my model is :
public function relations()
{
return array(
'company' => array(self::BELONGS_TO, 'Companies', 'CompanyID'),
);
}
My Model->search() function is
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.CompanyID', $this->CompanyID, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
and my admin.php view is:
<?php
$this->breadcrumbs = array(
'Divisions' => array('index'),
'Manage',
);
$this->menu = array(
array('label' => 'List Divisions', 'url' => array('index')),
array('label' => 'Create Divisions', 'url' => array('create')),
);
");
?>
<div class="row">
<?php
$this->renderPartial('_dropdownfilter', array(
'model' => $model,
));
?>
</div><!-- end dropdown partial form -->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
?>
You need to add together=true to your criteria.
$criteria->together = true;
It'll add join to query. Some information about lazy loading http://www.yiiframework.com/wiki/527/relational-query-lazy-loading-and-eager-loading-with-and-together/
If you want to display company name,just do this in view.Don't change anything in model->search().
array(
'name'=>'Name',
'value'=>$model->company->name //here name is column name in company table.
),
In your gridview code do the following changes.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'companies',//fied from division table which refers to companyId from company table.
'header' => 'Company',
'value' => '$data->company->company_name'
),
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
And in your model->search()
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.company_name', $this->companies, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

Drupal custom form get and record current user

I looked up on the internet and write an custom module.
I want to add a form for people to add some information into database.
I use "data" module to create a table called "profile".
And my module's code is (addfood.module):
<?php
/**
* Implements hook_menu().
*/
function addfood_menu() {
$items['food/add'] = array(
'title' => '新增食物檔案',
'page callback' => 'addfood_page',
'access callback' => TRUE,
);
return $items;
}
/**
* Implements hook_permission.
*/
function addfood_permission() {
return array(
'addfood module' => array(
'title' => t('Addfood module permission'),
));
}
/**
* Returns form render array.
*/
function addfood_form($form, &$form_state) {
if (user_access('addfood module')) {
//Allowed
$form['name'] = array(
'#title' => t('名稱'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['calorie'] = array(
'#title' => t('卡路里'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('填寫卡路里(單位:千卡)'),
);
$form['water'] = array(
'#title' => t('水'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['protein'] = array(
'#title' => t('蛋白質'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['saturated_fat'] = array(
'#title' => t('飽和脂肪'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['trans_fat'] = array(
'#title' => t('反式脂肪'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['carbohydrates'] = array(
'#title' => t('碳水化合物'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['dietary_fiber'] = array(
'#title' => t('膳食纖維'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['cholesterol'] = array(
'#title' => t('膽固醇'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['sodium'] = array(
'#title' => t('鈉'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['sugars'] = array(
'#title' => t('糖'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['notes'] = array(
'#title' => t('Please explain your what makes you a prime candidate for our beta test'),
'#type' => 'textarea',
'#resizable' => TRUE,
'#description' => t('Beta spaces are limited, but let us know if there is a really good reason to let you in.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
} else {
//Access denied
header('Location: ../user/login?destination=food/add');
}
}
/**
* Menu callback.
*/
function addfood_page() {
return drupal_get_form('addfood_form');
}
/**
* Submission handler for form_example -> Insert into database
*/
function addfood_form_submit($form, &$form_state) {
$fe_id = db_insert('profile')
->fields(array(
'name' => $form_state['values']['name'],
'calorie' => $form_state['values']['calorie'],
'water' => $form_state['values']['water'],
'protein' => $form_state['values']['protein'],
'saturated_fat' => $form_state['values']['saturated_fat'],
'trans_fat' => $form_state['values']['trans_fat'],
'carbohydrates' => $form_state['values']['carbohydrates'],
'dietary_fiber' => $form_state['values']['dietary_fiber'],
'cholesterol' => $form_state['values']['cholesterol'],
'sodium' => $form_state['values']['sodium'],
'notes' => $form_state['values']['notes'],
))
->execute();
drupal_set_message(t('HO GYA Submit!!!!'));
return $form;
}
?>
If I want to record who add the information automatically(by get the current username).
Anyone has idea how to do it? Thank you.
Figure is my table schema.
(source: libk.info)
You can use global user variable to get current user. You can test it:
<?php
global $user;
print_r($user->name);
?>

How to add raw HTML in Yii CMenu label

I create a menu like a Twitter Bootstrap navbar with CMenu widget:
<?php
$this->widget( 'zii.widgets.CMenu', array(
'items' => array(
array(
'label' => 'Home',
'url' => array( '/site/index' ),
),
array(
'label' => 'Dropdown <b class="caret"></b>',
'url' => '#',
'submenuOptions' => array( 'class' => 'dropdown-menu' ),
'items' => array(
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/create' ),
),
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/list' ),
),
),
'itemOptions' => array( 'class' => 'dropdown' ),
'linkOptions' => array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' ),
),
'htmlOptions' => array( 'class' => 'nav' ),
)); ?>
This code generate menu with 2 items in it and 1 submenu for second menu item. Fine. But only thing, which not worked is 'label' => 'Dropdown <b class="caret"></b>', in 9th line. It rendered as Dropdown <b class="caret"></b> on page. So I see caption 'Dropdown <b class="caret"></b>' instead of Dropdown ▼.
How I can change code to show unescaped HTML in menu label?
Thanks for your attention.
You have to set encodeLabel attribute of CMenu to false
<?php
$this->widget('zii.widgets.CMenu', array(
'encodeLabel' => false,
'htmlOptions' => array('class' => 'nav'),
'items' => array(
array(
'label' => 'Home',
'url' => array('/site/index'),
),
array(
'label' => 'Dropdown <b class="caret"></b>',
'url' => '#',
'submenuOptions' => array('class' => 'dropdown-menu'),
'items' => array(
array(
'label' => 'Submenu Item 1',
'url' => array('/user/create'),
),
array(
'label' => 'Submenu Item 1',
'url' => array('/user/list'),
),
),
'itemOptions' => array('class' => 'dropdown'),
'linkOptions' => array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'),
),
),
));
?>
My solution is creating CMenu extension:
layout/main.php
'submenuOptions'=>array('class'=>'dropdown-menu'),
'itemOptions'=>array('class'=>'dropdown'),
'linkOptions'=>array('class'=>'dropdown-toggle', 'data-toggle'=>'dropdown'),
// Dropdown arrow toggle
'dropdownArrow'=>true,
ext/widgets/BootstrapCMenu.php
class BootstrapCMenu extends CMenu {
protected function renderMenuItem($item)
{
if(isset($item['url']))
{
$item['label'] .= ($item['dropdownArrow']) ? ' <b class="caret"></b>' : '';
$label=$this->linkLabelWrapper===null ? $item['label'] : CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']);
return CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
}
else
return CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
}
}
In Yii 2.0, for adding glyphicon in navbar menu you can follow the below info.
Edit in vendor\yiisoft\yii2-bootstrap\Nav.php under renderItem function the following code:
if(isset($item['icons']))
$label=Html::tag('span', '', ['class' => 'glyphicon glyphicon-'.$item['icons']]).$label;
Now, you can directly use any icon from your code with icons option as
<?php
$this->widget( 'zii.widgets.CMenu', array(
'items' => array(
array(
'label' => 'Home',
'url' => array( '/site/index' ),
'icons'=> 'home',
),
array(
'label' => 'Dropdown <b class="caret"></b>',
'url' => '#',
'submenuOptions' => array( 'class' => 'dropdown-menu' ),
'items' => array(
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/create' ),
),
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/list' ),
),
),
'itemOptions' => array( 'class' => 'dropdown' ),
'linkOptions' => array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' ),
),
'htmlOptions' => array( 'class' => 'nav' ),
)); ?>
You can make the corresponding changes even in older versions of yii.

Drupal Tableselects

I can (finally!!!) set a query to return results for a checkbox(es) set within a collapsible fieldset in Drupal 7- however when I try to put it into a tableselect, I get no results. Would someone be able to check out this code and see if you can tell me why? I also have a screen shot of both results- but since I am new here it won't allow me to post it.
$form = array();
$secnum = 1;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$options = array();
foreach ($result as $record) {
$options[$record->secser_id] = $record->ser_name;
}
$form['secser']['1'] = array(
'#title' => t('Basic Sanitation'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$form['secser']['1']['secser'] = array(
'#title' => t('Choices'),
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#options' => $options,
'#description' => t('choose!'),
);
$form['secser']['2'] = array(
'#title' => t('Community Systems'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$secnum = 2;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$opt2 = array();
foreach ($result as $record) {
$opt2[$record->secser_id] = $record->ser_name;
}
$header = array(
'ser_name' => t('Choose Service(s)'),
);
$form['secser']['2']['secser'] = array(
'#type' => 'tableselect',
'#title' => t('Community Systems'),
'#header' => $header,
'#options' => array($opt2),
'#multiple' => TRUE,
);
I haven't tried running your code, but it looks like your options are an array which contains another array.
#options' => array($opt2),
because $opt2 is an array, this should be:
#options' => $opt2,
the same way you have the options set up in your checkboxes