Wordpress Metabox populate select from array - php

I'm trying to populate a select with the registered users (contributors) from an array to use it in the frontend, but for some reason I can't get it done.
Here's my code:
$user_query = new WP_User_Query(array('role' => 'Contributor'));
if (!empty($user_query->results))
{
$index = 0;
$contributors = array();
foreach ($user_query->results as $user)
{
$contributors[$index] = $user->ID;
$index++;
}
}
array(
'name' => esc_html__('Select', 'your-prefix'),
'id' => "{$prefix}tecnico",
'type' => 'select',
// Array of 'value' => 'Label' pairs for select box
'options' => $contributors,
// Select multiple values, optional. Default is false.
'multiple' => false,
'std' => 'value2',
'placeholder' => esc_html__('Select an Item', 'your-prefix'),
),

Related

push one more element to array php

I have an array like this:
return array(
'User Management -> Role Management' => array(
array(
'permission' => 'role-management.view',
'label' => 'View',
),
array(
'permission' => 'role-management.create',
'label' => 'Create',
),
array(
'permission' => 'role-management.edit',
'label' => 'Edit',
),
array(
'permission' => 'role-management.delete',
'label' => 'Delete',
),
),
'User Management -> User Management' => array(
array(
'permission' => 'user-management.view',
'label' => 'View',
),
array(
'permission' => 'user-management.create',
'label' => 'Create',
),
array(
'permission' => 'user-management.edit',
'label' => 'Edit',
),
array(
'permission' => 'user-management.delete',
'label' => 'Delete',
),
),
);
I have accessed this array successfully like this:
$permissions = Config::get('permissions');
I want to add new index below label. I have tried this but could not add.
`foreach ($permissions as $permission) {
foreach ($permission as $eachPermission) {
$encodedPermission = base64_encode($eachPermission['permission']);
// $eachPermission['encodedPermission'] = $encodedPermission;
array_push($eachPermission, "encodedPermission", $encodedPermission);
}
}
var_dump($permissions); `
I have tried these but could not set the new index. My expected result was this:
'permission' => 'role-management.view',
'label' => 'View',
'encodedPermission' => 'someencodedstring'
Am I doing it wrong or missing something.
Your array is multi-level, so you need to do double-foreach to reach the level required for your changes.
And for your changes being saved in the original array, you need to use ampersand (&) before your iterators to keep the reference to the original array. Without it, you would insert the new data only to the copy of the array that was created for your foreach loop.
This works:
foreach ($permissions as &$eachPermission) {
foreach ($eachPermission as &$singlePermission) {
$encodedPermission = base64_encode($singlePermission['permission']);
array_push($singlePermission, "encodedPermission", $encodedPermission);
}
}
var_dump($permissions);
i would do it like this just because it is easier to understand ...
foreach($permissions as $name1=>$ar1){
foreach($ar1 as $name2=>$ar2){
$permissions[$name1][$name2]['encodedPermission'] = base64_encode($ar2['permission']);
}
}

PrestaShop FormHelper preload multilanguage values

here I am seeing a small problem, I have two tables template and template_text, in template table I store the template type and visibility data and in the template_text table I store the template text in many languages, atm. I have two langs. such as EN and DE. So, when I am editing the template I preload the current template values into the form, but there is a problem with the values like autoemailer_title and autoemailer_body, when the from helper generates the form field it is generating this fields this way autoemailer_title_1, autoemailer_title_2 according the language ID, so, the question would be how to preload all this values the right way? I could use javascript and ajax get request to get the values and then set them by using the input ID, but I think there is a better way using the form helper.
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l($params['title']),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select', // This is a <select> tag.
'label' => $this->l('Template type'), // The <label> for this <select> tag.
'desc' => $this->l('Choose a template type'), // A help text, displayed right next to the <select> tag.
'name' => 'autoemailer_type', // The content of the 'id' attribute of the <select> tag.
'required' => true, // If set to true, this option must be set.
'options' => array(
'query' => array(
array(
'id_option' => 'static', // The value of the 'value' attribute of the <option> tag.
'name' => 'Static' // The value of the text content of the <option> tag.
),
array(
'id_option' => 'dynamic',
'name' => 'Dynamic'
),
), // $options contains the data itself.
'id' => 'id_option', // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
'name' => 'name' // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
)
),
array(
'type' => 'text',
'label' => $this->l('News letter title'),
'name' => 'autoemailer_title',
'desc' => $this->l('Title'),
'lang' => true,
// 'style' => 'width:300px',
'class' => 'fixed-width-lg',
),
array(
'type' => 'textarea',
'label' => $this->l('Text'),
'name' => 'autoemailer_body',
'desc' => $this->l('Email body'),
'lang' => true,
'cols' => 60,
'rows' => 10,
'class' => 'rte', // we need this for setuping the tiny mce editor
'autoload_rte' => true, // we need this for setuping the tiny mce editor
),
),
'submit' => array(
'title' => $this->l('Save')
)
)
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'newTemplate';
$helper->currentIndex = $this->getThisUrl() . '&emailer_action=main';
$helper->token = Tools::getAdminTokenLite('AdminModules');
return $helper->generateForm(array($fields_form));
Solved this one. So, when we have more than one lang then the attribute for which we apply the multilangiage option becomes an array, so to set the value according the language we can just do this way :
$lang_id = 1; // setting the language ID, usually it's done by foreaching the records from ps_lang table
$helper->fields_value['description'][$lang_id] = 'my default value'; // setting the default value
return $helper->generateForm(array($fields_form)); // returning the output of generated form

