I was just wondering the best way/practice to implement menus, headers and footers with changing content such as notifications using codeigniter.
For example say I had an alert within the header menu that linked back to data within a database and I needed to check for changes each time a page is loaded. Initially I thought I could call the header using $this->load->view('header') each time, but this would mean I would need a global function to work out any changes on alerts and then pass that to the header view, each time, not good!
I guess I need a global way to call function that loads the website header (menu) from any controller which works out the content and displays the view accordingly.
so for example a controller that shows blog pages.
in your controller constructor - define the folder your blog view files are in and the template name
// the folder your content files are in
$this->templatefolder = 'blog' ;
// the template name
$this->view_template = 'blog_template' ;
in a method when you are ready to call some views
$data['content01'] = 'search_articles';
$data['content02'] = 'main_article';
$data['content03'] = 'suggested_articles';
$this->load->view( $this->view_template, $data );
the template itself
views/blog_template.php
// opening html etc that is generic to website
$this->load->view('tmpl_open');
// so if the header has to be dynamic
// get the header from a model (or library etc)
// and either pass the header content or just echo it out directly
$this->load->model('header');
if( ! $newHeader = $this->header->returnNewHeader() )
{
// fallback if the header doesn't come back from the model
$this->load->view('default_header');
}
else
{ echo $newHeader ; }
// this is optional but IF the template folder is not set
// we have a default folder called 'pages' to look in for the content views
// but in this example the folder is set to be 'blog'
// so the blog view files will be in application/views/blog/search_articles.php etc etc
if( isset($this->templatefolder)){
$templatefolder = $this->templatefolder . '/' ; }
else { $templatefolder = 'pages/'; }
// header that is specific for the content
$this->load->view($templatefolder . 'header');
// so in this specific example its going to load 3 view files, but this part is completely flexible
if(isset($content01))
$this->load->view($templatefolder.$content01);
if(isset($content02))
$this->load->view($templatefolder.$content02);
if(isset($content03))
$this->load->view($templatefolder.$content03);
if(isset($content04))
$this->load->view($templatefolder.$content04);
if(isset($content05))
$this->load->view($templatefolder.$content05);
if(isset($content06))
$this->load->view($templatefolder.$content06);
if(isset($content07))
$this->load->view($templatefolder.$content07);
if(isset($content08))
$this->load->view($templatefolder.$content08);
// example of an optional file that you can uncomment for testing
// $this->load->view('objecttesting');
// bottom nav bar generic to website
$this->load->view('tmpl_footer');
// closing html etc generic to website
$this->load->view('tmpl_close');
Related
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 :)
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();
With Drupal 7, I have a content type with multiple fields. I then have a view page that takes this content type and displays all the content on it.
So think of it like a blog.. and then a main blog display page.
I have it set so that a menu item is automatically created in the proper place.
I also have Pathauto set up so that it creates a link
www.site.com/blog_anchor_node-title
The individual content page will never be accessed, so I'm not worried about the strange url, however, since hashtags are not supported by pathauto, I used anchor
I need every instance of anchor to be replaced with a # via the template.php file.
This will allow anchor tags to automatically be added to my main menu, the footer, as well as jump menus on the "blog" page it's self.
so far, the closest thing I have is:
function bartik_theme_links($variables) {
$links = $variables['links'];
if (!(strpos($links, "_anchor_") === false)) {
$links = str_replace("http://", '', $links);
$links = str_replace("_anchor_","#",$links);
} }
This doesn't work.
First, your theme_links implementation should not include theme in its function name. And second to quote the documentation page linked before, `$variables['links'] is …
An associative array of links to be themed. The key for each link is used as its CSS class. Each link should be itself an array, with the following elements
Your replacement does not work because you're using strpos on an array.
To make this work go to the API documentation page, copy the code (yes the hole code) and just insert something like the following at the beginning:
function bartik_links($variables) {
$links = $variables['links'];
foreach($links as $key => $l) {
// do your replacements here.
// You may want to print out $l here to make sure
// what you need to replace.
}
//...
}
Also make sure the function is named properly.
To allow me to use the # symbol in a URL, what worked for me was adding the following to my template.php file (before the function above you want to call). You don't have to change anything else besides YOURTHEMENAME to your theme's name:
function YOURTHEMENAME_url_outbound_alter(&$path, &$options, $original_path) {
$alias = drupal_get_path_alias($original_path);
$url = parse_url($alias);
if (isset($url['fragment'])){
//set path without the fragment
$path = $url['path'];
//prevent URL from re-aliasing
$options['alias'] = TRUE;
//set fragment
$options['fragment'] = $url['fragment'];
}
}
I have been trying to load fivestar module and show the rating widget of the selected node in an external php file. I have gotten the rating widget displayed on the page but it only displays degraded version of the widget (non-JavaScript, dropdown widget and "Rate" button) I looked into the source code of the page but the javascript for fivestar module was not loaded. I have tried to load javascript using following functions but had no luck:
fivestar_add_js();
$path = drupal_get_path('module','fivestar');
drupal_add_js($path.'/js/fivestar.js', 'inline', 'footer');
The following is the code in the php file:
//require the bootstrap include
require_once 'includes/bootstrap.inc';
//Load Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
fivestar_add_css();
// I have used one of the following two functions one at a time to test.
fivestar_add_js();
$path = drupal_get_path('module','fivestar');
drupal_add_js($path.'/js/fivestar.js', 'inline', 'footer');
$book = $_GET["book"];
$chap = $_GET["chap"];
$prob = $_GET["prob"];
$string = $book.'/'.$chap.'/'.$prob;
$query = "SELECT ctcr.nid FROM content_type_comments_ratings AS ctcr WHERE ctcr.field_problem_value = '".$string."'";
$result=db_query($query);
$row = db_fetch_array($result);
if(isset($row['nid']))
{
$nid = $row['nid'];
node_load(FALSE, NULL, TRUE);
$fivestar = node_load($nid, NULL, TRUE);
if (function_exists('fivestar_widget_form')) print fivestar_widget_form($fivestar);
}
If you could give me a hint or direct me to some reading on the web, I would appreciate it. Thank you very much in advance.
By doing all this on an 'external' page/file, you circumvent the Drupal theming system - drupal_add_js() (and fivestar_add_js(), as it is just using that in the end) do not output the script tags themselves, but simply ensure that they will be included in the $scripts variable in page.tpl.php, which is then responsible to print that variables content. As you do not go through the page template, you get no script tags.
You could do a print drupal_get_js(); in your external file to output the scripts added via drupal_add_js() as a quick fix, but note that this will output all the default drupal js files as well, which might be more than you need (but might as well contain other scripts needed by fivestar, e.g. jquery). Alternatively, you'll have to create the needed script tags yourself.
As for hints on what to read, it is difficult to point you to something particular, but you might want to read up on the theming system in general.
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.