The Capability to choose post template in wordpress - php

I have created a role in wordpress with add_role( string $role, string $display_name, array $capabilities = array() )
function I want to know witch Capability from the array of $capabilities will give it access to choose post template?
This is my code:
$capabilities = array(
"activate_plugins" => false,
"create_users" => false,
"delete_themes" => false,
"delete_users" => false,
"edit_files" => false,
"edit_plugins" => false,
"edit_theme_options" => false,
"edit_themes" => false,
"edit_users" => false,
"export" => false,
"import" => false,
"install_plugins" => false,
"install_themes" => false,
"list_users" => false,
"manage_options" => false,
"promote_users" => false,
"remove_users" => false,
"switch_themes" => false,
"update_core" => false,
"update_plugins" => false,
"update_themes" => false,
"edit_dashboard" => false,
"customize" => false,
"delete_site" => false,
"moderate_comments" => false,
"manage_categories" => false,
"manage_links" => false,
"edit_others_posts" => true,
"edit_pages" => false,
"edit_others_pages" => false,
"edit_published_pages" => false,
"publish_pages" => false,
"delete_pages" => false,
"delete_others_pages" => false,
"delete_published_pages" => false,
"delete_others_posts" => true,
"delete_private_posts" => true,
"edit_private_posts" => true,
"read_private_posts" => true,
"delete_private_pages" => false,
"edit_private_pages"=> false,
"read_private_pages" => false,
"unfiltered_html"=> false,
"edit_published_posts" => true,
"upload_files" => true,
"publish_posts" => true,
"delete_published_posts" => true,
"edit_posts" => true,
"delete_posts" => true,
"read" => true );
add_role( "parisa", "parisa", $capabilities );
I want to know witch one to set to true?

A user with the capability of Create/Edit/Delete posts may access to choose post template.For more, please visit Roles and Capabilities
Hope this will helps you.

Related

php-cs-fixer does not work on visual studio

