drupal username hyperlink missing site url - ahref from user_load($node->uid) - php

I'm using the drupal FAQ module which sends an email to the admin including the authors username as hyperlink, defined via:
'creator' => theme('username', array('account' => user_load($node->uid), 'plain' => TRUE)),
http://cgit.drupalcode.org/faq_ask/tree/faq_ask.module?id=9f4fbb7859c8fc24977f5d67cd589685236e442d#n480
unfortunately it only links to /users/joeblock and thus missing the site url https://example.com which means it won't work in emails.
Joe Block
I already tried the module pathologic hoping it adds the site url but didn't help (perhaps because the rendered ahref includes a / infront of it).
Is it possible to modify the hyperlink just for this instance to insert the siteurl?
Update:
adding $variables['link_options']['absolute'] = true;into includes/theme.inc worked.
function theme_username($variables) {
if (isset($variables['link_path'])) {
// We have a link path, so we should generate a link using l().
// Additional classes may be added as array elements like
// $variables['link_options']['attributes']['class'][] = 'myclass';
$variables['link_options']['absolute'] = true;
$output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
}

Yes, it's possible! the faq module uses the theme username. This theme is defined in includes/theme.inc function theme_username
In your custom theme you can implement the template_process_username hook and alter the $variables array.
The theme username uses the url function to create the url. This function accepts the absolute attribute to build an absolute url.
to create this function you can create a custom theme https://www.drupal.org/docs/7/theming/howto/create-a-new-custom-theme-with-css-alone and put the yourthemename_process_username function inside the template.php file of your custom theme.
Otherwise you can add the function in a custom module.
Let's do an example with a custom module (with the markus name) because is much more common to create a custom module than a custom theme.
Create the site/all/modules/custom/markus directory.
Inside this directory create the markus.module file with this content:
<?php
function markus_node_presave($node){
if( $node->type == 'faq' ){
drupal_static('markus_faq_node_save', true);
}
}
function markus_process_username( &$variables ){
if( drupal_static('markus_faq_node_save', false) ){
// alter the link_options only when you came from the ask module otherwise, without
// this if, all the username links in drupal will be absolute url.
// Actually this is not a problem but it may be overkilling
$variables['link_options']['absolute'] = true;
}
}
create the markus.info file inside the markus directory with this content:
name = markus
description = "my custom module"
core = 7.x
package = "markus"
Now from the admin menu enable your theme.
It's better to implement the markus_process_username function in a custom module and not to edit the includes/theme.inc file because in this way you can update drupal much more easly. The drupal core should never be edited :)

Related

Change whmcs template from hook

I too searched in all forums and even I post my question on the whmcs forum but no response. What i need is that i create an addon in modules and i want to change template (from six to five) in hooks of this addon. The aim is to change template for specific clients.
I already test to change the GET var but not working :
$_GET['systpl'] = 'five';
I also tested this but the css files don't load. It redirect me to home :
global $smarty;
$template = $smarty->getTemplateVars('template');
$template = 'six';
$smarty->assign('template', $template);
$template = $smarty->getTemplateVars('template');
Any suggestion please?
I have done this in one of my products - to get this to work, you have to pull the global $systpl variable:
global $systpl;
$systpl = $tpl;
$GLOBALS['_SESSION']['Template'] = $tpl;
$GLOBALS['CONFIG']['Template'] = $tpl;
Where $tpl is the template name you are looking to set, in your case 'five'. You have to also set the GLOBALS variables there so that the user session is maintained with that template and so that the system knows to use that template name when pulling from the config.
Hope that is of help.
In WHMCS to load another template folder for specified page, I did:
<?php
use WHMCS\Database\Capsule;
use WHMCS\View\Menu\Item as MenuItem;
define("CLIENTAREA", true);
// Set the template you want to use for the custom page BEFORE init.php is called
$GLOBALS['_REQUEST']['systpl'] = 'five';
require("init.php");
// WHATEVER YOU ARE DOING IN HERE
// Set the session back to the default template:
$GLOBALS['_SESSION']['Template'] = 'six';
$ca->output();

Create new page in drupal

Iam new in drupal. I want to create a page and want to add data in page directly with out using backend content. I have created a page like this, page-test.tpl.php. How can I call this newly created page in browser?
First you have to create your own custom module.Let the module name be test.Create 2 files namely test.info and test.module.Declare a hook_menu function inside the test.module file.Since your module name is test your hook_menu will be named as test_menu.
Hook_menu enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only, or they can register a link to be placed in a menu.
Test.module
function test_menu() {
$items = array();
$items['test'] = array(
'title' => 'My Page',
'description' => 'Study Hard',
'page callback' => 'test_simple', //Calls the function
'access arguments' => array('access content'),
);
return $items;
}
function test_simple() {
return array('#markup' => '<p>' . t('My Simple page') . '</p>');
}
Test.info
name = Test
description = Provides a sample page.
core = 7.x
After adding this try clearing the drupal cache and then navigate to /test.Try this link if you are a beginner.Try reading hook_theme which allows you to use your custom template.More about hook_theme here.Hope it might help u mate.. :)
Pages in drupal are stored in the database, not in actual code files like old websites used to. The template files (.tpl.php) are exactly that - templates. So for example, lets say you have blog content. You'd have to create a content type of 'Blog'. you could then create a template file (ex. node--blog.tpl.php) which would format the way the blog pages would look. But you would still need to add new pages through the drupal interface in order to add them to the site. You could always use an input type of PHP if you'd like to include php in the body of the page.
If you simply want to display a php file in your browser from your site, you can just upload a php file of your choice to your sites/default/files directory and access it directly via the file path.

