Why wouldn't these theme functions work with Drupal D6? - drupal

I'm at my wits end with Drupal 6 right now, and I'm hoping a second pair of eyes might be able to point out some syntax sublety I was missing earlier this morning.
Lets say I have a theme called my_theme.
This is a theme that subthemes from Ginkgo, a theme which which in turn subthemes from
Rubik, whith in tun subthemes from Tao, a fairly common CSS reset theme.
I'm trying to understand how to declare my own theme functions, so I can
clean up the mark up on a fee page I'm workign on for site.
Now my understanding of theming is along the lines of ' if register a
theming function with hook_theme, passing it into an array, with the
function's name, you'be able to call it in future with
theme_function_name from within your theme.
For example, if have a module called my, I'd immplent the hook_theme
like below, defining the name of the function, and defining which
argument should be passed into it:
<?php
function my_theme() {
$items = array();
$items['randomtext'] = array('arguments' => array('element' => NULL));
$items['button_links'] = array('arguments' => array( '$links' => NULL, '$attributes' => NULL));
return $items;
};
Now I have the functions registered, I just need to implement them like
this somewhat contrived example:
function theme_randomtext($element) {
$output = ' <h1>This is some very random text with' . ' this text concatenated: ' . $element . '</h1>';
return $output;
}
I can now use this new function by calling
<php
print theme('randomtext', 'some arbitrary words')
?>
And expect the following content to be returned:
<h1>This is some very random text with this text concatenated: some arbitrary words</h1>
I'm getting nada.
And I don't know why. When I look at the theme registry, I see my
function listed as my_randomtext - I've tried calling both of these
options, in case I should have been adding the theme prefix:
theme('my_randomtext', 'getting desparate');
theme('randomtext', 'really losing my rag now');
Still no luck.
Why might these theme functions not be working? And what advantage does
this give you over simply declaring a function like so in a theme?
function manual_random_text($element) {
$output = ' <h1>This is gives me everything I aleady need,
'without relying on the weird themeing sytem. It's perfect for' . $element . '</h1>';
return $output;
}

Ah, after a bit of distance from the problem, I see the issue.
For this to work, I need to add the prefix of the theme I declare the function in, to make it available to me for use.
So if my module name was my, the desired theme function name would be:
function my_random_text($element) {
$output = ' <h1>,
'This is finally outputting content. Now I can: ' . $element . '</h1>';
return $output;
}
The thing is, if I'm creating an override to a theme function that doesn't already exist, like random_text, there are no functions for it to override anyway.
The term theme override suggests there being a default theming function to override - in many themes, we're not making explicit overrides of functions, but creating alternatives to use in specific cases.
I'm now assuming that I'd need to define this in a module if I wanted to define a function as theme_function_name rather than themename_theme_function_name.
This page here about theming on Drupal.org was a useful quide in finding out some of this info.

Related

is wrong to add my code in wp admin page wordpress

I must to add new options and functions in post pages in admin panel. I call a new function in edit-form-advanced.php and edded this function in template.php file. The question is this wrong? Becouse my function is in one file with functions on wordpress. Or maybe must be in other file? but where i must call it?
For wp-content part i know and i make a child theme of parent theme, but i do not know what to do when i must add code in wp-admin part.
example:
edit-form-advanced.php
do_custom_boxes( null, $post );
and in template.php
function do_custom_boxes( $screen, $object ) {
global $wpdb;
$appTable = $wpdb->prefix . "post_panel";
$query = $wpdb->prepare("SELECT * FROM $appTable WHERE post_id = ".$_GET['post']." ", $screen);
$applications = $wpdb->get_results($query);
......
}
Short answer: Yes, it's wrong to do so. Whenever you update your WordPress you'll loose all your changes.
WordPress allows you to hook into its code, modify its behavior and many things.
Please read about actions and filters.
Basically, Actions allow you to fire a function when something happens in WordPress.
For example:
<?php
function do_something_when_admin_pages_init() {
// Do something here
}
add_action('admin_init', 'do_something_when_admin_pages_init')
Filters allow you to modify data/output of another function. It's like it let you step in the middle, do something with the data and then continue.
Example from the WordPress page:
<?php
function wporg_filter_title($title) {
return 'The ' . $title . ' was filtered';
}
add_filter('the_title', 'wporg_filter_title');
This modifies the title before it's printed.
So with those two ways of 'hooking' into the WordPress code, you can write your code in your theme's functions.php file, or write a Plugin (it's up to you).

Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.
what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

