Cake PHP parent/child/grandchildren menus - cakephp

i want to display menus with their children and grand children
output will be like this
Parent
-child
-grandchildren
Parent 1
-child
heres my function in my MenuItemsController
public function buildTree($arr, $parent_id = 0) {
$op=array();
foreach ($arr as $item){
if ($item['parent_id']== $parent_id){
$op[$item['id']]=array('name'=>$item['name'],'parent'=>$item['parent_id']
);
$children=buildtree($arr,$item['id']);
if (children) {
$op[$item['id']]['parent_id'] =$children;
}
}
}
return $op;
}
Heres on my View Element app\view\elements\navigation.ctp
<?php
App::import('Controller', 'MenuItems');
$menu_items = new MenuItemsController();
?>
<ul id="navigation">
<?php
foreach($mainMenuItems as $item) {
echo "<li>".$item['MenuItem']['name']."</li>";
$child=$menu_items->buildsubmenus($item['MenuItem']['id']);
pr($child);
}
?>
</ul>

Your controller needs to generate the tree and pass it to the view. How to generate it? it depends on your application, maybe you need to call the data base, maybe it's just a static array, etc.
You'll need to create your own format for the tree, for example:
Array
(
[0] => Array
(
[0] => child 11
[1] => Array
(
[0] => grandchild 121
[1] => grandchild 122
)
)
[1] => parent 2
[2] => Array
(
[0] => child 31
)
)
then you can pass it to the view and loop the array to create the menu. To do that , you could use a recursive function to display the array.. or if you know that there wont be more than 3 levels you could simply use 3 for loops
=) so this has nothing to do with cakephp.. however, if your menu is stored on the database, maybe you could use the tree behaviour to retrieve the menu from the DB, however you'll still need to pass it to the view and create a function to display it
Hope this helps

Related

Get access to an array in an array from REST API

Hi I'm trying to display variants in two separate categories Color and Size, to do this I need to get data from the api, I can access "attributes", but I would like to be able to access 0 and 1 and map them, I have no idea how to do this.
{variants.length > 1 ? (
variants.attributes.map(({ name, values }) => (
<ProductOptions
key={`key-${name}`}
name={name}
values={values}
selectedOptions={selectedOptions}
setOptions={setOptions}
/>
))
) : (
<Fragment />
)}
Thank you so much!
As i understand the output of Array with 6 elements where each of that 6 has attributes and attributes is yet another array and you want to loop through those attributes so you need 2 loops. One to loop through parent array and seconds inside the child.
variants.map((variant) => {
variant.attributes.map((attribute) => {
console.log('attribute: ', attribute);
console.log('attribute id: ',attribute.id);
});
});
p.s. you may use forEach https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach but it has little difference in this case.
Also it seems like you working with ReactJS and building some JSX to put into rendered jsx. I would argue to form the array upfront and in final jsx just insert the rows, so your final jsx will be more readable, especially in cases of double loops or any more complex jsx.
const attributes = [];
variants.map((variant) => {
variant.attributes.map((attribute) => {
console.log('attribute: ', attribute);
console.log('attribute id: ',attribute.id);
attributes.push(<div key={attribute.id}>{attribute.id}</div>)
});
});
// later
return (
<div>
{attributes}
</div>
)

How to convert an json data in array using accessor method in Laravel?

I m new in Laravel 8. Please give any suggestion for the given below code or answer my question:
Given below is my code an example of what i really want to get:
my data is :
[{"column_1_1":"value_1_1","column_1_2":"value_1_2"},{"column_2_1":"value_2_1","column_2_2":"value_2_2"}]
and I want
Array
(
[0] => stdClass Object
(
[column_1_1] => value_1_1
[column_1_2] => value_1_2
)
[1] => stdClass Object
(
[column_2_1] => value_2_1
[column_2_2] => value_2_2
)
)
I am adding this answer as the other is showing only one way and is the least used... Mine is the Laravel way, the one you should always use and the most you are going to see all the time...
If you have a Model who's property is, for example, data, you don't have to overwrite how it is read by doing getDataAttribute and use json_decode...
If that property/field is a JSON/TEXT in your database (hence storing JSON), you just have to cast it.
class YourModel extends Model
{
protected $casts = [
'data' => 'array'
];
}
So later you can do:
foreach ($model->data as $item) {
...
}
And store info in it like:
$array = ['products' => ['item1', 'item2'], 'quantity' => 2];
$model->data = $array;
And it will get saved on the database like {products: ["item1", "item2"], quantity: 2}
Bro you just need to add the accessor method in your model like:
Guess you have xyz column in your database
public function getXyzAttribute($xyz)
{
return json_decode($xyz);
}
It will directly return the array.
Hope it worked. :)

Wordpress / PODS - echo array items from a relational field as single items

