Display Drupal taxonomy tree - drupal-7

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

Related

Cypress.io Iterate list of elements to see if text does not exist, then perform action to add element

I'm trying to iterate over a list (<UL>) to see whether or not a specific string exists within the <li> list item elements. If the string (Lemon in this example) does not exist then I want to perform an action to add the element. Otherwise, if the string does exist then I just want to do nothing and continue on.
Sample html:
<ul id=theTree>
<li>Apple</li>
<li>Pear</li>
<li>Orange</li>
</ul>
I have tried:
let found = false
cy.get('#theTree').find('li').each( ($node, i, $list) => {
if ($node.textContent.includes("Lemon")) {
found = true
}
})
// At this point after each <li> has been iterated over, will 'found' be true or false?
if (!found) {
// Add "Lemon" to the list here if it was not found in the <ul> list...
} else {
// Do nothing. Lemon already exists in the list.
}
The if condition "$node.textContent" is returning undefined and not returning the text content from the list elements. Therefore, the .includes() function throws an error.
Also, I have feeling the asynchronous callback function from .each() needs a Promise or some other means of saving the "found = true" state, such that, the second if block can correctly determine whether or not "Lemon" was found in the list iteration.
Any help is very much appreciated.
Perhaps iterating with .each() is not the easiest way to do this.
Using .then() allows you to pass the found value down the chain. You could also add an alias if you want to use it later in the test.
Setting a variable external to the command chain is called a "backflip" and can cause problems depending on the test structure.
cy.get('#theTree').find('li')
.then($els => {
const texts = [...$els].map(el => el.innerText) // extract texts
const found = texts.includes('Lemon')
return found
})
.then(found => {
if (!found) {
...
})
With an alias
cy.get('#theTree').find('li')
.then($els => {
const texts = [...$els].map(el => el.innerText) // extract texts
const found = texts.includes('Lemon')
return found
})
.as('found')
// later
cy.get('#found').then(found => {
if (!found) {
...
})
You have to check the result after the each command will be executed.
For instance, you can use then command as follows:
let found = false
cy.get('#theTree').find('li').each( ($node, i, $list) => {
if ($node.textContent.includes("Lemon")) {
found = true
}
}).then(() => {
// this callback will be executed after 'each' command so we know the result at this point.
if (!found) {
// Add "Lemon" to the list here if it was not found in the <ul> list...
} else {
// Do nothing. Lemon already exists in the list.
}
})

How to resolve an undefined value when retrieving data from one controller to another controller's view in cakephp using elements

I am trying to retrieve data from another controller to display it in the PagesController's view using an element. I have a table
service_categories(id, service_category);
my ServiceCategoriesController looks like this
public function category() {
$serviceCategories = $this->paginate();
if ($this->request->is('requested')) {
return $servicesCategories;
} else {
$this->set('serviceCategories', $servicesCategories);
}
}
my category.ctp element looks like this
<?php
$serviceCategories = $this->set('serviceCategories/category');
foreach ($serviceCategories as $serviceCategory):
echo $serviceCategory['ServiceCategory']['service_category'];
endforeach;
But I seem to get an undefined value of "$serviceCategories" when I create an alert before the foreach loop. Please assist! What am I missing?
I rewrite the answer after turning on brain:
In your element replace:
$serviceCategories = $this->set('serviceCategories/category');
with
$serviceCategories = $this->requestAction('service_categories/category');

Cake PHP parent/child/grandchildren menus

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

CakePHP 2.0 Controller render not working all of the time

I have a search controller that will look up values and render specific views according to the type of report that should be displayed. There is a weird thing happening. When I issue the $this->render the report view is not rendered. The "catch all" redirect line always is rendered... Code as follows:
public function admin_printReport() {
if (isset($this->request->data['Reports'])) {
$nons = $this->request->data['Reports'];
$res = array();
// lets lookup the noncons.....
foreach ($nons as $dat=>$vdat) {
// skip the ones that are not checked
if ($vdat == 0) {
continue;
}
// this is the temporary array that holds all of the selected report numbers > $res[] = $dat;
}
$this->loadModel('Noncon');
$this->Noncon->recursion = 0;
$results = $this->Noncon->find('all', array('conditions'=>array('Noncon.id'=>$res)));
$this->set('results', $results);
// lets do the selection now...
if (isset($this->request->data['PS'])) {
// Print summary
$this->render('summary', 'print');
} elseif (isset($this->request->data['PD'])) {
// Print detail
$this->render('detail', 'print');
} elseif (isset($this->request->data['PDH'])) {
// Print detail with history
$this->render('detailhistory', 'print');
}
}
// catch all if the render does not work....
$this->redirect(array('controller'=>'noncons', 'action'=>'search','admin'=>true));
}
Any Ideas?
I just figured it out....
for each $this->render, add return. For example:
return $this->render('summary', 'print');
I've just add a similar problem.
In a controller, that has an ajax submit, it was not submitting the render.
$this->render($viewName, 'ajax');
Using return didn't helped.
The problem was that I added
$this->render('add');
at the end of the controller, although it was not necessary since the controller name is add and the autoRender is default (true to automatically render the view with the same name of the controller).
Hope this helps someone else.

Populate Webform hidden field with title of referring node

Drupal 7
I'm having a similar problem to one that's been presented previously but so far I've not been able to make any of the suggestions work.
I have 'Product' pages of content type 'Software Products'. I want to place a link on the product pages pointing to a Webform 'Request Information' I want to populate a (hidden) field on the form with the product name which is also the title of the referring product page.
I have tried the following but this just results in the title of the form being shown - not the referring page.
<?php
/**
* Implementation of hook_form_alter().
*/
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
switch($form_id) {
case 'webform_client_form_10': // the id of the form
{$current_object = menu_get_object();
$product_title = $current_object->title;
$form['submitted']['product']['#default_value'] = $product_title; }
return $form;
}
}
I would appreciate any pointers - I'm new to Drupal
That's quite a messy way round of doing what you need to, you should just put the product nid in the URL as part of the query string in the link from your product page and then load it up from the webform.
In your node template/preprocess:
$webform_path = 'node/10'; // Or whatever the webform's nid is
$link = l('Request Information', $webform_path, array(
'query' => array(
'product_nid' => $product_node->nid
)
));
echo $link;
Then in your form alter:
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'webform_client_form_10' && isset($_GET['product_nid']) && is_numeric($_GET['product_nid'])) {
$product_node = node_load($_GET['product_nid']);
if ($product_node) {
$product_title = $product_node->title;
$form['submitted']['product']['#default_value'] = $product_title;
}
}
}
Note that you don't return the form from the hook_form_alter function, the $form variable is passed in by reference so changes are stored that way.

Resources