Drupal - fetch body text of a node via URL? - php

I'm trying to create a function to retrieve and display just the Body field of a Drupal node, based on a given URL.
Currently, I've extended the functionality of the standard Drupal RSS to do a detection method. This will happen if you enter the following url:
http://mysite.com/rss.xml/page=54
That last part is critical - it tells what node ID to load from. As far as code goes, I've modified the node_feed() function in /modules/node/node.module to do the following:
if (stristr($nids, 'page=') !== FALSE) {
$nid = substr($nids, stripos($nids, 'page=') + 5);
$result = NULL;
if ((string)(int)$nid === $nid) {
$result = db_result(db_query("SELECT `nid` FROM {node} WHERE `nid` = %s AND `type` = 'flashnode'", $nid));
}
if ($result) {
$item = node_load($result);
print($item->body);
}
exit;
}
While this works, we're worried about it for two reasons:
1) Do the results cache? This is a very heavy-load page that will be called a lot.
2) Is there an "official" way to do this? One that uses modules with caching, instead of a semi-hacky workaround?
Thanks in advance for any guidance.
-Tom

You should definitely not be hacking apart the RSS feed in order to create a URL that returns a node's body. You should create a very simple custom module: http://drupal.org/developing/modules
The Drupal module system is lets you set up a url so that something like /fetch_body/1234 calls the fetch_body() function with $nid=1234 as the parameter. As for caching, you have a lot of options in your custom module. The most obvious would be to use cache_get() and cache_set() to do simple caching on your own based on the node ID.
What version of Drupal are you using? I would highly recommend the book Pro Drupal Development, just make sure to get the edition that matches your drupal version.

Related

Whats the correct way of outputting html in a custom module in Drupal

I have worked on a few project with drupal, and the main suggestion I got when learning was to use Views to output your data, although this was fine for some items I found it cumbersome to achieve my projects requirements in the set time and to style it the controls were too limiting.
I want to know whats wrong with doing it like this, or whats right about doing it like this?
You run a query to get the specific data you want.
function custom_block_get_item($nid){
$result = db_query("SELECT `field_custom_item` FROM {field_data_field_custom_item} WHERE entity_id = :entity_id",
array(
':entity_id' => $nid,
));
return $result;
}
Then you loop through creating the html
function custom_block_display_blurb($nid){
$html='<div id="blurb_wrapper"><div id="recent_projects_blurb">';
$result=custom_block_get_blurb($nid);
while($row1 = $result->fetchAssoc()){
$a=check_markup($row1['field_custom_item'],'full_html','','');
$html.='
<span class="recent_project_text">'.
$a.'</span>';
}
$html.='</div></div>';
return $html;
}
Finally display it at the end
function custom_block_block_view($delta=''){
if (arg(0) == 'node' && is_numeric(arg(1))) $nodeid = arg(1);
switch($delta) {
case 'custom_block':
$block['content'] = custom_block_display_blurb($nodeid);
break;
}
return $block;
}
This seems like a kind of hacky way to do things, what is the correct drupal way?
Maybe you mean using the drupal theme layer? This allows for use of .tpl files and overriding. I have a simple block module that does it here Source here.
Here is some documentation on using the theme layer within a module.
Here is some official Drupal docs on themeing (mainly themes rather than using the theme layer in a module).

Typo3 getting data and convert it to specific xml schema