How to populate list box in yii by using the following code

$selected = array( '5' => array('selected' => 'selected'),'6' => array('selected' => 'selected'),);
$htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'true', 'options' => $selected);
echo $form->listBox($model,'team_members', $mem_arr, $htmlOptions);
From the above code it works properly. But I have selected variable as an array variable..
then how do i give instead of '$selected' array
If I understand your question correctly, you could do this (not tested):
$result = array(5,6);
$selected = array():
foreach ($result as $selectedIndex) {
$selected[$selectedIndex] = array('selected'=>'selected');
}
$htmlOptions = array(
'size' => '5',
'prompt'=>'Use CTRL to Select Multiple Staff',
'multiple' => 'true',
'options' => $selected);
echo $form->listBox($model,'team_members', $mem_arr, $htmlOptions);

Drupal check boxes creation

I have the form in drupal7.
What i need is: i have category and sub category . I have to list these in checkboxes like
I used the code as
$form['rate_the_room']['talent'] = array(
'#prefix'=>'<h1>category:</h1>',
'#type' => 'checkboxes',
'#options' => $sub_tal,
);
Here $sub_tal is an array. If it is the single dimensional array it works fine. Here how to pass an array(The category , sub categories are came from db). How to maintain this structure with this code.
Please help me thanks.
Is there any particular reason why these all need to be in one form element? If your data is set up properly, you can simply loop through them and create elements in a somewhat organized fashion.
function my_test_form($form_state) {
// Assuming you have your data in a format like this
$categories = array(
'cat1' => array(
'#title' => 'category 1',
'sub1' => 'Subcat1',
'sub2' => 'Subcat2',
),
'cat2' => array(
'#title' => 'category 2',
'sub21' => 'Subcat21',
'sub22' => 'Subcat22',
),
'cat3' => 'category 3',
);
$elements = array();
foreach ($categories as $cat => $data) {
// If multidimensional, create parent and loop through children
if (is_array($data)) {
$elements[] = array(
'#type' => 'checkbox',
'#title' => $data['#title'],
'#return_value' => $cat,
);
foreach (element_children($data) as $subcat) {
$elements[] = array(
'#type' => 'checkbox',
'#title' => $data[$subcat],
'#attributes' => array(
'style' => 'margin-left: 2em',
),
'#return_value' => $cat . '-' . $subcat,
);
}
}
// Else, just create top level
else {
$elements[] = array(
'#type' => 'checkbox',
'#title' => $data,
'#return_value' => $cat,
);
}
}
// Group all of these elements together. They'll be submitted into the same array.
$form['categories'] = $elements;
$form['categories']['#tree'] = TRUE;
$form['categories']['#prefix'] = '<div style="font-weight:bold" class="label">' . t('Checkboxes title') . ':</div>';
$form['submit'] = array(
'#value' => t('Submit me'),
'#type' => 'submit',
);
return $form;
}
function my_test_form_submit($form, &$form_state) {
$submitted_values = array_filter($form_state['values']['categories']);
}
Renders as:
And when the form is submitted, assuming some checkboxes were checked, $submitted_values looks like this:
array('cat2', 'cat2-sub21', 'cat2-sub22')
You can change the values of the checkboxes submitted to the submit handler by changing the #return_value attribute that I set on each checkbox above.
The submitted values are grouped together not only because they are all within the $form['categories'] section, but also because #tree is set to TRUE.

ZF2: how to get options for dropdown from db

I followed the tutorial. But I can find no way to populate a form select from a database like this:
// Blog/src/Blog/Form/BlogItemForm.php
$blogCategoryTable = new Model\BlogCategoryTable;
$this->add(new Element\Select('category_id',
array('label' => 'Category', 'value_options' => $blogCategoryTable->getFormChoices())
));
Does anyone have any ideas?
I use a function to retrieve the data and set it to the form:
From my factory:
$option_for_select = $this->model->getWhatEver();
$this->add($factory->createElement(array(
'name' => 'what_ever',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'options' => $option_for_select,
),
'options' => array(
'label' => 'What ever:',
),
)));
From the model:
public function getWhatEver()
{
$sql = "SELECT something";
$statement = $this->adapter->query($sql);
$res = $statement->execute();
// set the first option
$rows[0] = array (
'value' => '0',
'label' => 'Top',
'selected' => TRUE,
'disabled' => FALSE
);
foreach ($res as $row) {
$rows[$row['triplet_id']] = array (
'value' => $row['col1'],
'label' => $row['col2'],
);
}
return $rows;
}

Categories