Drupal: Inserting fivestar widget in an external php file - php

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.

Related

Wordpress custom query

I have a wordpress blog. I created a db table which stores dictionary information and I want to publish this data from a URL . (For ex: "myblogaddress.com/mytest.php")
I have been researching for 2 days but nothing works I tried.
In my page; I use the php code shown in blow.
<?php
global $wpdb;
$words = $wpdb->get_results("SELECT * FROM $wpdb->words")
echo $words[0]->ENG;
?>
I wonder that;
- Which directory does my php page to be into ?
- What I need to do (other config, permission etc.) to do what I want.
Regards.
If you're loading it from a standalone PHP file (ie not from within your WordPress theme), you'll have to call wp-load.php to initialise the WordPress variables (including $wpdb). Have a look at this answer, including the comment about only needing wp-load.php.
I'd consider using a relative path (what that would be would depend on where you put your page relative to WordPress) rather than using $_SERVER['DOCUMENT_ROOT'];, but that's just a personal preference.
EDIT
Rereading after seeing your comment, I've just realised $wpdb->words probably won't exist. Try
$words = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "words")
instead. That'll generate the table name correctly as wp_words. Of course, you'll need to populate it the same way.

How to add the script to the top of script list in Joomla 2.5

I use $doc->addScript in top of template file to add my script (and jquery) in a head, but components are added their scripts before.
Can I control this output queue?
I'm writing this for Joomla 3.3.x
Unfortunatly this cannot be done without either hacking core-files (don't do that!!) or use an own implementation for javascript-adding.
If you look into [joomlaroot]\libraries\joomla\document\document.php -> line 446, where the function addScript is defined, you will see, that the _scripts-Array in JDocument is an assoc. Array where the key is the url to the script.
If you don't want to rewrite the joomla-methods that add a script and every installed extension in your system, you should add your javascripts in the template after < jdoc:include type="head" >.
You could write a system-plugin, that uses the event "onBeforeCompileHead" to reorder the document-scripts, but i think that would be too much.
I don't know, why joomla does not simply use a sort-attribute in addScript(), that would be so needed. Also Script-Dependencies could be realised. Even Wordpress can do that.
EDIT: What you COULD do, is use JDocument::addCustomTag() to add your scripts AFTER the joomla-scripts. Because the custom-tags are rendered after all the scripts. Although this does not affect the order of scripts already loaded by other components.
You don't need to use a foreach loop for that.
For Joomla 2.5: In your templates index.php add the following if you want to prepend a script, example shows the inclusion of jquery:
$headData = $this->getHeadData();
reset($headData['scripts']);
$newHeadData = $headData['scripts'];
$jquery = array('http://code.jquery.com/jquery-1.9.1.min.js' => array('mime' => 'text/javascript', 'defer' => FALSE, 'async' => FALSE));
$newHeadData = $jquery + $newHeadData;
//$some_other_script = array("path-to-your-script" => array('mime' => 'text/javascript', 'defer' => FALSE, 'async' => FALSE));
//$newHeadData = $some_other_script + $newHeadData;
$headData['scripts'] = $newHeadData;
$this->setHeadData($headData);
For Joomla 3.3: This might work in the same way as for Joomla 2.5 as the functions getHeadData() and setHeadData() are still present. See the API documentation: http://api.joomla.org/cms-3/classes/JDocumentHTML.html
You'll need to modify your template's index.php file. The following includes Joomla's head section:
<jdoc:include type="head"/>
Simply move $doc->addScript after this (or before it) to set which loads first. Files load top to bottom, so whatever is on top will load first.
This can be done, but it's a bit tricky.The following code is based on https://gist.github.com/AmyStephen/5971447
$head = JFactory::getDocument()->getHeadData();
$scripts = $head['scripts'];
$newScripts = array();
foreach ($scripts as $key => $value)
{
if (/** test to reorder the scripts */)
{
$newScripts[$key] = $value;
}
}
$head['scripts'] = $newScripts;
JFactory::getDocument()->setHeadData($head);
In other words, the scripts are stored as an array and you need to reorder it.

Typo3 +TV not rendering content elements

I have installed typo3, templavoila and mapped a template.
Everything works fine, except my content elements. They just don't appear. They did before I installed templavoila and mapped a template.
Also, when using
10 = RECORDS
10 {
tables = tt_content
source = 9
}
it does not give me any output.
even nothing with:
10 = RECORDS
10 {
tables = tt_content
source = 9
conf.tt_content = TEXT
conf.tt_content.value = TEST
}
Does anyone have a clue as to what I might be doing wrong?
You must include the css styled content static template in your TS template.
In your ts page object you need to assign it to the templavoila object.
# Default PAGE object:
page = PAGE
page.typeNum = 0
page.10 = USER
page.10.userFunc = tx_templavoila_pi1->main_page
Probably the page, where tt_content is situated is not visible to regular visitor ?
In this case, following snippet will help.
10 = RECORDS
10.tables = tt_content
10.source = 9
10.dontCheckPid = 1
Finally we got it to work. Don't know what I did wrong, but I guess I learned not to do it again.
I'll put the "solution" here since someone might find it helpful and lose more than a day over this like I did.
Solution:
copy an existing content element in list mode, pasted it on a page via page mode
This was to test if that would do anything. Guess what, everything worked again. Not only the newly copied element but also ALL other test elements created via different ways on different pages and storage folders.
Thank you all for helping and thinking along.

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.)

Categories