Drupal 7 Views custom view template fields

I've successfully created a custom view template for my Drupal 7 site but am having issues adding attributes to the content which is outputted. I've searched high and low for the answer to this but to no avail.
I have a view called: views-view-fields--homepage-articles.tpl.php
I am printing content like :
$fields['title']->content
This is fine and expected, and outputs:
Title
But I want to add classes to it - how? I'm thinking I need to write a hook, but I cannot find this documented anywhere. At the moment my solution is a string replace:
<?php print str_replace('<a ', '<a class="brand-blue uppercase nodecoration"', $fields['title']->content); ?>
As you can imagine, this is not a satisfactory or long-term solution.
Many thanks!
You should be able to add the classes to the field using template_preprocess_views_view_fields().
Edit: Couldn't do it the way I thought, but you can overwrite the output of the field like so:
function MY_THEME_preprocess_views_view_fields(&$vars) {
$view = $vars['view'];
if ($view->name == 'node_listing') {
foreach ($vars['fields'] as $id => $field) {
if ($id == 'title') {
$field_output = l($view->result[$view->row_index]->node_title, 'node/'. $view->result[$view->row_index]->nid, array('attributes' => array('class' => 'brand-blue uppercase nodecoration')));
$vars['fields'][$id]->content = $field_output;
}
}
}
}
Have you tried using Semantic Views? https://drupal.org/project/semanticviews - that way you can override the classes within the UI instead of template files, may suit your needs better.

How to add warning text in Drupal comment form

