So I've just installed the Baum package in Laravel and put together a small tree of categories.
I've been able to display the tree in nested JSON format with the getDependentsAndSelf() method, but I have no idea how to go about actually displaying this in a usuable format with laravel.
Ideally I'd just like to spew them out in an indented list format, but I feel like that would probably require some recursion and I have no idea how to do that in Laravel.
Here's the output I have right now:
{"14":{"id":14,"parent_id":null,"name":"Root","lft":1,"rgt":6,"depth":0,"children":[{"id":15,"parent_id":14,"name":"Child 1","lft":2,"rgt":5,"depth":1,"children":[{"id":16,"parent_id":15,"name":"Child 2","lft":3,"rgt":4,"depth":2,"children":[]}]}]}}
Essentially it's just a tree of the format
- Root
- Child 1
- Child 2
So what's the best way to go about this in laravel? In php I could have just made a function that recursed upon itself, but I'm not sure how to do that in my laravel view.
You can use method described here: https://gist.github.com/etrepat/6920301
<?php
$roots = Category::roots()->get();
echo "<ul>";
foreach($roots as $root) renderNode($root);
echo "</ul>";
// *Very simple* recursive rendering function
function renderNode($node) {
echo "<li>";
echo "<b>{$node->name}</b>";
if ( $node->children()->count() > 0 ) {
echo "<ul>";
foreach($node->children as $child) renderNode($child);
echo "</ul>";
}
echo "</li>";
}
Related
I have a mystery about RSS feed generated by Views RSS
In a Drupal 9 project I have a custom theme. In this custom theme I have
web/themes/custom/CUSTOM_theme/templates/views-view-row-rss.html.twig
but it never uses by Drupal.
In CUSTOM_theme.theme I have
function CUSTOM_theme_registry_alter(&$theme_registry) {
echo '<pre>';
var_dump($theme_registry['views_view_row_rss']);
echo '</pre>';
}
and the path for the theme is good..
["path"]=>
string(34) "themes/custom/CUSTOM_theme/templates"
BUT in web/core/lib/Drupal/Core/Theme/ThemeManager.php I add (only for test, of course, I don't want to modify this file) for the hook views-view-row-rss
echo '<pre>';
var_dump($info);
And the result says the path is
["path"]=>
string(35) "modules/contrib/views_rss/templates"
More mysterious, in both cases, the thme_path is good
["theme path"]=>
string(24) "themes/custom/CUSTOM_theme"...
Do you know how to correct this and use the web/themes/custom/CUSTOM_theme/templates/views-view-row-rss.html.twig
thanks
Ok, someone found the trouble on a Discord, thanks to him.
/
* Implemenents hook_theme_registry_alter().
*/
function views_rss_theme_registry_alter(array &$registry) {
// Use the twig file that comes with this module so that the CDATA wrapper can
// be added.
$module_dir = \Drupal::service('extension.list.module')->getPath('views_rss');
$registry['views_view_row_rss']['path'] = $module_dir . '/templates';
}
the module will search the path on views_rss each time.
So, the solution is to do
function CUSTOM_theme_registry_alter(array &$registry) {
// Use the twig file that comes with this module so that the CDATA wrapper can
// be added.
$module_dir = \Drupal::service('extension.list.theme')->getPath('NAME OF THEME');
$registry['views_view_row_rss']['path'] = $module_dir . '/templates';
}
I'm currently building links like this:
<?php echo get_the_title(111); ?>
I was building links like this using the WPML plugin (but steering away from it due to various reasons):
<?php icl_link_to_element(111); ?>
This builds a link similar to my first example.
So my question is is there a native Wordpress function that does this? I'm sure there must be, but cannot find the solution anywhere. I'm looking to reduce my markup...
Thanks!
EDITED WITH ANSWER
This is how I built my custom function:
function build_pretty_link($id,$link_title='') {
if($link_title=='') {
$link_title = get_the_title($id);
}
$link_url = get_permalink($id);
echo "{$link_title}";
}
WordPress give a function that print an anchor tag with the title and the url, but you have to be in a the loop (http://codex.wordpress.org/Function_Reference/permalink_anchor).
I suggest you to create your own function (the functions.php file in your theme is here for that).
You can do someting like that :
function vp_link_to($post_id) {
echo '<?php echo get_the_title($post_id); ?>';
}
get_permalink(x);
Where the ID of the page is x and wrap this in whatever you need, so
$id = 10;
$link = get_permalink($id);
echo 'Linked text';
Is it possible to get a pod items index in the template? For instance, if I have a pods variable that has ten entries in it, could I make a template that renders each entry with its place in the list or, even better yet, if it's first/last as such:
Rendering item {#index} out of {#total} called {#title}
Note: I mean the index in the scope of the group of items being rendered. Not the posts ID or something else. If this doesn't exist it would be a great feature to have!
It's possible with PHP:
Total in this list: <?php echo $obj->total(); ?>
Or you can get the total (across all pages, if using pagination or limiting):
Total Found: <?php echo $obj->total_found(); ?>
Or you can get the current position in the loop (new in Pods 2.3):
Current Position: <?php echo $obj->position(); ?>
Or you can even do an nth check (CSS nerds know what I'm talking about in regards to how nth-child works):
<?php
if ( $obj->nth( 'even' ) )
echo 'even row';
if ( $obj->nth( 'odd' ) )
echo 'odd row';
if ( $obj->nth( '1n+3' ) )
echo 'you get the picture';
if ( $obj->nth( '3n+0' ) )
echo 'you get the picture';
?>
For info about nth, it just takes the same input as nth-child does in CSS: http://www.w3schools.com/cssref/sel_nth-child.asp
If you'd like these available as magic tags, please submit a feature request at http://pods.io/submit/
I have the same requirement. My scenario is that I need to detect first element so that I can display it in a slideshow. Inside template, I declare a global variable:
<?php global $counter;
if ($counter == 0) {
$class='active';
$counter++;
} else {
$class='';
}?>
Then I echo variable $class to where i want.
By this way, I cannot know totally how many row but It sold my problem.
PHP is deprecated in Pods templates. Now you should use Pods Frontier plugin or put PHP code in WP templates.
I am wondering how to create a simple tag drop-down menu (all tags included DESC) without rewriting the WP core functions. It has to work outside of any loop.
wp_tag_cloud() with the 'format=array' attribute would seem the best choice since it works outside of any loop/template and returns all available tags sorted A-Z (which I need) but the array values contain HTML formatting (instead of just a plain string value) and that is not suitable for creating the drop-down.
i.e.:
<?php $tag = wp_tag_cloud('format=array'); // 'format=array' contains <a>link</> !!!
foreach($tag as $tagkey => $tagvalue) // ...need to be somehow filtered out !!
{
echo "<option value='".$tagvalue."'>".$tagvalue."</option>";
}
?>
The get_the_tag_list() function works great but it doesn't work outside of the template (loop).
Is there a simple way how to get the list of all tags so I can put them into the drop-down?
...OMG! I can't believe I actually asked this publicly!
Of course the code is ...
<?php $tag = wp_tag_cloud('format=array' );
foreach($tag as $tagkey => $tagvalue)
{
$cleanedup = strip_tags($tagvalue);
echo "<option value='".$cleanedup."'>".$cleanedup."</option>";
}
?>
How can I trace all the relevant tags of a page in the form of classes to the <body> tag? I'm referring to how Wordpress puts the following in a page's body class attribute:
home page page-id-12766 page-template page-template-page-home-php cufon2-disabled safari theme-*
This could be extremely helpful in Joomla template development.
Thanks in advance.
I don't know whether there is any out of the box solutions exists for this. As the main structure of the site is mainly based on the Menu structure, the below snippet may be useful. This is used in the templates/mytemplate/index.php file.
<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
$pageclass= '' ;
//$currentpage = ''; //In case if wanted to get the alias of the current page in a later stage.
if( count($active->tree) > 0 ) {
foreach ($active->tree as $key => $value) {
$pageclass .= 'level-'.$key . ' pageid-'.$value. ' page-'.$menu->getItem($value)->alias ;
//$currentpage = $menu->getItem($value)->alias;
}
}
?>
<body class="<?php echo $pageclass; ?>">
This will output something like:
<body class="level-0 pageid-101 page-home">
You may improve this using various values available in the $active variable.
Here's some information on the Body Class Function
How Wordpress determines which classes to apply using it is found in post-template.php
The relevant function you're looking for is get_body_class within this template. It essentially runs a series of checks to determine if the current page meets the criteria for given classes. It also relies heavily on Wordpress specific function calls that helps make that determination.