Drupal html output theme - drupal

I have a custom module that generates a page of output, my question is about how to add html elements to the output such as headings and paragraphs etc.
My module is as follows
function mymodule_page() {
$output .= '<h3>'.t('Installs '.$cell).'</h3>';
$output .= theme('flot_graph', $graph1);
return $output;
}
is this the correct way to do this?
should line two be like this instead?
$output .= t('<h3>Installs '.$cell.'</h3>');

I prefer do not mix logic and presentation in module files, so I would use a tpl to print the content. For example, in your module I would put this code:
function mymodule_page() {
return theme_render_template(drupal_get_path('theme', 'your_theme') . 'path/to/your/file.tpl', array('cell' => $cell, 'graph1' => $graph1);
}
And in the tpl file:
<h3><?php echo t('Installs') . ' ' . $cell; ?></h3>
theme('flot_graph', $graph1);

Related

How to automatically add IDs to headings using functions.php in Wordpress

I am wondering how I could implement unique IDs for each heading element in a post (excluding pages) in Wordpress. That is, if two headings are identical, they should get different IDs. The IDs should also be descriptive. A dashed version of the actual title text is great.
I have found this code online:
// This function adds nice anchor with id attribute to our h2 tags for reference
// #link: http://www.w3.org/TR/html4/struct/links.html#h-12.2.3
function anchor_content_h2($content) {
// Pattern that we want to match
$pattern = '/<h2>(.*?)</h2>/';
// now run the pattern and callback function on content
// and process it through a function that replaces the title with an id
$content = preg_replace_callback($pattern, function ($matches) {
$title = $matches[1];
$slug = sanitize_title_with_dashes($title);
return '<h2 id="' . $slug . '">' . $title . '</h2>';
}, $content);
return $content;
}
add_filter('the_content', 'anchor_content_h2');
My concern here is that two identical headings will get the same ID. It also only works for H2-elements. Is there a better way to do what I want, or is this kind of thing generally not smart to implement?
There are plenty of ways to do this but if you use the post_name, you'll end up with a unique, dashed and descriptive value.
https://codex.wordpress.org/Class_Reference/WP_Post#Member_Variables_of_WP_Post
If it were me, I would likely throw the code below into the respective template file.
the_title( '<h2 class="' . get_post_field( 'post_name', get_post() ) . '">', '</h2>' );
EDIT - So each h2 isn't tied to a post? You could just append $slug with a unique ID.
$slug = sanitize_title_with_dashes($title) . '-' . uniqid();

The Devel module query log doesn't appear in a custom theme

The Devel module query log appears correctly (at the bottom of the page) when I use one of the default themes, but not when I switch to my custom theme. The query log seems to appear within the inner pages in the custom theme, though not the front page or administrator pages (if those are set to use the custom theme).
I have been through page.tpl.php and html.tpl.php and I can't see any missing variables.
How does the query log get printed on the page?
STEP 1:
Check in your template.php you can find the next hooks. If it doesnt exists, create it.
function YOURTHEME_preprocess_node(&$variables) {
$variables['messages'] = theme('status_messages');
}
function YOUTRHEME_status_messages($variables) {
$m = drupal_get_messages();
foreach ($m as $type => $messages) {
if (count($messages) > 0) {
$output .= "<div class=\"messages\">";
$output .= " <ul>";
foreach ($messages as $key => $message) {
if (count($messages) > 0) {
$output .= '<li class="message '.$type.'"><span class="text">'. $message . "</span></li>";
}
}
$output .= " </ul>";
$output .= "</div>";
}
}
return $output;
}
STEP 2
In your template check if var $message is printed. If it doesnt exists, create it.
print $message;

Trying to insert a linked featured image into the content

I am using filter hook to insert a featured image into the_content, that works, but when I try to wrap the featured image in an anchor tag, the link tag ends up directly after the content(not wrapping the image at all)
Is there something I am missing as far as understanding how to filter the_content()? Here's my code:
add_filter('the_content', 'add_img_to_ps_archive');
function add_img_to_ps_archive($content) {
if (is_post_type_archive('past_symposia') ) {
echo $content . '<a href ="#" "alignleft">' . the_post_thumbnail('symposia-thumb') .
'</a>';
} elseif( is_singular('past_symposia') ) {
echo $content . '<br />';
} else {
return $content;
}
}
Try to use commas ',' and not dots '.' for concatenating - donĀ“t ask me why, but wordpress does that sometimes...
This happened because the_post_thumbnail() outputs the image tag directly to the output buffer. You need to use get_the_post_thumbnail() to return the image tag so you can concatenate it with $content.

Drupal 6: Printing Unadulterated Primary Links and all children

How in the WORLD is possible? I swear, I've read the equivalent of 3 encyclopedias to no avail. I've tried solutions within regions, page.tpl.php and blocks. None of them give me what I need... and I know there are so many other people that need this too!
I've come to the conclusion that I want to print out the menu within my page.tpl.php ... so no block solutions, please.
I want to be able to loop through the primary menu links (AND children) and rewrite the output so that there's no default Drupal class tagging. The closest I've found is this example:
<?php if (is_array($primary_links)) : ?>
<ul id="sliding-navigation">
<?php foreach ($primary_links as $link): ?>
<li class="sliding-element"><?php
$href = $link['href'] == "<front>" ? base_path() : base_path() . drupal_get_path_alias($link['href']);
print "<a href='" . $href . "'>" . $link['title'] . "</a>";
?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
As you can see, links are being reprinted with a custom UL and LI class ... that's GREAT! However, no children are being printed. How would I extend this code so that all children are a part of the list? NOTE: I don't want the children to only appear on their parent page, they must be present all the time. Otherwise, the drop-down menu I have planned is useless.
I sincerely thank you in advance to lessening my gargantuan headache!
It's hard to affect the output once it's got as far as the page.tpl - you might do better looking for template.php functions.
This is one I used to alter the classes of my primary links:
function primary_links_add_icons() {
$links = menu_primary_links();
$level_tmp = explode('-', key($links));
$level = $level_tmp[0];
$output = "<ul class=\"links-$level\">\n";
if ($links) {
foreach ($links as $link) {
$link = l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
$output .= '<li class="sublevel">' . $link .'</li>';
};
$output .= '</ul>';
}
return $output;
}
And then in page.tpl.php I just called it like this:
<?php if ($primary_links) :?>
<?php print '<div id="menu">'; ?>
<?php print primary_links_add_icons(); ?>
<?php print '</div>'; ?>
<?php endif;?>
I had to add a <span> to my links for styling, so I overrode theme_links() in includes/theme.inc
You can copy the function to your template.php, rename it to yourthemename_links(), and modify it as needed.
This function outputs the ul, li tags, the drupal_attributes, classes of 'first', 'last', 'active', etc, and affects the menus throughout the site.
You may also want to check out the functions in includes/menu.inc, including theme_menu_local_tasks() and menu_local_tasks(), if you need to output the primary and secondary differently.
MarkLNH

Drupal Site Map Module

I am looking for a module that can create sitemap in Drupal, but couldn't find any. I tried Site Map module, but it can only generate a sitemap page; it can't create a sitemap block at the end of every page. I also tried site menu module, but it can't create a sitemap block as shown above, as well.
Maybe it's just that I don't know how to configure, but I read every readme file, and tried for a few days, still can't get it to work.
Anyone has any idea?
I had the same problem, after trying a module (site-map) but missing customization options I wrote a custom module. Took less time then messing with the site-map module, for just getting a site map the following code is enough (adapt your-menu):
function sitemap_render_menu ($menu) {
$output = "<ul>";
foreach ($menu as $item) {
$link = $item["link"];
if ($link["hidden"]) {
continue;
}
$output .= "<li>" . $link["title"] . "</li>";
if ($item["below"]) {
$output .= sitemap_render_menu($item["below"]);
}
}
$output .= "</ul>";
return $output;
}
function sitemap_content () {
$output = "<h1>Sitemap</h1>";
$output .= "<span id=\"sitemap\">";
$output .= sitemap_render_menu(menu_tree_all_data("your-menu"));
$output .= "</span>";
return $output;
}
function sitemap_menu () {
$items = array();
$items["sitemap"] = array (
"title" => "Sitemap",
"page callback" => "sitemap_content",
"access arguments" => array("access content"),
"type" => MENU_CALLBACK);
return $items;
}
There is a basic comparison of sitemap modules at http://groups.drupal.org/node/15980
I have used sitemenu and it worked for my needs, but the real answer depends on how you structure your site with taxonomy, content types, etc.
Something like Auto Menu might work for you here as well. You could simply add the menu it generates to a footer block on your front page.
With the Site Map module installed, this php code will print the site map.
<?php echo theme('site_map'); ?>
You could create an empty views block and specify the above for the empty text, selecting the PHP Code input format.
There may be a better way to create a custom block to display php code, but I don't know it.
My idea here is to use Views module with customized block type.
I think you can benefit from Menu block module. as you can create menu blocks for all the menus you need in the footer. Then you can add them all in footer or in a one block using minipanels block (from panels module).
You can use Footer_sitemap module which provides us with the configurable block.
https://drupal.org/project/footer_sitemap
This is a little mod of best answer it uses current theme to show hierarchy
function sitemap_render_menu ($menu) {
$output = "<ul class='menu'>";
foreach ($menu as $item) {
$link = $item["link"];
if ($link["hidden"]) {
continue;
}
$cc=($item["below"]) ? "class='collapsed'" : '';
$output .= "<li $cc>" . $link["title"] . "";
if ($item["below"]) {
$output .= sitemap_render_menu($item["below"]);
}
$output .= "</li>";
}
$output .= "</ul>";
return $output;
}
function sitemap_content ($title,$menu) {
$output = "<h1>$title</h1>";
$output .= "<span id=\"sitemap\">";
$output .= sitemap_render_menu(menu_tree_all_data($menu));
$output .= "</span>";
return $output;
}
function sitemap_menu () {
$items = array();
$items["sitemap"] = array (
"title" => "Sitemap",
"page callback" => "sitemap_content",
"access arguments" => array("access content"),
"type" => MENU_CALLBACK);
return $items;
}
print sitemap_content("Navigational menu","Navigation");
A simple solution which doesn't depend on content being included as a menu item can be achieved by :
Creating a new view
Output as a block
Using Fields:
Content Title (configured to "Link this field to the original piece of content"
Content Type (configured to "Exclude from display")
Format as
Unformatted list with settings - Grouping field Nr.1 select Content:Type;
Filter Criterea:
Content: Published (Yes)
Content Type - Configure to select the content types you wish to include;
Sort criteria - Configure as per your preference

Resources