I have a php function in a drupal module. This function outputs some random text. I want to attach this module to a Drupal article so that each time someone creates an article, random text will appear in it. How can I do this?
The solution here is to use a Drupal hook function to modify the content of the node.
Assuming your module is called "my_module":, you'd add another function to your my_module.module file as follows:
function my_module_node_view(&$node, $view_mode, $langcode) {
// We want to make sure this only applies to nodes of the content type "article"
if ($node->type == "article") {
// Append the output of your function to the body; this could easily be added to any other field as well
$node->body['und'][0]['value'] = $node->body['und'][0]['value'] . my_module_random_text_function();
}
}
Note: the $node object is automatically passed to this hook function by reference, so you don't need to worry about returning anything from the function.
If you wanted to apply this at the theme layer, you could use the theme_preprocess_node hook in your theme's template.php file, but your original question suggested that you'd already gone down the plugin route.
Related
I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).
the function inside the plugins is like this
if( !function_exists('send-invoice') ){
function send-invoice(){
//The Plugin Invoice
}
}
This is what I did
function send-invoice(){
//My Custom Invoice
}
add_action('init', 'send-invoice');
How can I make sure that my code runs before the plugin codes?
The plugin load before the theme, I tried plugin-loaded hook but nothing changed
You can to use the anonymous function for example:
add_action('init', function() {
//code here
});
More detail is here
Or use another hook muplugins_loaded:
function send-invoice(){
//My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.
The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.
The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way
Trying to fetch output in A.tpl but not getting any output. I think i'm doing something wrong to call php function in tpl file.
A.tpl
{myModifier}
B.php
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template)
{
return "100.70";
}
}
$smarty1 = new Smarty();
$smarty1->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
I 've already used this code. And this doesn't seem to work. It also breaks my existing working code in A.tpl post this use.
My Need here is to get output from the php function in A.tpl from an external php file.
Thanks in Advance. Sorry for being noob.
To add this modifier to Smarty and use it in your template, you're best to use a WHMCS hook.
If you create a new PHP file under the '~/includes/hooks/' directory (you can name it anything - in this case, let's use 'myhook.php'), WHMCS will automatically inject this hook on each request.
For this, you're going to want to use the ClientAreaPage hook. Inside your hook, you can then access the global $smarty variable.
Example:
function MySmartyModifierHook(array $vars) {
global $smarty;
// I recommend putting your Geolocation class in a separate PHP file,
// and using 'include()' here instead.
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template) {
return "100.70";
}
}
// Register the Smarty plugin
$smarty->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
}
// Assign the hook
add_hook('ClientAreaPage', 1, 'MySmartyModifierHook');
That should do the trick. If you want to explore with other hooks, you can take a look at the Hook Index in the WHMCS documentation.
The function names in each of your hook files must be unique.
As a side note, if you're only wanting to run this hook on specific pages, you can check the templatefile key in the passed $vars array. For example, say you only wanted this hook to run on the 'View Cart' page on the order form:
function MySmartyModifierHook(array $vars) {
global $smarty;
// If the current template is not 'viewcart', then return
if ($vars['templatefile'] != 'viewcart')
return;
// ... your code here ...
}
Also, note that with hooks like the 'ClientAreaPage' hook, returning an array of keys and values will automatically add them as Smarty variables. So if your hook function ended with return ['currentTime' => time()];, you could then use {$currentTime} in your Smarty template to output its value.
I'm trying to customize the rendering of the standard_end_of_body(), but I can't seem to find the proper function.
I found the abstract function in /lib/outputrenderers.php, but not the actual theme implementation. In the code it is mentioned that it should be in the theme renderer, so I checked into every renderer, as well as the themes mine is based in (bootstrap and Elegance), but so far, nada.
So I'm very much open to any suggestions!
Thanks
In /theme/yourtheme/renderers/php
add this
class theme_yourtheme_core_renderer extends core_renderer {
public function standard_end_of_body_html() {
// Optionally add the parent html.
$output = parent::standard_end_of_body_html();
$output .= 'my stuff';
return ($output);
}
alternatively, you can also add footer html to the setting additionalhtmlfooter
via site admin -> appearance -> additional html
or direct to /admin/settings.php?section=additionalhtml
in template.php many times they used classes_array..am not getting the meaning and why they using,..what is the purpose of classes_array and when we have to use that in drupal7 .tpl.php
example code:
if(in_array('administrator',array_values($variables['user']->roles)))
{
$variables['classes_array'][]="debug";
}
$variables['classes_array'] is used in preprocess functions. It adds classes to be used in rendering the element to be processed. In your example, a class named "debug" will be added to the html container of the rendered element: if the actual code is
function <YOUR THEME>_preprocess_html(&$variables) {
if (in_array('administrator',array_values($variables['user']->roles))) {
$variables['classes_array'][]="debug";
}
}
your theme will output a body tag like
<body class='debug [...OTHER CLASSES...]'>
for users with administrator role.
You can also add classes to nodes, or other kind of elements for which a preprocess hook is available. E.g. you could write a node preprocess function:
function <YOUR THEME>_preprocess_node($variables) {
$classes_array[] = 'my-class';
}
if you wanted to add 'my-class' to every node of your site.
In general, you will not find $classes_array among the defined variables in tpl.php files. Your theme will, most of the times, implode them in a $classes variable. It must be noted, however, that a kind of confusion arised over time, so different themes may use $classes_array, $attribute_array, $classes, $attributes['class'] and so on for the same purpose, so you should check your theme's documentation to find out what suits your case.
I have a theme that has a plugin called Option Tree integrated in it to create a theme options panel (that plugin allows for a "theme mode" so it isn't installed as a plugin).
The Option Tree plugin allows you to access the saved data in the theme options by using this function:
$data_of_a_single_option = ot_get_option( 'name_of_option_field_to_retrieve', NULL );
I have another plugin that needs to get data saved in the theme option, but the theme (and option tree) is loaded after the plugin is, so calling the function leads to a "function does not exist" error.
Is there a way the plugin can call that function and get the data and be able to store that data in a variable?
I tried using an action like this:
function get_special_data() {
$test = ot_get_option( 'test_field', NULL );
return $test;
var_dump( $test );
}
add_action('after_theme_setup', 'get_special_data', 2);
// then just below in same plugin
$data_from_theme = get_special_data();
Because I read that Option tree is loaded after_Theme_setup with a priority of 1.
By using add_action, I can see the data (var_dump outputs it correctly), but I can't get the data from inside the plugin by calling the get_special_data() function because it sends a "ot_get_option function does not exist" error.
Is there any way to do this? Or am I trying to solve the wrong problem?
Thanks in advance!
I believe you are looking for the plugins_loaded action. That should work as long as the plugin defines that function without some other action required first to load the function.
Also, in your code example $data_from_theme = get_special_data(); is going to execute before the action callback you're defining actually fires.