I am trying to use "php-cs-fixer" plugin with visual studio code.
I read this topic : topic
I followed all the steps. When I try to format the code, I see a brief message in the bottom tool bar "php-cs-fixer finished". But the code is not formatted at all.
Here is my settings , perhaps you will see something ? Precision : I am on widows environment.
{
"editor.fontSize": 12,
"eslint.alwaysShowStatus": true,
"diffEditor.ignoreTrimWhitespace": false,
"editor.wordWrapColumn": 120,
"files.autoSave": "onWindowChange",
"editor.mouseWheelZoom": true,
"[php]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "junstyle.php-cs-fixer"
},
"php-cs-fixer.lastDownload": 1632503694554,
"php-cs-fixer.exclude": [],
"php-cs-fixer.onsave": true,
"php-cs-fixer.rules": "#PSR2",
"php-cs-fixer.allowRisky": false, //it is safe not to allow risky linting
"php-cs-fixer.pathMode": "override",
"php-cs-fixer.autoFixByBracket": true,
"php-cs-fixer.autoFixBySemicolon": false,
"php-cs-fixer.formatHtml": true,
"php-cs-fixer.documentFormattingProvider": true,
"terminal.integrated.fontSize": 10,
"blade.format.enable": true,
"[blade]": {
"editor.defaultFormatter": "onecentlin.laravel-blade"
},
"editor.rulers": [80],
"php-cs-fixer.executablePath": "C:\\Users\\Domi\\AppData\\Roaming\\Composer\\vendor\\bin\\php-cs-fixer.bat",
"php.suggest.basic": false,
"editor.formatOnSave": true,
"workbench.colorTheme": "Solarized Light",
"php-cs-fixer.config": "C:\\Users\\Domi\\AppData\\Roaming\\Composer\\vendor\\bin\\config.php_cs",
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Found the solution here : https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5696
The config.php_cs was not correct. Here mine which works fine :
<?php
return (new PhpCsFixer\Config())
->setRules([
'#PSR2' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one',]],
'multiline_whitespace_before_semicolons' => false,
'single_quote' => true,
'binary_operator_spaces' => [
'operators' => [
// '=>' => 'align',
// '=' => 'align'
]
],
// 'blank_line_after_opening_tag' => true,
// 'blank_line_before_statement' => true,
'braces' => [
'allow_single_line_closure' => true,
],
// 'cast_spaces' => true,
// 'class_definition' => array('singleLine' => true),
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => true,
'function_typehint_space' => true,
'single_line_comment_style' => ['comment_types' => ['hash']],
'include' => true,
'lowercase_cast' => true,
// 'native_function_casing' => true,
// 'new_with_braces' => true,
// 'no_blank_lines_after_class_opening' => true,
// 'no_blank_lines_after_phpdoc' => true,
'no_blank_lines_before_namespace' => true,
// 'no_empty_comment' => true,
// 'no_empty_phpdoc' => true,
// 'no_empty_statement' => true,
'no_extra_blank_lines' => [
'tokens' => [
'curly_brace_block',
'extra',
// 'parenthesis_brace_block',
// 'square_brace_block',
'throw',
'use',
]
],
// 'no_leading_import_slash' => true,
// 'no_leading_namespace_whitespace' => true,
// 'no_mixed_echo_print' => array('use' => 'echo'),
'no_multiline_whitespace_around_double_arrow' => true,
// 'no_short_bool_cast' => true,
// 'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_around_offset' => true,
// 'no_trailing_comma_in_list_call' => true,
// 'no_trailing_comma_in_singleline_array' => true,
// 'no_unneeded_control_parentheses' => true,
// 'no_unused_imports' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
// 'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
// 'php_unit_fqcn_annotation' => true,
// 'phpdoc_align' => true,
// 'phpdoc_annotation_without_dot' => true,
// 'phpdoc_indent' => true,
// 'phpdoc_inline_tag' => true,
// 'phpdoc_no_access' => true,
// 'phpdoc_no_alias_tag' => true,
// 'phpdoc_no_empty_return' => true,
// 'phpdoc_no_package' => true,
// 'phpdoc_no_useless_inheritdoc' => true,
// 'phpdoc_return_self_reference' => true,
// 'phpdoc_scalar' => true,
// 'phpdoc_separation' => true,
// 'phpdoc_single_line_var_spacing' => true,
// 'phpdoc_summary' => true,
// 'phpdoc_to_comment' => true,
// 'phpdoc_trim' => true,
// 'phpdoc_types' => true,
// 'phpdoc_var_without_name' => true,
// 'increment_style' => true,
// 'return_type_declaration' => true,
// 'self_accessor' => true,
// 'short_scalar_cast' => true,
// 'single_blank_line_before_namespace' => true,
// 'single_class_element_per_statement' => true,
// 'space_after_semicolon' => true,
// 'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
// 'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,
'space_after_semicolon' => true,
// 'single_blank_line_at_eof' => false
])
// ->setIndent("\t")
->setLineEnding("\n")
;

Add custom User role for woocommerce to Only Edit ORDERS

I wrote this code to create a custom user called "payment manager", this user should only edit/ view woo-commerce orders, Im have looked everywhere and didn't found any answer, see code:
function sitelab_simple_role() {
add_role(
'payment-manager',
'Payment Manager',
array(
'read' => true,
'edit_posts' => true,
'upload_files' => false,
‘manage_woocommerce’ => false,
'manage_woocommerce_orders' => true,
'edit_shop_order' => true,
'edit_shop_order_terms' => true,
'edit_shop_orders' => true,
'manage_shop_order_terms' => true,
'publish_shop_orders' => true,
'read_private_shop_orders' => true,
'read_shop_order' => true,
),
);
}
add_action( 'init', 'sitelab_simple_role' );
For some-reason it doesn't allow to edit order with error message "you are not authorized for this action"
What could it be?
Thanks for the helpers
You are missing a few capabilities.
array(
'read' => true,
'edit_posts' => true,
'upload_files' => false,
'manage_woocommerce' => false,
'manage_woocommerce_orders' => true,
'edit_shop_order' => true,
'edit_shop_order_terms' => true,
'edit_shop_orders' => true,
'manage_shop_order_terms' => true,
'publish_shop_orders' => true,
'read_private_shop_orders' => true,
'read_shop_order' => true,
'assign_shop_order_terms' => true,
'delete_others_shop_orders' => true,
'delete_private_shop_orders' => true,
'delete_published_shop_orders' => true,
'delete_shop_order' => true,
'delete_shop_order_terms' => true,
'delete_shop_orders' => true,
'edit_others_shop_orders' => true,
'edit_private_shop_orders' => true,
'edit_published_shop_orders' => true,
),

