Drupal - PHP Validation - php

I have some content types (nodes) that are attached to various taxonomies. For specific node types, I want to do some validation on the taxonomy. I do not want to hard-code the nodes types and their corresponding fields that reference the taxonomy. So I put them in array.
However, I am unable to dereference the field names. I've tried double $$, quotes, etc, but can't get it to work. Is what I want to do possible?
Below is a standalone PHP that I am trying to get to work.
<?php
$node = (object) array(
'nid' => NULL,
'vid' => NULL,
'uid' => '1',
'type' => 'price_document',
'language' => 'und',
'field_taxonomy_price' => array(
'und' => array(
array(
'tid' => '94'
)
)
),
);
$nodes_to_check = array("price_document" => "field_taxonomy_price",
"package" => "field_taxonomy_package",
);
if (array_key_exists($node->type,$nodes_to_check)) {
$taxonomy_field = $nodes_to_check[$node->type];
print_r($taxonomy_field);
$tid = $node->field_taxonomy_price ['und'][0]['tid']; // <- this works but, how
//$tid = $node->"$$taxonomy_field" ['und'][0]['tid']; <- can I deref variable?
}
?>

Well, you can do this:
$taxonomy_field = $nodes_to_check[$node->type];
$tid = $node->{$taxonomy_field}['und'][0]['tid];
You don't need the double dollar signs. That's in case you want to do things like this:
$dog = "I am a dog";
$var = "dog";
$$var = "Now I'm a pussycat";
echo $dog; // Output: Now I'm a pussycat

Related

How to use if for this array

I want to use if for this array :
$options[] = array( "name" => __('slider Settings','wordpresstools'),
"desc" => __('','wordpresstools'),
"id" => $shortname."_favSlider",
"std" => "",
"type" => "select",
"options" => array(
'option1' => 'test',
'option2' => 'test 2',
'option3' => 'test 3'
));
if this select option = option1
echo " Test ";
I have more $options , but I want to if just for that option ( Slider Settings )
Thanks .
Maybe the problem in your code are those brackets after $options. That way you append a new array element to the collection which means you have to test for something like this:
// Assuming your code creates the first array element
if ($options[0]['std'] == 'option1') {
// Do your stuff
}
If you don't need the square brackets in your first line of code it would look like this:
$options = array(/* Your values */);
if ($options['std'] == 'option1') {
// Do your stuff
}
$shortname='test';
$options = array( "name" => create_function('slider Settings','wordpresstools'),
"desc" => create_function('','wordpresstools'),
"id" => $shortname."_favSlider",
"std" => " I am here",
"type" => "select",
"options" => array(
'option1' => 'test',
'option2' => 'test 2',
'option3' => 'test 3'
));
echo "<pre>";
print_r($options['std']);
If i am right you try to create a constructor function there. In order to do something like that you must use create function. Read more details here The output of this code is printing std field as you want. The downside is that this method will be deprecated in PHP 7.2 so maybe you can find another work around than creating functions inside an array.
Foreach loop on options
foreach($options['options'] as $k=>$v){
// Check value of key
if($k == 'option1'){
echo $v . "\n";
}
}
I'm not exactly sure what kind of implementation you need but this should be a decent start for however you're trying to use it. I'm not sure if you mean to always have option1, option2, option3, etc. set or if you're checking to see if they exist. If the prior, then you'd need to make a few slight changes; if the latter, then this should work fine as it will check every key value and it should only exist if the option is selected.

Creating array from translated datas and merging an array in PHP

I'm designing a site with multi-language support. Our main language is Turkish. I do not want while admin is inserting Turkish data, empty other fields in foreign languages. So when the Turkish data insert in ends with "_tr" columns, in the foreign language fields ends with "_en" and "_de", I want to get from Yandex Translator data which is automatically translated.
Here is my table structure:
My data structure like this which will be inserted:
$data = array(... 'parent_id' => 234, 'date' => "2014-08-31 23:07:47", 'status' => 1);
I want to to add in "..." like this:
$translated = array('fruit_tr' => "Elma", 'fruit_en' => "Apple", 'fruit_de' => "Apfel", 'color_tr' => "Kırmızı", 'color_en' => "Red", 'color_de' => "Rot");
I tried this:
$from_turkish = array('fruit' => "Elma", 'color' => "Kırmızı");
public function Translate ($from_turkish) {
$langs = array("tr", "en", "de");
$translated = array();
foreach ($langs as $lang){
foreach ($from_turkish as $field_name => $value) {
$translated[] = array($field_name.'_'.$lang => YandexTrApi($value, 'tr', $lang));
}
}
return $translated;
}
YandexTrApi function returns translated data. Finally, I used array_merge function like this:
$data_array = array_merge($translated, $data);
But it did not take form like this:
$data_array = array('fruit_tr' => "Elma", 'fruit_en' => "Apple", 'fruit_de' => "Apfel", 'color_tr' => "Kırmızı", 'color_en' => "Red", 'color_de' => "Rot", 'parent_id' => 234, 'date' => "2014-08-31 23:07:47", 'status' => 1);
array_merge($translated, $data) is fine and $translated + $data would give the same result in this case, but...
$translated[] = array($field_name.'_'.$lang => YandexTrApi($value, 'tr', $lang));
...this way you'll push new arrays inside $translated array. You need to add new keys only - try this instead:
$translated[$field_name.'_'.$lang] = YandexTrApi($value, 'tr', $lang);
Also the foreach() loops nested like this will give you slightly different order, but I guess you don't need it to match db (as your ... part may be placed before parent_id key).

How to define an array element using a PHP variable

community.
I've been looking for an answer, but not close enough to this scenario
I have a code like this (actually working fine):
array('desc'=>'Home')
It is working defining it as text (Home) but I would rather to use it as a PHP variable due multilanguage site. Using the language package I have a variable such:
$lang['HOME'] = 'Home';
So depending on the language selected, the value changes
In other words, I need the array to have the variable value as the element
array('desc'=>'variablehere')
Can anyone plese let me know what am I missing? I have tried to set it as variable, as echo and so other ways.
Thanks a lot!
Like this?
$myArray = array('desc' => $variable);
or
$myArray = array(
'desc' => $desc,
'name' => $name
);
In your case:
$lang = array('HOME' => $homeVariable);
Use a multi-dimensional array. e.g., given something like
$lang = 'en';
$key = 'desc';
The exact array structure depends on your needs, but it could either be primarily by language, or by key-to-translate:
language-based:
$translations = array(
'en' => array('desc' => 'foo'),
'fr' => array('desc' => 'bar')
);
$text_to_display = $translations['en']['desc']; // produces 'foo'
or
$translations = array(
'desc' => array(
'en' => array('desc' => 'foo'),
'fr' => array('desc' => 'bar')
)
)
$text_to_display = $translations['desc']['fr']; // produces 'bar'
Use a translate function instead:
// It can be key-based, i.e., t('home_string'), or literal like below
t('Home');
function t($text, $language = NULL)
{
if (!$language) {
// determine visitor's language
}
// look up translation in db/file
// if none found just return $text as is
}

How do I make this little PHP Array work?

I want to find out how I can store small strings in an array and output them correct.
In this case I want to set a two-letter language code in an array at the top and
then output a string in that language later.
I really appreciate your help.
The following code I've made does not work but it's something like this I'm looking for:
<?php
// Set the language
$settings = array(
Language => "en"
);
// Set the strings
$locales = array(
Installed => array("en", "da"),
TheString => array("Dog", "Hund")
);
// Do some magic
$lang = $settings["Language"][0];
// Output Dog (or Hund if the language is "da")
echo $lang["TheString"];
?>
$settings = array( 'lanaguage' => 'en');
$locales = array(
'en' => array(
'dog' => 'dog'
),
'da' => array(
'dog' => 'hund'
)
);
// You don't need this, but you can get it like so:
$installed_languages = array_keys( $locales);
echo $locales[ $settings['language'] ]['dog'];
This will either output dog if $settings['language'] is en, or hund if it is da.

Need help about array

What do
$categories[$id] = array('name' => $name, 'children' => array());
and
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
mean?
Thanks a lot.
How should i format the output so i can learn the results that was returned?
You can format your code into tables by looping on the array using for or foreach. Read the docs for each if you don't have a grasp on looping.
2.What does
$categories[$id] = array('name' => $name, 'children' => array());
and
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
The first line assigns an associative array to another element of the $categories array. For instance if you wanted the name of the category with ID of 6 it would look like this:
$categories[6]['name']
The second line does something similar, except when you are working with an array in PHP, you can use the [] operator to automatically add another element to the array with the next available index.
What is the uses of .= ?
This is the concatenation assignment operator. The following two statements are equal:
$string1 .= $string2
$string1 = $string1 . $string2
These all have to do with nesting arrays.
first example:
$categories[$id] = array('name' => $name, 'children' => array());
$categories is an array, and you are setting the key id to contain another array, which contains name and another array. you could accomplish something similar with this:
$categories = array(
$id => array(
'name' => $name,
'children' => array()
)
)
The second one is setting the children array from the first example. when you have arrays inside of arrays, you can use multiple indexes. It is then setting an ID and Name in that array. here is a another way to look at example #2:
$categories = array(
$parentID => array(
'children' => array(
'id' = $id,
'name' => $name
)
)
)
note: my two ways of rewriting are functionally identical to what you posted, I'm just hoping this makes it easier to visualize what's going on.

Categories