how to add node in through Drupal API - drupal

In order to insert an article to Drupal there are thee ways of doing that:
by admin panel - really slow and not feasable if talking about 400 articles
by pure sql - number of tables that have to maintained and calculated (content, core, cat etc.) is quite high and it's not really reliable
by using drupal API - that's something that I was trying to implement but can't find a good documentation on it. What I'm trying to achieve is to use drupal classes and insert content (ie. running a PHP file with (catid,title,introtext....))
Example: what i want to add node in xyz.com/drupal site but my php code should be run in irankmedi.com
Can you please point me into direction where I can find some info on how to manage articles and categories this way?
Thanks in advance

If your request is very specific and you can't find a module that does what you need it shouldn't be too difficult to make a module on your own and import (create drupal) content from your code. Should be something like this:
Create a php file that will do the job.
At start of your script include standard Drupal's bootstrap code so you'll have all Drupal's functionality available in your script
Make code that will read content (from database or feed or something else).
Use Drupal api to insert node programatically:
https://www.drupal.org/node/1388922
Call your script manually or set cron to call it on specific time periods.

First you have to read RSS feed in drupal custom module. Then, following code can create node programatically.
$node = new stdClass();
$node->type = 'blog'; // This type should exist
node_object_prepare($node); // Sets some defaults.
$node->language = LANGUAGE_NONE; // The language of the default
$node->title = "testnode";
$node->body[LANGUAGE_NONE][0]['value'] = "Body";
node_save($node); // function that actually saves node in database

Related

Display data from taxonomy using Ektron CMS with ASP.Net

I am trying display the content from taxonomy by using Ektron CMS with ASP .Net
By using the taxonomy path i got the id and trying to display the content.
But i am getting content as null.
Please let me know the possible solutions to solve this.
Waiting for experts answers.
Thanks,
In my development environment, I have the following taxonomy:
const string eventsTaxonomyPath = "\\Upcoming Events";
const long eventsTaxonomyId = 89;
It sounds like you already found this method (or something like it) in what I like to call the "Legacy API":
var taxonomyApi = new Ektron.Cms.API.Content.Taxonomy();
var taxonomyId = taxonomyApi.GetTaxonomyIdByPath(eventsTaxonomyPath);
Without any info on what version you're on, I'll assume it's a recent (8.5+) version. The Framework API makes it really easy to get the content from a given taxonomy. Below are a couple of ways that work on v9.0 and will most likely work in anything 8.5+ -- in the developer briefing webcast the only major change for the Framework API in v9 was the inclusion of the e-commerce namespace.
Getting the full taxonomy tree via the TaxonomyManager:
var taxonomyItemManager = new Ektron.Cms.Framework.Organization.TaxonomyManager();
var taxData = taxonomyItemManager.GetTree(eventsTaxonomyId, includeItems: true);
Getting all the content recursively from a given taxonomy folder via the ContentManager:
var contentManager = new Ektron.Cms.Framework.Content.ContentManager();
var criteria = new ContentTaxonomyCriteria();
criteria.AddFilter(eventsTaxonomyPath, true);
criteria.ReturnMetadata = true;
var content = contentManager.GetList(criteria);
The potential downside to the ContentManager way is that you lose the hierarchical taxonomy structure. The upside to using the ContentManager is that you can tell it to include all the metadata for each content block. That's not possible with the TaxonomyManager or TaxonomyItemManager.
My guess is that the "Get Content By Taxonomy" function you are using by default does not fetch the content. You can either-
a) Use the ID to get the content via the content manager API
b) Investigate if the function you are using has an override to include content.

Drupal - Get image from url and import it into node

Im writing a module for drupal, Im trying to create a node from my module, everything is fine , I only have 1 problem with creating an image , The image exist on different server, so I want to grab the page and insert it , I install module http://drupal.org/project/filefield_sources , which has remote option , I search in the module code , I could not find the function that he used for this process, module work very nice from interface , but how i make it do the job from code ? which function should i call and what parameter should i pass .
I'm over Drupal 6.
Hopefully you're using Drupal 7...
The system_retrieve_file() function will download a file from a remote source, copy it from temp to a specified destination and optionally save it to the file_managed table if you want it to be managed.
$managed = TRUE; // Whether or not to create a Drupal file record
$path = system_retrieve_file($url, 'public://my_files/', $managed);
If you want to get the file object immediately after you've done this, the following is the only way I've found so far:
$file = file_load(db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField());
get fid using $path->fid. no need to mysql