Making SOAP call to a WSDL with PHP

https://www.ticimax.com/dokumanlar/webservis.pdf
Ticimax is an e-commerce software in Turkey. I'm trying to create products by using their webservice for one of my clients. I've used all of their webservice methods. But i'm stuck with their product creation method "SaveUrun". Because this method has only one thing different from other methods. This method accepts a parameter as ref
SaveUrun method is in the document from page 7 to page 9.
If you look at page 9, you will see this line at the end of the method definitions:
urunServis.SaveUrun("U15saQ48dW453X1cA", ref urunKartlari, ukAyar, varyasyonAyar);
urunKartlari is passed as ref.
What is ref? What is it stands for? What is the equivalent of it in PHP? How can i make my code work? Can you please help me?
This is what i get when i make the SOAP call:
Value cannot be null. Parameter name: source
Here is my example code:
<?php
$ticimax = new SoapClient("http://www.CLIENTDOMAIN.com/Servis/UrunServis.svc?wsdl");
$kategoriler = array();
$urunResimleri = array();
$varyasyonlar = array(
array(
"ID" => 0,
"Aktif" => false,
"AlisFiyati" => 10,
"Barkod" => "",
"Desi" => 1,
"KargoUcreti" => 0,
"KdvDahil" => true,
"KdvOrani" => 8,
"Ozellikler" => array(
array(
"Tanim" => "Numara",
"Deger" => 38
),
array(
"Tanim" => "Renk",
"Deger" => "Mavi"
)
),
"ParaBirimiID" => 1,
"Resimler" => array(),
"SatisFiyati" => 100,
"StokAdedi" => 15,
"StokKodu" => ""
),
array(
"ID" => 0,
"Aktif" => false,
"AlisFiyati" => 10,
"Barkod" => "",
"Desi" => 1,
"KargoUcreti" => 0,
"KdvDahil" => true,
"KdvOrani" => 8,
"Ozellikler" => array(
array(
"Tanim" => "Numara",
"Deger" => 40
),
array(
"Tanim" => "Renk",
"Deger" => "Mavi"
)
),
"ParaBirimiID" => 1,
"Resimler" => array(),
"SatisFiyati" => 100,
"StokAdedi" => 15,
"StokKodu" => ""
)
);
$urunKarti = array(
"ID" => 0,
"Aktif" => false,
"UrunAdi" => "Test ürün adı",
"Aciklama" => "Test ürün açıklama",
"AnaKategori" => "İç Giyim",
"AnaKategoriID" => 1,
"Kategoriler" => $kategoriler,
"MarkaID" => 1,
"TedarikciID" => 1,
"Resimler" => $urunResimleri,
"SatisBirimi" => "Adet",
"UcretsizKargo" => false,
"OnYazi" => "Test ürün önyazı",
"PuanDeger" => 12,
"SeoAnahtarKelime" => "",
"SeoSayfaAciklama" => "",
"SeoSayfaBaslik" => "",
"Varyasyonlar" => $varyasyonlar,
"Vitrin" => false,
"YeniUrun" => false
);
$params = array(
array(
"UyeKodu" => WEBSERVICE_PASSWORD,
"UrunKartlari" => $urunKarti,
"ukAyar" => array(
"AciklamaGuncelle" => true,
"AktifGuncelle" => true,
"FBStoreGosterGuncelle" => false,
"FirsatUrunuGuncelle" => true,
"KategoriGuncelle" => false,
"MaksTaksitSayisiGuncelle" => false,
"MarkaGuncelle" => false,
"OnYaziGuncelle" => false,
"ParaPuanGuncelle" => true,
"SatisBirimiGuncelle" => false,
"SeoAnahtarKelimeGuncelle" => false,
"SeoSayfaAciklamaGuncelle" => false,
"SeoSayfaBaslikGuncelle" => false,
"TedarikciGuncelle" => false,
"UcretsizKargoGuncelle" => true,
"UrunAdiGuncelle" => true,
"UrunResimGuncelle" => false,
"VitrinGuncelle" => false,
"YeniUrunGuncelle" => true
),
"vAyar" => array(
"AktifGuncelle" => false,
"AlisFiyatiGuncelle" => true,
"BarkodGuncelle" => false,
"IndirimliFiyatiGuncelle" => true,
"KargoUcretiGuncelle" => false,
"KargoAgirligiGuncelle" => true,
"ParaBirimiGuncelle" => false,
"PiyasaFiyatiGuncelle" => true,
"SatisFiyatiGuncelle" => false,
"StokAdediGuncelle" => true,
"UyeTipiFiyat1Guncelle" => false,
"UyeTipiFiyat2Guncelle" => true,
"UyeTipiFiyat3Guncelle" => false,
"UyeTipiFiyat4Guncelle" => true,
"UyeTipiFiyat5Guncelle" => false,
"TedarikciKodunaGoreGuncelle" => false
)
)
);
try{
print_r($ticimax->__soapCall("SaveUrun", $params));
}
catch(Exception $e){
echo $e->getMessage();
}
?>
Can you please fix my code?
UyeKodu UrunKartlari fields in Params array must be uyeKodu urunKartlari