i am new to typo3, so sorry, if this is too obvious.
I do not expect a complete solution, just the topics i would need to read about in order to solve the task would be perfectly enough. :)
Here the task:
I have a typo 3 installation with job advertisements in it. Now the company wants to publish that data in to a social website, which needs to have the job advertisement data put on a server in an xml feed, which looks like this: http://www.kununu.com/docs#jobfeed. Don't worry about what it says in there, it's just stuff like Job Title, Description etc.
Like i said, i am completely new to this and just have a vague idea.
My thoughts so far were something like this:
I probably need to write a plugin, which pulls the data out of typo3 by the push of a button
That Plugin need to establish a database connection to pull the data (probably it's mysql, but i am not entirely sure yet)
The data need to be formatted, which is either done by some string operations or by some kind of xml handler i assume.
Sidenote: I read something about TypoScript, but i'd like to avoid that, since it's a one time project and probably not worth the time to learn it. For me at least.
Thank you loads for your help in advance.
Cheers
you can handle that (basicly with typoscript). The other part has to come from PHP (F.e. extbase plugin) ... First part creates the XML output. Second part uses my Demo plugin to include data (Pages+special fields) from DB.
Within TS we are able to create a new typeNum. With that you can call your XML. ( ?type=1234 ) Within the BE you can select each page for output.
If you need help just contact me. I would send you the plugin.
sitemap = PAGE
sitemap {
typeNum = 1234
config {
# Set charset to utf-8
metaCharset = utf-8
# Deactivate TYPO3 Header Code
disableAllHeaderCode = 1
# Content-type settings
additionalHeaders = Content-type:text/xml;charset=utf-8
# Do not cache the page
no_cache = 1
# No xhtml cleaning trough TYPO3
xhtml_cleaning = 0
}
10 = USER_INT
10 {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
pluginName = Sitemap
extensionName = Srcxmlprovider
controller = Sitemap
vendorName = Sourcecrew
action = exportXml
switchableControllerActions {
Sitemap {
1 = exportXml
}
}
}
}
Ich habe die EXT noch schnell ins TER gepushed. Ein Tutorial liegt innerhalb.
http://typo3.org/extensions/repository/view/srcxmlprovider

How can I get an id from a simplepie post which can be used to look it up later?

I've recently started developing a portfolio website which I would like to link to my wordpress blog using simplepie. It's been quite a smooth process so far - loading names and descriptions of posts, and linking them to the full post was quite easy. However, I would like the option to render the posts in my own website as well. Getting the full content of a given post is simple, but what I would like to do is provide a list of recent posts which link to a php page on my portfolio website that takes a GET variable of some sort to identify the post, so that I can render the full content there.
That's where I've run into problems - there doesn't seem to be any way to look up a post according to a specific id or name or similar. Is there any way I can pull some unique identifier from a post object on one page, then pass the identifier to another page and look up the specific post there? If that's impossible, is there any way for me to simply pass the entire post object, or temporarily store it somewhere so it can be used by the other page?
Thank you for your time.
I stumbled across your question looking for something else about simplepie. But I do work with an identifier while using simplepie. So this seems to be the answer to your question:
My getFeedPosts-function in PHP looks like this:
public function getFeedPosts($numberPosts = null) {
$feed = new SimplePie(); // default options
$feed->set_feed_url('http://yourname.blogspot.com'); // Set the feed
$feed->enable_cache(true); /* Enable caching */
$feed->set_cache_duration(1800); /* seconds to cache the feed */
$feed->init(); // Run SimplePie.
$feed->handle_content_type();
$allFeeds = array();
$number = $numberPosts>0 ? $numberPosts : 0;
foreach ($feed->get_items(0, $number) as $item) {
$singleFeed = array(
'author'=>$item->get_author(),
'categories'=>$item->get_categories(),
'copyright'=>$item->get_copyright(),
'content'=>$item->get_content(),
'date'=>$item->get_date("d.m.Y H:i"),
'description'=>$item->get_description(),
'id'=>$item->get_id(),
'latitude'=>$item->get_latitude(),
'longitude'=>$item->get_longitude(),
'permalink'=>$item->get_permalink(),
'title'=>$item->get_title()
);
array_push($allFeeds, $singleFeed);
}
$feed = null;
return json_encode($allFeeds);
}
As you can see, I build a associative array and return it as JSON what makes it really easy using jQuery and ajax (in my case) on the client side.
The 'id' is a unique identifier of every post in my blog. So this is the key to identify the same post also in another function/on another page. You just have to iterate the posts and compare this id. As far as I can see, there is no get_item($ID)-function. There is an get_item($key)-function but it is also just taking out a specific post from the list of all posts by the array-position (which is nearly the same way I suggest).

How can I create a RSS feed with a limit statement in wordpress

I am basically creating an iphone app that get's it's data from wordpress. Wordpress will serve audio and video links via a RSS feed to the iphone app. I have the feed and audio player working great but can't seem to find anything related to how to create a custom feed where I can specify pagination like start=0&items=10. A plugin would be great but I can code something up in PHP if anyone has any ideas.
I'm going to answer this question by changing the standard RSS feed of a WordPress installation to respond to limits passed by query parameters. As you say you've already got a working feed, this should hopefully give you everything else you need.
By default, the standard feeds in WordPress are limited by the setting "Syndication feeds show the most recent X items" on the Settings→Reading page, and are unpaginated, as that wouldn't generally make sense for an RSS feed. This is controlled by WordPress's WP_Query::get_posts() method, in query.php, if you're interested in taking a look at how things work internally.
However, although the feed query's limit is set to LIMIT 0, X (where X is the above setting, 10 by default) , you can override the limit by filtering the query in the right place.
For example, the filter post_limits will filter the LIMIT clause of the query between the point it's set up by the default code for feeds and the time it's run. So, the following code in a plugin -- or even in your theme's functions.php -- will completely unlimit the items returned in your RSS feeds:
function custom_rss_limits($limits) {
if (is_feed()) {
// If this is a feed, drop the LIMIT clause completely
return "";
} else {
// It's not a feed; leave the normal LIMIT in place.
return $limits;
}
}
add_filter('post_limits', 'custom_rss_limits');
(At this point I should mention the obvious security implications -- if you've got 20,000 posts on your blog, you'll cause yourself a lot of server load and bandwidth if if lots of people start grabbing your feed, and you send out all 20,000 items to everyone. Therefore, bear in mind that whatever you end up doing, you may still want to enforce some hard limits, in case someone figures out your feed endpoint can be asked for everything, say by analysing traffic from your iPhone app.)
Now all we've got to do is to respond to query parameters. First of all, we register your two query parameters with WordPress:
function rss_limit_queryvars( $qv ) {
$qv[] = 'start';
$qv[] = 'items';
return $qv;
}
add_filter('query_vars', 'rss_limit_queryvars' );
That allows us to pass in the start and items variables you're suggesting for your URL parameters.
All we have to do then is to adjust our original LIMIT changing function to respond to them:
function custom_rss_limits($limits) {
if (is_feed()) {
global $wp_query;
if (isset($wp_query->query_vars['start']) &&
isset($wp_query->query_vars['items'])) {
// We're a feed, and we got pagination parameters. Override our
// standard limit.
// First convert to ints in case anyone's put something hinky
// in the query string.
$start = intval($wp_query->query_vars['start']);
$items = intval($wp_query->query_vars['items']);
$limits = "LIMIT $start, $items";
} else {
// We weren't passed pagination parameters, so just
// leave the default limits alone.
}
}
return $limits;
}
add_filter('post_limits', 'custom_rss_limits');
And there you go. Throw those last two blocks of code at WordPress, and you can now use a URL like this on any of your existing feeds:
http://example.com/feed/?start=30&items=25
For this example, you'll get the normal RSS feed, but with 25 items starting from item number 30.
...and if you don't pass the query parameters, everything will work like normal.

How do I create a node from a cron job in drupal?

In a custom module for drupal 4.7 I hacked together a node object and passed it to node_save($node) to create nodes. This hack appears to no longer work in drupal 6. While I'm sure this hack could be fixed I'm curious if there is a standard solution to create nodes without a form. In this case the data is pulled in from a custom feed on another website.
The best practices method of making this happen is to utilize drupal_execute. drupal_execute will run standard validation and basic node operations so that things behave the way the system expects. drupal_execute has its quirks and is slightly less intuitive than simply a node_save, but, in Drupal 6, you can utilize drupal_execute in the following fashion.
$form_id = 'xxxx_node_form'; // where xxxx is the node type
$form_state = array();
$form_state['values']['type'] = 'xxxx'; // same as above
$form_state['values']['title'] = 'My Node Title';
// ... repeat for all fields that you need to save
// this is required to get node form submits to work correctly
$form_state['submit_handlers'] = array('node_form_submit');
$node = new stdClass();
// I don't believe anything is required here, though
// fields did seem to be required in D5
drupal_execute($form_id, $form_state, $node);
node_save() still works fine in Drupal 6; you'll need a couple of specific pieces of data in place to make it work.
$node = new stdClass();
$node->type = 'story';
$node->title = 'This is a title';
$node->body = 'This is the body.';
$node->teaser = 'This is the teaser.';
$node->uid = 1;
$node->status = 1;
$node->promote = 1;
node_save($node);
'Status' and 'Promote' are easy to overlook -- if you don't set those, the node will remain unpublished and unpromoted, and you'll only see if you go to the content administration screen.
I don't know of a standard API for creating a node pragmatically. But this is what I've gleaned from building a module that does what you're trying to do.
Make sure the important fields are set: uid, name, type, language, title, body, filter (see node_add() and node_form())
Pass the node through node_object_prepare() so other modules can add to the $node object.
One more answer I discovered was to use the example from the blogapi module in drupal core. The fact that it is in core gives me a bit more confidence that it will continue to work in future versions.
There are some good answers above, but in the specific example of turning an ingested feed item into a node, you could also take the approach of using the simplefeed module (http://wwww.drupal.org/project/simplefeed). This module uses the simplepie engine to ingest feeds and turns individual items from each feed into nodes. I realize that this doesn't specifically address the issue of creating nodes from cron, but it might be an easier solution to your problem overall.

Categories