How to display a Drupal 6.20 menu in WordPress?

Is is possible to display (via php) the main menu of a Drupal 6.20 site in a WordPress theme template file located in a subdirectory on the same domain?
Right now, I'm displaying the menu by copying the static html from the Drupal site and adding it to header.php in the WordPress template in the site located in mydomain.com/blog/. But of course that's not going to work when another menu item is added to the Drupal site, or the Drupal menu is changed in any way.
So is there a Drupal php function that will pull the menu into the WP file?
Failing that, is there a way with php to parse a Drupal page for the html of the menu (yes, this would be ugly) and display it in WP?
The first part of the challenge is to output only the menu, with as little (or none) of the surrounding HTML as possible, so you have as little work to do in parsing the HTML as possible.
The second part is to take that output from Drupal and actually display it on your WordPress site.
You could add the Drupal database as a secondary database in WordPress using the a new instance of the $wpdb object, write the query to get the right content from the tables, and format the results. That could work, but might be overkill.
An alternative workable option may be to use JSON to format the output of the primary links, using the drupal_json function in Drupal, then consume the JSON feed in Wordpress.
I'm assuming:
you have admin access to login to the Drupal site, which you'll need to create nodes, and clear the theme cache
you want to output the Primary Links menu, which 90%+ of Drupal sites use. This is probably true, but it is possible your site uses custom menus. If so, this is still possible, you'd just write slightly different code in step 3.
The steps would be:
Create a Drupal node (you can call it anything, it's just a placeholder)
Get the node id of your dummy page (ie., node/234). From the node id, create a one-off page template in your Drupal site's themes folder. It should be called page-node-xxxx.tpl.php, with xxxx being your node id
Add this code to page-node-xxxx.tpl.php:
<?php
drupal_json(menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')));
?>
This will create a JSON feed of your menu items.
Clear the theme cache of your Drupal site by visiting http://yoursite.com/admin/build/themes and visit http://yoursite.com/node/xxxx to see the raw JSON feed.
You should now be able to use a jQuery method like $.getJSON or $.ajax in your Wordpress theme to consume and display the JSON feed, or possibly use json_decode and curl to output your array as HTML.
A good thing about Drupal's drupal_json function is that it already sends the correct JSON headers, so now all you have to do is write the jQuery or PHP that does what you need.
I'm assumed you are more of a Wordpress specialist and have a working knowledge of Drupal but maybe not a lot of familiarity with its inner workings. So, sorry if it seemed too basic (or not basic enough :).
The Drupal theming engine is very modular - you may be able to make an appropriate PHP call into Drupal to get just the menu rendered, then emit that HTML as a part of your WordPress page.
g_thom's answer is very good and if you wish to create a very simple module to output the main navigation you can write something like this:
<?php
function getmenus_help($path, $arg) {
// implementing the help hook ... well, not doing anything with it just now actually
}
function getmenus_all() {
$page_content = '';
$page_content = json_encode(menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')));
// fill $page_content with the menu html
print $page_content;
return NULL;
}
function getmenus_menu() {
$items = array();
$items['getmenus'] = array(
'title' => 'Get Menus',
'page callback' => 'getmenus_all',
'access arguments' => array('access getmenus'),
'type' => MENU_CALLBACK,
);
return $items;
}
// permissions
function getmenus_perm() {
return array('access getmenus');
}
In your PHP code you can then write something like:
function primary_links() {
$primary_links = file_get_contents(SITE_URL . '/getmenus');
$primary_links = json_decode($primary_links);
$primary_links = (array)$primary_links;
$i = 0;
$last = count($primary_links);
$output = '';
foreach ($primary_links as $pm) {
$href = $pm->href;
if (strpos($pm->href, 'http://') === FALSE) {
if ($pm->href == '<front>') {
$href = SITE_URL . '/';
} else {
$href = SITE_URL . '/' . $pm->href;
}
}
$output .= '
<li>
'.$pm->title.'</li>';
$i++;
}
return $output;
}
I hope this helps!
PS: Make sure you update the module's permissions to allow anonymous users to have access to the path you set in your module - otherwise you will get a 403 Permission Denied.

Drupal 7 - What is the variable in template.php that dictates which page template is used?

Ok, here's the deal: I am constructing a Drupal website that has several different sections. Each section is a view that displays a content type. (Each section has it's own content type) For example, I have a view that points to ?q=blog which displays content type blog.
All the sections look a little different than each other. Not like 'website-within-a-website' different but different enough that they can't all use the same template file and each be modified with CSS. Each section needs it's own page.tpl.php.
Unfortunately, AFAIK Drupal theme's .info files can only either assign one page.tpl.php for the entire theme or assign a page-node-####.tpl.php for each node. There is going to be lots of content on this website so setting Drupal to make a new identical page-node-####.tpl.php for every created node would get unmanagable very fast.
To solve this problem, I am going to use pathauto to create an alias for each content type. For example, all nodes of content type blog are given an alias ?q=blog/[post title]. Modify template.php to use page-blog.tpl.php for any page who's alias starts with the word 'blog'.
Other people have tried doing this sort of thing and have created functions such as the one described. Unfortunately, all the ones I have seen are for Drupal 6 or below. I have tried modifying existing ones with no success. So far, though, I think this is on the right track:
function basic_preprocess_page(&$vars, $hook) {
...
if( module_exists('path') ) {
$alias = drupal_get_path_alias( $_GET['q'] );
$site_section = "blog";
if( strpos( $alias, $site_section ) === 0 ) {
$VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
}
}
}
I cannot find $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE does anyone know what it is?
Maybe my site is structured badly. If anyone knows how to restructure my site so I can more easily make a theme with seperate sections please share how!
Thanks a million! (c:
EDIT: Perhaps I need to use template suggestions instead. Does anyone know the function or variable to use to set this?
They changed the name of this array key in D7 and I haven't seen it documented anywhere. I finally figured this out after a good bit of debugging. You can override the theme template in template.php with a hook_preprocess_page() like so:
function myTheme_preprocess_page(&$vars) {
global $node;
if ($node->type == 'blog') {
$vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
$vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}
Oh and don't forget to flush the Drupal caches after making changes to your template.php.
Ok, I found it:
http://drupal.org/node/223440#comment-991840
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
Credit to this function goes to user mfb.
I had a lot of trouble with this so I will explain it here in case anyone finds it useful.
This function goes in your template.php. It needs to be part of the <theme name>_preprocess_page function. What it does is it takes the alias and then explodes it into a bunch of different components. For example if you are on a page with the alias ?q=blog/blog-post-title it would be exploded into blog and blog-post-title. It then turns each component into a name for a template suggestion. It puts each template suggestion into the template_files[] array (inside the $variables[] array) so that the page now has two new template suggestions:
page-blog, and page-blog-blog-post-title
Template suggestions are alternate template files. In this case they are for pages, but they don't necessarily have to be. You can have template suggestions for anything you can think of including blocks, nodes and the like. Don't let the name 'template suggestion' fool you. Template suggestions will be used over default templates as long as they exist. I don't know why it was named like that. I think it should be renamed.
What you do, then, now that you've set up Drupal to look for a template suggestion that points to your alias, is create a new template file where all the rest are in your theme. In this case, let's say I want to theme my entire blog section. In the templates folder I should create a file named page--blog.tpl.php (note the --double hyphens--) with the layout I want.
Drupal will use the most specific template suggestion it can find so if you wanted you could make one blog post to look completely different than the rest of the site long as you make a template for it named page--blog--blog-post-title and put it in your theme's templates directory. (again, note the double hyphens.)

Open file, write to file, save file as a zip and stream to user for download

I'd like if someone could give me some advice on creating this script, which I will add to the existing plug-in (see code below) script below.
So what I have now (with the script below) is a means to insert a predefined set of defaults into the wordpress site. What I'm wanting to add, is a helper utility, activated by a button or link that just reads "Copy Settings", that will take a site's existing settings (the sb2_options), write that to a file, then package the resulting file, along with the original file into a new zip file that essentially becomes a custom copy of the original plug-in for use in another site.
So the code needs to take an existing .php file containing the static code, open it up for writing, then insert all the name/value pairs from the wordpress options table matching a specific prefix (for example, all my custom options are prefixed with "sb2_"). Once it's done this, it would save the resulting file as "plugin.zip", for example and stream it to the user for download.
Here is the code that I have now, which sets up the site's defaults...
<?php
/**
* Plugin Name: my plugin
* Description: Sets up your sites defaults.
* Version: 1.0
*/
function sb2_plugin_init()
{
if ( get_option( 'sb2_plugin' ) == "")
{
//Begin Insert List here. Open the file and write out all the name value pairs, just like in the example.
//Option 1",
$sb2_option1 = "test";
//Option 2",
$sb2_option2 = "test";
//Option 1",
$sb2_option3 = "test";
//End insert list here
//update site defaults
update_option('sb2_option1', sb2_option1);
update_option('sb2_option2', sb2_option2);
update_option('sb2_option3', sb2_option3);
//etc
// Create post objects
$my_post = array();
$my_post['post_title'] = 'Main Blog Post Title';
$my_post['post_content'] = 'Main Blog Post Content';
$my_post['post_type'] = 'post';
//TODO >>> NEED TO MAKE THE POST STICKY
// Insert the post into the database
wp_insert_post($my_post);
wp_cache_flush();
update_option('sb2_plugin', "1");
}
}
add_action( 'init','sb2_plugin_init');
Reading and writing to file shouldn't be difficult for you but here is a good way to create zip files.

Categories