How do I echo the slug of specific array in loop

I have an array here but I want to echo the single_name of the 8th key in the array, I was just wondering how do I do that please?
$stm_listings_update_options = array (
1 => array ( 'single_name' => 'Condition', 'plural_name' => 'Conditions', 'slug' => 'condition', 'font' => '', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => false, 'use_on_car_filter' => true, 'use_on_car_modern_filter' => true, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => true, ),
2 => array ( 'single_name' => 'Body', 'plural_name' => 'Bodies', 'slug' => 'body', 'font' => 'stm-service-icon-body_type', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => true, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => true, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'listing_rows_numbers' => 'two_cols', 'enable_checkbox_button' => false, ),
3 => array ( 'single_name' => 'Make', 'plural_name' => 'Makes', 'slug' => 'make', 'font' => '', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => false, 'use_on_car_filter' => true, 'use_on_car_modern_filter' => true, 'use_on_car_modern_filter_view_images' => true, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => true, 'enable_checkbox_button' => false, 'use_in_footer_search' => true, ),
5 => array ( 'single_name' => 'Model', 'plural_name' => 'Models', 'slug' => 'serie', 'font' => '', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => false, 'use_on_car_filter' => true, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => true, 'listing_taxonomy_parent' => 'make', 'enable_checkbox_button' => false, 'use_in_footer_search' => true, ),
6 => array ( 'single_name' => 'Mileage', 'plural_name' => 'Mileages', 'slug' => 'mileage', 'font' => 'stm-icon-road', 'numeric' => true, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => true, 'use_on_car_archive_listing_page' => true, 'use_on_single_car_page' => true, 'use_on_car_filter' => true, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'number_field_affix' => 'mi', 'enable_checkbox_button' => false, ),
7 => array ( 'single_name' => 'Fuel type', 'plural_name' => 'Fuel types', 'slug' => 'fuel', 'font' => 'stm-icon-fuel', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => true, 'use_on_single_car_page' => true, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, ),
8 => array ( 'single_name' => 'Engine', 'plural_name' => 'Engines', 'slug' => 'engine', 'font' => 'stm-icon-engine_fill', 'numeric' => true, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => true, 'use_on_single_car_page' => true, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'enable_checkbox_button' => false, 'use_in_footer_search' => false, ),
9 => array ( 'single_name' => 'Year', 'plural_name' => 'Years', 'slug' => 'ca-year', 'font' => 'stm-icon-road', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => false, 'use_on_car_filter' => true, 'use_on_car_modern_filter' => true, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'enable_checkbox_button' => false, ),
10 => array ( 'single_name' => 'Price', 'plural_name' => 'Prices', 'slug' => 'price', 'font' => 'stm-icon-road', 'numeric' => true, 'use_on_single_listing_page' => true, 'use_on_car_listing_page' => true, 'use_on_car_archive_listing_page' => true, 'use_on_single_car_page' => false, 'use_on_car_filter' => true, 'use_on_car_modern_filter' => true, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'enable_checkbox_button' => false, ),
11 => array ( 'single_name' => 'Fuel consumption', 'plural_name' => 'Fuel consumptions', 'slug' => 'fuel-consumption', 'font' => 'stm-icon-fuel', 'numeric' => true, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => true, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => false, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, ),
12 => array ( 'single_name' => 'Transmission', 'plural_name' => 'Transmission', 'slug' => 'transmission', 'font' => 'stm-icon-transmission_fill', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => true, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => true, 'use_on_car_filter' => true, 'use_on_car_modern_filter' => true, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, ),
13 => array ( 'single_name' => 'Drive', 'plural_name' => 'Drives', 'slug' => 'drive', 'font' => 'stm-icon-drive_2', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => true, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => true, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, ),
14 => array ( 'single_name' => 'Fuel economy', 'plural_name' => 'Fuel economy', 'slug' => 'fuel-economy', 'font' => '', 'numeric' => true, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => false, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'enable_checkbox_button' => false, ),
15 => array ( 'single_name' => 'Exterior Color', 'plural_name' => 'Exterior Colors', 'slug' => 'exterior-color', 'font' => 'stm-service-icon-color_type', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => true, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'enable_checkbox_button' => false, ),
16 => array ( 'single_name' => 'Interior Color', 'plural_name' => 'Interior Colors', 'slug' => 'interior-color', 'font' => 'stm-service-icon-color_type', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => true, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => false, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'enable_checkbox_button' => false, ),
17 => array ( 'single_name' => 'Features', 'plural_name' => 'Features', 'slug' => 'features', 'font' => '', 'numeric' => false, 'use_on_single_listing_page' => false, 'use_on_car_listing_page' => false, 'use_on_car_archive_listing_page' => false, 'use_on_single_car_page' => false, 'use_on_car_filter' => false, 'use_on_car_modern_filter' => false, 'use_on_car_modern_filter_view_images' => true, 'use_on_car_filter_links' => false, 'use_on_directory_filter_title' => false, 'listing_rows_numbers' => 'one_col', 'enable_checkbox_button' => true, ), );
Quick basic howto:
echo $stm_listings_update_options[7]['single_name'];
Better:
$i = 7; // Using a variable
$k = 'single_name';
echo $stm_listings_update_options[$i][$k];
I realize that OP may not have a zero as the first index. If the array starts with '1' as the first index, then $i should be 8.
Directly echoing the array value
echo $stm_listings_update_options[8]['single_name'];
or in loop gettingb the 8th elem
foreach( $stm_listings_update_options as $key =$value){
if ($key = 8 ){
echo $value['single_name'];
}
}
$i = 7; // The array to pick data from
foreach( $stm_listings_update_options as $data){
//if array number is same as $i then echo Slug
if ($data = $i ) {
echo $data["slug"];
}
}