I want a warning message displayed in the comment form when people try to add comments:
"Please write comments in correct
grammatical English, otherwise they
will not published"
How can I do it?
Here is how you can do it by using hook_form_alter in your own module:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case "comment_form":
$form['#prefix'] .= "<div><p>Show some text before the comment form.</p></div>";
break;
}
}
You can alter the comment form so that your guidelines are added to it. There are a handful of ways to alter forms in Drupal. You can do it in your theme's template.php file (which I prefer for simple changes) or in a custom module. This article describes both methods, in Drupal 5 and 6, however not for the form you're interested in. However, the method used is the same that leads to the solution below. This is how you can make the change via template.php:
The following PHP code can be added to your theme's template.php file:
function YOURTHEME_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
function YOURTHEME_comment_form($form) {
$output = '';
$output .= '<div class="comment-help">' . t('Please write comments in correct grammatical English, otherwise they will not published.') . '</div>';
$output .= drupal_render($form);
return $output;
}
Replace YOURTHEME with the name of your theme. If you already have a YOURTHEME_theme function you will need to add the 'comment_form' key to the array it is already returning. I doubt you do, but it's worth mentioning just in case.
A note: you should not be editing any of the themes in /themes, but you should have made a new theme or copied and renamed any of those themes into /sites/default/themes or /sites/all/themes.
The above code is based on code from this page.
Once you are inside a hook_form_alter function you can use the Development module (http://drupal.org/project/devel) dpm() function in place of var_dump to help view and isolate which properties to change in the big form arrays. I find this is a must-have when trying to figure out changes to an existing form. It puts all the elements of the form array into clickable rows.
In Drupal 7 go to
admin/structure/types/manage/mycontenttype/comment/fields/comment_body
There you can add your text. It will be shown below the field as usual. If you want the warning displayed above the field, you'd have to go the form_alter way.

How to insert a block into a node or template in Drupal 7?

In Drupal 6, it was easy to insert a block into a template with the following code:
$block = module_invoke('views', 'block', 'view', 'block_name');
print $block['content'];
However, using the same instructions in Drupal 7 does not seem to work. I have looked around and cannot find the new method.
Does Drupal 7 have a routine that can allow for programmatically inserting a block into a template or node?
D7:
<?php
$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);
?>
'module_name' = The machine name of the module (i.e. the module's folder name). This is true for core modules too, so for instance 'search', 'user' and 'comment' would all work here.
'block_delta' = The machine name of the block. You can determine what this is by visiting the block administration page and editing the block. The URL for editing a webform block, for instance, would be something like:
Drupal 7: admin/structure/block/manage/webform/client-block-11/configure
In this example, 'webform' is the module's name, 'client-block-11' is the block's delta.
Custom blocks will have module name of 'block' and a number for a delta, which you can also find by editing the block.
More information: http://drupal.org/node/26502
This appears to be the solution for inserting blocks into templates for Drupal 7, but it seems a bit clunky and I have no idea about impact on performance:
$block = block_load('views', 'block_name');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
If anyone has a better procedure, please do add.
With wrburgess's answer you may get an error if your server is using a newer version of PHP.
Strict warning: Only variables should be passed by reference in include()...
This is what I did to not cause/get rid of the error.
<?php
$blockObject = block_load('views', 'block_name');
$block = _block_get_renderable_array(_block_render_blocks(array($blockObject)));
$output = drupal_render($block);
print $output;
?>
This work for me:
98 is the id of the block
$block =block_load('block',98);
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
Just tested this in drupal 7 and it works:
$bloqueServicios = module_invoke('views', 'block_view', 'servicios-blo_home');
print render($bloqueServicios);
Good luck!
The module_invoke() function works. However, I found that rendering a block this way apparently won't use a custom template for that block. This might be OK depending upon your needs.
As commented before in other answers, this works as well and also makes use of custom templates:
$raw_block = block_load('your-module', 'delta');
$rendered_block = drupal_render(_block_get_renderable_array(_block_render_blocks(array($raw_block))));
print $rendered_block;
So, if you have a custom block--your-module--delta.tpl.php template file, it will be used to format the block.
Source: http://api.drupal.org/api/drupal/includes!module.inc/function/module_invoke/7
For some reason render() doesn't work for me, but this does:
<?php
$block = module_invoke('block', 'block_view', '1');
echo $block['content'];
?>
In my search to include a block in a template, i came across this post.
As an addition, if you want to include a custom block (that you added through the block interface) you have to use (instead of block_load(); in drupal 7)
$block = block_get_custom_block($bid);
$content = $block['body'];
Improving wrburgess' answer, you can do it in one line...
<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('module_name', 'block_delta'))))); ?>
So for example, I use block number 6...
<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('block', '6'))))); ?>
This worked for my Drupal 7 ,
URL: admin/structure/block/manage/addthis/addthis_block/configure
NOTE:delta and module name present in the url itself
$addblock = module_invoke('addthis','block_view','addthis_block');
print render($addblock['content']);
More information can be found on
http://technarco.com/drupal/insert-block-node-or-template-drupal-7
$block = module_invoke('menu_block', 'block_view', '6');
echo render ($block['content']);
This works for me for printing menu block.
There's module called insert_block for those which want to insert block "Drupal way" (not to program anything, just enable the module). Here's how to set it up.
NOTE: I know this question is about "programmatically inserting a block into a template or node" but Google sends people here even their are looking for non-programmer solution like me.
Have a look how Drupal does it in _block_render_blocks. The result of that function gets passed to drupal_render.
Recently I faced the same issue and I came across a nice solution which describes the solution in drupal as drupal's way.
You can print regions inside any template, but they aren't available out of the box in the node.tpl.php template. To make them available, you'll create a new variable for use in your node.tpl.php template that'll contain all the region content.
Creating new template variables is done by using a preprocess function. In your theme's template.php file, create a function that looks like this:
function mytheme_preprocess_node(&$variables) {
// Get a list of all the regions for this theme
foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
// Get the content for each region and add it to the $region variable
if ($blocks = block_get_blocks_by_region($region_key)) {
$variables['region'][$region_key] = $blocks;
}
else {
$variables['region'][$region_key] = array();
}
}
}
Then, in your theme's node.tpl.php template, you can render any region by doing the following:
<?php print render($region['sidebar_first']); ?>
Where sidebar_first is the name of the region you want to render.
Read the complete article here: https://drupal.stackexchange.com/questions/20054/can-regions-be-printed-within-a-node-template
module_invoke Working fine for render block in the template file, but it's not working multilingual sites.

Resources