I have to know what are the codes(ISO codes) of the languages in
Intl::getLanguageBundle()->getLanguageNames($this->name);
Anyone know how to do that?
Update
I discover that the codes are in
"alpha-2 code" and exist a list in the site http://www.nationsonline.org/oneworld/language_code.htm
but I what to see the list of the Intl::getLanguageBundle() to see if it has any different codes
Thanks in advance for any help
From the LanguageBundleInterface class, here's the relevant docblock:
/**
* Returns the names of all known languages.
*
* #param string $displayLocale Optional. The locale to return the names in.
* Defaults to {#link \Locale::getDefault()}.
*
* #return string[] A list of language names indexed by language codes.
*/
public function getLanguageNames($displayLocale = null);
So you just need to use the result like this:
$languages = Intl::getLanguageBundle()->getLanguageNames($this->name);
foreach ($languages as $code => $name) {
/*...*/
}
Related
I got a lot of products, and it can be filter with a lot of different parameters.
So user input search parameter in a form, and then the list is filter by those parameters.
I have try to create a route like this :
/**
* Display a list of product
* #Route("/product/list/{name}/{price_min}/{price_max}/{publish_date}/{supplier_code}", name="product_list")
*/
public function listProduct(){
// ... Check parameters format and then escape special caracters
// ... Display product logic
return $this->render('Product/product_list.html.twig', $array_params_view);
}
I know you can provide optional parameter, but this solution looks really bad to me...
I think there might be another solution.
I have think to use the Request instead of a lot of parameter, but if I do so, I loose the fonctionality of nice and easily readable URL, and maybe it'll be more difficult to manage routing.
I don't know what is the best solution for search functionnality.
If you use route for search into your list, I think you need to read this : link
Query String is a better way for search.
// the query string is '?foo=bar'
$request->query->get('foo');
// returns 'bar'
/**
* Display a list of product
*
* #Route("/list/", name="product_list")
*
* #param Request $request
*
*/
public function listProduct(Request $request)
{
$name = $request->query->get('name');
$price_min = $request->query->get('price_min');
$price_max = $request->query->get('price_max');
$publish_date = $request->query->get('publish_date');
$supplier_code = $request->query->get('supplier_code');
$list_products = $this->getListProducts($name,$price_min,$price_max,$publish_date,$supplier_code);
//Next code
......
}
You would only have to control within the getListProducts function
or whatever you call it, that the arguments can arrive as null
I'm trying to find ou a way to make this in twig but I can't find a solution..
preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', ucfirst("thisIsAnExample")) ;
The output in php is "This Is An Example"
Is it possible to do the same thing in twig ?
You will need to use a twig extension which will create a custom function for you to use in twig.
For example:
/**
* Convert camel case to a capitalised human readable string.
*
* #param $camelCase string
* #return string
*/
public function camelCaseToString($camelCase)
{
return preg_replace("/([A-Z])/", " $1", ucfirst($camelCase));
}
This should convert thisIsAnExample to This Is An Example.
I am storing money in my database using an integer. This means that $0.50 is 50. I extended the Integer db field in such a way that this is now working correctly at the front end. It is nicely converted to and from integer.
In the backend, however I am having problems. The silverstripe CMS seems to do its own conversion (adding thousand separators for example), which has interesting results :).
How would you guys approach this problem? I tried to use an onbeforewrite and a custom getter.
This is the code I have, starting with an extension of the integer db-field
/**
* Format a number to currency
* #param int $number_of_decimals When larger than 0 it will return this number of decimals, AND divide the amount by 10^number of the amount of decimals
* #param bool $round Round the resulting number to the closest whole number
* #param string $thousands_char Character used as thousands separator
* #param string $decimal_char Character used as decimal separator
* #return string
*/
public function toCurrency($number_of_decimals=2, $round=false, $thousands_char=".", $decimal_char=",") {
$divide_by = pow(10,$number_of_decimals);
$value = $this->owner->value/$divide_by;
if($round) {
//no decimals when rounding :)
$number_of_decimals=0;
}
return number_format($value, $number_of_decimals, $decimal_char,$thousands_char);
}
public function fromCurrency($number_of_decimals=2, $thousands_char=".", $decimal_char=",") {
$multiply_by = pow(10,$number_of_decimals);
//get rid of the thousand separator
$value = str_replace($thousands_char,"",$this->owner->value);
//replace the decimal char with a point
$value = str_replace($decimal_char,".",$value);
$value = $value*$multiply_by;
return number_format($value, 0, ".","");
}
Also I added this to an extension of the SiteConfig (thus creating a sort of globally available function
/**
* Creates a DBField equivalent of the value, based on the type. In such a way, we can make use of the dame functions that are in an extension of a dbfield.
* #param $type The type of the DBfield to create (e.g. Varchar, Int etc.).
* #param $value The value, a string or number
* #return mixed
*/
public function ToDBField($type,$value) {
$field = $type::create();
$field->setValue($value);
return $field;
}
These functions do the actual work, and they are in a dataobject:
public function GetAmount() {
$amount = parent::getField("Amount");
if (is_subclass_of(Controller::curr(), "LeftAndMain")) {
$int_amount = SiteConfig::current_site_config()->ToDBField("Int", $amount);
return $int_amount->toCurrency($number_of_decimals=2, $round=false, $thousands_char="", $decimal_char=".");
}
return $amount;
}
public function onBeforeWrite() {
$int_amount = SiteConfig::current_site_config()->ToDBField("Int", $this->Amount);
$this->Amount = $int_amount->fromCurrency(2,",",".");
parent::onBeforeWrite();
}
So the problem you're facing is one of the default scaffolding based on the DbField type. By default, type Int will use a NumericField in the CMS which does no currency formatting.
We can override the scaffolding for a form field in the getCMSFields function of a DataObject. Specifically using FieldList::replaceField to replace the form field with one of our choosing.
function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->replaceField('Amount', CurrencyField::create('Amount', 'Amount'));
return $fields;
}
You have a number of options for this from building your own from scratch, building on-top of an existing form field or using a pre-existing one. CurrencyField and MoneyField both should do what you want without you needing to have your own functions exposed on the DataObject to do the conversion back and forth.
There are a few things to note:
From the documentation, CurrencyField is limited to US-centric formats
CurrencyField and MoneyField are not designed to be backed by an integer
At this point, I would consider altering your approach. MoneyField seems to describe most-closely to what you want with the ability to more easily handle different currencies. The drawback is that it requires a Money database field.
I've never used the MoneyField though I believe for a few projects I have used the CurrencyField. Because of that, I can't speak for how easy it is to work with and tweak to the way you want.
If you still would rather not take this approach, I would look at extending perhaps the CurrencyField class (by this I mean Object-Orientated extending a class, not the built-in extension system) and tweaking that to your liking. Implementing that however is likely too broad to cover in this question.
Is there a phpdoc-compliant way* of expressing the various returned values in a PHPUnit dataProvider?
For example:
return array(
array(false, 17, array(1,2)),
array(true, 19, array(1,2)),
);
I would like to write something more detailed than #return array, that expresses that the first value passed to the test method is a bool, the second is an integer, third is an array (and also what each of these values represent).
* If there's no compliant way, does anyone have tips on how to document in a clear manner?
The only further specification for arrays is what they contain (if always the same type):
/**
* #return array[]
*/
function() {
// ...
}
This annotation expresses the function will return an array of arrays, you can find a further discussion in this question:
PHPDoc type hinting for array of objects?
Note: Not every IDE will be able to interpret it
To a possible clear manner: In my opinion, you can always use code blocks in your comments to explain some of your code:
/**
* returns array with following structure:
* <code>
* array(
* array(
* (bool), (int), (array)
* )
* )
* </code>
*
* #return array[]
*/
Or similar explanation. I'm sure there are plenty ways to give other developers a hint about what to expect. In the end it just needs to be understandable to a human if it is not included in the usual annotations.
I've got a node, I want it's menu. As far as I can tell, node_load doesn't include it. Obviously, it's trivial to write a query to find it based on the path node/nid, but is there a Drupal Way to do it?
if the menu tree has multiple levels sql seems a better option.
a sample for drupal 7 is given bellow where path is something like 'node/x'
function _get_mlid($path, $menu_name) {
$mlid = db_select('menu_links' , 'ml')
->condition('ml.link_path' , $path)
->condition('ml.menu_name',$menu_name)
->fields('ml' , array('mlid'))
->execute()
->fetchField();
return $mlid;
}
The Menu Node module exposes an API to do this.
You can read the documentation (Doxygen) in the code. I think the functionality you need is provided by the menu_node_get_links($nid, $router = FALSE) method:
/**
* Get the relevant menu links for a node.
* #param $nid
* The node id.
* #param $router
* Boolean flag indicating whether to attach the menu router item to the $item object.
* If set to TRUE, the router will be set as $item->menu_router.
* #return
* An array of complete menu_link objects or an empy array on failure.
*/
An associative array of mlid => menu object is returned. You probably only need the first one so it might look like something like this:
$arr = menu_node_get_links(123);
list($mlid) = array_keys($arr);
Otherwise, you can try out the suggestion in a thread in the Drupal Forums:
Use node/[nid] as the $path argument for:
function _get_mlid($path) {
$mlid = null;
$tree = menu_tree_all_data('primary-links');
foreach($tree as $item) {
if ($item['link']['link_path'] == $path) {
$mlid = $item['link']['mlid'];
break;
}
}
return $mlid;
}