ZF2 Add custom attribute to option in a select form element

I would like to add a custom HTML attribute to an option of a select in a Zend Framework 2 Form.
This is my (partial) code from my Form class:
$this->add(array(
'name' => 'lieuRemplissage',
'type' => 'Select',
'attributes' => array(
'class' => 'form-control',
),
'options' => array(
'label' => _('Lieu pré-enregistré'),
),
));
I populate my options values in my controller like this :
$form = new \Vente\Form\Vente;
foreach($this->getAdminLieuDeVenteTable()->fetchAll() as $lieu) {
$optionsLieu[$lieu->getId()] = $lieu->getNom();
}
$form->get('lieuRemplissage')->setValueOptions($optionsLieu);
But now, for each option I want to add an html attribute to all select options but with a different value for each one.
Is there a way do achieve that in ZF2 ?
Thanks.
Yes this is possible with ZF2
You pass in the attributes within the option value. The value should be in array format:
//example in view:
$select=new \Zend\Form\Element\Select('test');
$select->setValueOptions(
[
['attributes'=>['data-key'=>'value'],'value'=>'myValue','label'=>'myLabel']
]
);
echo $this->formselect($select);
prints:
<select name="test"><option value="myValue" data-key="value">myLabel</option></select>
EDIT:
The attributes you provide must be valid HTML attributes you cannot put any random key/value pairs.
For example data-* is fine as are the following :
protected $validGlobalAttributes = array(
'accesskey' => true,
'class' => true,
'contenteditable' => true,
'contextmenu' => true,
'dir' => true,
'draggable' => true,
'dropzone' => true,
'hidden' => true,
'id' => true,
'lang' => true,
'onabort' => true,
'onblur' => true,
'oncanplay' => true,
'oncanplaythrough' => true,
'onchange' => true,
'onclick' => true,
'oncontextmenu' => true,
'ondblclick' => true,
'ondrag' => true,
'ondragend' => true,
'ondragenter' => true,
'ondragleave' => true,
'ondragover' => true,
'ondragstart' => true,
'ondrop' => true,
'ondurationchange' => true,
'onemptied' => true,
'onended' => true,
'onerror' => true,
'onfocus' => true,
'oninput' => true,
'oninvalid' => true,
'onkeydown' => true,
'onkeypress' => true,
'onkeyup' => true,
'onload' => true,
'onloadeddata' => true,
'onloadedmetadata' => true,
'onloadstart' => true,
'onmousedown' => true,
'onmousemove' => true,
'onmouseout' => true,
'onmouseover' => true,
'onmouseup' => true,
'onmousewheel' => true,
'onpause' => true,
'onplay' => true,
'onplaying' => true,
'onprogress' => true,
'onratechange' => true,
'onreadystatechange' => true,
'onreset' => true,
'onscroll' => true,
'onseeked' => true,
'onseeking' => true,
'onselect' => true,
'onshow' => true,
'onstalled' => true,
'onsubmit' => true,
'onsuspend' => true,
'ontimeupdate' => true,
'onvolumechange' => true,
'onwaiting' => true,
'role' => true,
'aria-labelled-by' => true,
'aria-described-by' => true,
'spellcheck' => true,
'style' => true,
'tabindex' => true,
'title' => true,
'xml:base' => true,
'xml:lang' => true,
'xml:space' => true,
);
I just figured this out and wanted to share here since I saw this question while I was searching for the same question. Should give the same result with the suggested way but directly using options' attributes in form class; especially useful if passing data object to form construct to populate options like me.
$this->add(array(
'name' => 'lieuRemplissage',
'type' => 'Select',
'attributes' => array(
'class' => 'form-control',
),
'options' => array(
'label' => _('Lieu pré-enregistré'),
'value' => 123
'attributes' => array(
'data-key' => 'value_for_data_attribute_goes_here',
),
),
));

Categories