Using specific drupal-related functions in a non-drupal.php file INSIDE the drupal dir

Good morning all.
I'm having some issues while trying to make the function "field_file_load" work in a php script I've done to process an AJAX call.
I've read about bootstrapping drupal core elements inside, but it doesn't seem to work.
So far I've succesfully populated a Select Box using the data from another Select Box, making an AJAX call to this php file (which is in the drupal directory folder, in a theme to be precise)
<?php
$var = $_GET['q'];
$con = mysql_connect('*******', '******', '********');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("drupal", $con);
$sql="SELECT DISTINCT xc.field_brand_value FROM node
INNER JOIN term_node AS tn ON node.vid = tn.vid
LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid
WHERE tn.tid IN (SELECT th.tid FROM term_hierarchy AS th WHERE th.parent = '149')
AND xc.field_location_value = '".$var."'";
$result = mysql_query($sql);
echo(' <select name="brand" id="brand">
<option value="default" selected>Select a brand</option>
');
while($row = mysql_fetch_array($result))
{
echo('<option value="'.$row['field_brand_value'].'">'.$row['field_brand_value'].'</option>');
}
echo('</select>');
mysql_close($con);
?>
And this is working like a charm because all I have to do is connecting to the drupal db and fetch the desired values.
The problem arises when I want to fetch the url of some pictures (with a query that uses values from the first and second dropdown) and use the "file_field_load" to load the url of the given picture.
I get (obviously) a "call to undefined function" error.
So I tried bootstrapping drupal.
But it doesn't work anyway.
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Since I don't have full access to the server where the site is hosted, assuming that drupal is convenientrly installed in the root, how can I figure out the path to drupal site htdocs ?
Moreover, does calling a full bootstrap (instead of just the needed part) can cause some problems?
So, to be brief:
1] how can I call a drupal function (in this case which comes from the filefiled module) in a non-drupal php script which resides however in the drupal directory?
2] Which is the correct way of bootstrapping?
3] Do I need to connect to the db (like in the previous working example) IN ADDITION to bootstrapping?
Or, finally. there's a different, speedier way you know how to do what I need to do?
Thanks in advance for any reply.
Hmm that's weird. If the FileField module is enabled, the function should be available. So maybe FileField is not actually enabled?
If that's the case you're gonna have to manually add the file that contains the function definition, which is the field_file.inc file in the module's directory, so you'd add that dependency to your bootstrapping code:
<?php
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('filefield', 'inc', 'field_file');
AFAIK what you're doing for bootstrapping Drupal from an outside script is the "correct" way.
Now, I'm not sure if, on a big picture level, whatever you're trying to do is a good idea at all... That is: You're making a little nonDrupal script which:
manually connects to the Drupal database with plain mysql functions instead of Drupal's DB API functions, in order to
fetch CCK information using a query that's 100% vulnerable to SQL injection, and
all of this put in a theme directory no less!
So you might want to rethink your angle of attack here, you know?. Maybe making a custom module for this.
But if you just have to do things this way (for reasons I can't think of), then at least use db_query so you don't have to do the whole mysql_connect() stuff, and do something like
<?php
db_query("YOUR BIG QUERY HERE... xc.field_location_value = '%s'", $var);
...for at least some degree of security.
I would also recommend that you browse the involved modules a bit (FileField, etc) to see if they have APIs (or at least some internal functions) that might return what you're trying to get through plain DB querying.

How to work with hook_nodeapi after image thumbnail creation with ImageCache

A bit of a followup from a previous question.
As I mentioned in that question, my overall goal is to call a Ruby script after ImageCache does its magic with generating thumbnails and whatnot.
Sebi's suggestion from this question involved using hook_nodeapi.
Sadly, my Drupal knowledge of creating modules and/or hacking into existing modules is pretty limited.
So, for this question:
Should I create my own module or attempt to modify the ImageCache module?
How do I go about getting the generated thumbnail path (from ImageCache) to pass into my Ruby script?
edit
I found this question searching through SO...
Is it possible to do something similar in the _imagecache_cache function that would do what I want?
ie
function _imagecache_cache($presetname, $path) {
...
...
// check if deriv exists... (file was created between apaches request handler and reaching this code)
// otherwise try to create the derivative.
if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
imagecache_transfer($dst);
// call ruby script here
call('MY RUBY SCRIPT');
}
Don't hack into imagecache, remember every time you hack core/contrib modules god kills a kitten ;)
You should create a module that invokes hook_nodeapi, look at the api documentation to find the correct entry point for your script, nodeapi works on various different levels of the node process so you have to pick the correct one for you (it should become clear when you check the link out) http://api.drupal.org/api/function/hook_nodeapi
You won't be able to call the function you've shown because it is private so you'll have to find another route.
You could try and build the path up manually, you should be able to pull out the filename of the uploaded file and then append it to the directory structure, ugly but it should work. e.g.
If the uploaded file is called test123.jpg then it should be in /files/imagecache/thumbnails/test123/jpg (or something similar).
Hope it helps.

Drupal - Getting node id from view to customise link in block

How can I build a block in Drupal which is able to show the node ID of the view page the block is currently sitting on?
I'm using views to build a large chunk of my site, but I need to be able to make "intelligent" blocks in PHP mode which will have dynamic content depending on what the view is displaying.
How can I find the $nid which a view is currently displaying?
Here is a more-robust way of getting the node ID:
<?php
// Check that the current URL is for a specific node:
if(arg(0) == 'node' && is_numeric(arg(1))) {
return arg(1); // Return the NID
}
else { // Whatever it is we're looking at, it's not a node
return NULL; // Return an invalid NID
}
?>
This method works even if you have a custom path for your node with the path and/or pathauto modules.
Just for reference, if you don't turn on the path module, the default URLs that Drupal generates are called "system paths" in the documentation. If you do turn on the path module, you are able to set custom paths which are called "aliases" in the documentation.
Since I always have the path module turned on, one thing that confused me at first was whether it was ever possible for the arg function to return part of an alias rather than part of system path.
As it turns out, the arg function will always return a system path because the arg function is based on $_GET['q']... After a bit of research it seems that $_GET['q'] will always return a system path.
If you want to get the path from the actual page request, you need to use $_REQUEST['q']. If the path module is enabled, $_REQUEST['q'] may return either an alias or a system path.
For a solution, especially one that involves a view argument in the midst of a path like department/%/list, see the blog post Node ID as View Argument from SEO-friendly URL Path.
In the end this snippet did the job - it just stripped the clean URL and reported back the very last argument.
<?php
$refer= $_SERVER ['REQUEST_URI'];
$nid = explode("/", $refer);
$nid = $nid[3];
?>
Given the comment reply, the above was probably reduced to this, using the Drupal arg() function to get a part of the request path:
<?php
$nid = arg(3);
?>
You should considder the panels module. It is a very big module and requires some work before you really can tap into it's potential. So take that into considderation.
You can use it to setup a page containing several views/blocks that can be placed in different regions. It uses a concept called context which can be anything related to what you are viewing. You can use that context to determine which node is being viewed and not only change blocks but also layout. It is also a bit more clean since you can move the PHP code away from admin interface.
On a side note, it's also written by the views author.
There are a couple of ways to go about this:
You can make your blocks with Views and pass the nid in through an argument.
You can manually pass in the nid by accessing the $view object using the code below. It's an array at $view->result. Each row in the view is an object in that array, and the nid is in that object for each one. So you could run a foreach on that and get all of the nid of all rows in the view pretty easily.
The first option is a lot easier, so if that suits your needs I would go with that.
New about Drupal 7: The correct way to get the node id is using the function menu_get_object();
Example:
$node = menu_get_object();
$contentType = node_type_get_name($node);
Drupal 8 has another method. Check this out:
arg() is deprecated

Resources