I'm a real n00b when it comes to coding in PHP, so I hope you can clear something up for me :)
I've created a portfolio with PODS, which creates custom post-types and taxonomies.
What I'd like to achieve is this:
Add label(s) to every post, showing the categories the post belongs to, as an image.
In my opinion it's the best way for efficiency and liability to make use of the already available data, instead of adding a new field which contains an image / value -> CSS. This would cause unnecessary requests.
So what I did is add a relational field (related to custom taxonomies) as multiple-select.
Eventually this should end up with something like:
<span class="value1"></span>
<span class="value3"></span>
<span class="value6"></span>
In that case I'm able to assign an image to it with CSS (as content or background)
I tried in a lot of ways to get the content of the field outputted as separate values, but my knowledge is the bottleneck in this, so I hope someone is willing to point me in the right direction.
This is the code I have at the moment:
<a href="<?php the_permalink(); ?>">
<h1 class="hentry__title"><?php the_title(); ?>
<?php $item_label = get_post_meta( get_the_ID(), 'item_label', false ); ?>
<?php if (!empty($item_label)):
foreach ( $item_label as $cat_single ) { ?>
<span class="item-label"><?php echo ($cat_single); ?></span>
<?php }
endif; ?>
</h1>
</a>
This gives me 2 times the word "array" which looks promising because of the count being correct I guess. But when I take a look at the actual content of the array,
<?php print_r ($item_label); ?>
I get this result:
Array ( [0] => Array ( [term_id] => 93 [name] => Banners [slug] => banners [term_group] => 0 [term_taxonomy_id] => [taxonomy] => portfolio_categorie [description] => [parent] => 0 [count] => 0 [object_id] => [term_order] => [pod_item_id] => 93 ) [1] => Array ( [term_id] => 90 [name] => Websites [slug] => websites [term_group] => 0 [term_taxonomy_id] => 90 [taxonomy] => portfolio_categorie [description] => [parent] => 0 [count] => 2 [object_id] => 2414 [term_order] => 0 [pod_item_id] => 90 ) )
I don't know how to handle this data. I'm sure someone here has an answer for me, so I hope that one would help me out here!
Kindest regards!
At a quick glance it looks like you are close... just need to access the values in the item_label array. Change your echo (inside your for loop) statement to:
<?php echo $cat_single['name']; ?>

Display Drupal taxonomy tree

I sucessfully print the whole taxonomy tree through this code
$voc = taxonomy_vocabulary_machine_name_load('product_sub_categories');
$tree = taxonomy_get_tree($voc->vid,0,NULL,TRUE);
foreach ($tree as $key => $term){
print $term->name."</br>";
}
, now I want to separatly print parent and chile in order to custon theme them, because I have already a markup in which parent has separate
<p class="parent_class">Parent terms</p>
and children has separate
<ul class="child_class">Child terms</ul>.
How should I do this, can anyone there to help me?
Here is the solution
`$tree=taxonomy_get_children(1);
foreach ($tree as $term)
{
$item[] = $term->name;
$child_term = taxonomy_get_children($term->tid);
if (!empty($child_term))
{
print $term->name."<br>";
foreach ($child_term as $child)
{
print $child->name."<br>";
}
}
else
print $term->name."<br>";
}`
You can use the Views Tree Module.
Here is the documentation for this module
https://www.drupal.org/node/1493366

Drupal 7 How to use render element instead of variables in hook_theme()

I'm having trouble understanding the 'render element' key purpose when implementing hook_theme().
function personal_news_theme($existing, $type, $theme, $path) {
return array(
'teaser_list_by_user' => array(
'render element' => 'element',
),
);
}
I did my research and the way I understand it, the render element key is use when creating a theme function to modify another theme function's output ONLY! And there's no way to use it as to implement a new theme function.
If I'm wrong, how can I use it instead of variables?
In the example below, I created a new theme function using a render element.
/**
* Implements hook_theme().
*/
function rojo_theme() {
$items = array(
'rojo_content' => array(
'render element' => 'element',
),
);
return $items;
}
/**
* Theme function.
*/
function theme_rojo_content($vars) {
return '<pre>' . print_r($vars, TRUE) . '</pre>';
}
/**
* Render function.
*/
function rojo_render() {
$build = array(
'#theme' => 'rojo_content',
'#module' => 'rojo',
'content' => 'Page content for the render function',
'list' => array('one', 'two'),
'tag' => 'div',
);
return render($build);
}
This will print the output of the $vars passed into the theme function. From here, you should be able to see what is going on. The #theme property will be called with theme() and passed the $build array during the render() process. Notice I added #module property and Drupal added the #printed / #children properties.
This is purely an example to demonstrate the creation of a new theme function using render element and argument passing. I hope this helps somebody out.
Array
(
[element] => Array
(
[#theme] => rojo_content
[#module] => rojo
[content] => Page content for the render function
[list] => Array
(
[0] => one
[1] => two
)
[tag] => div
[#printed] =>
[#children] =>
)
)
If your theme function returns an array - you can specify which key of the array to use for rendering.
render element: The name of the renderable element or element tree to pass to the theme function. This name is used as the name of the variable that holds the renderable element or tree in preprocess and process functions.
(Source: hook_theme)

Resources