Drupal and Oracle Technical Articles

Here you can find some useful technical information about Drupal, Oracle, LAMP and other related technologies.

 

Connecting to Oracle from your PHP program

PHP cannot access an Oracle database by default. To enable PHP Oracle connectivity you must recompile PHP with the appropriate option. Alternatively you can download a PHP version already compiled for Oracle connectivity. We at i-chaumiere can install and configure PHP for you, contact us for a quote. Read more here: Installing Oracle, PHP, and Apache on Linux Installing Oracle, PHP, and Apache on Windows 2000/XP OCI8

Content disappears after upgrade

If you upgrade from Drupal 5 to Drupal 6 you may encounter the following problem.

For an unauthenticated user (user not logged into the site) the content is not showing. The user gets the message: Access denied. When this happens the first thing to do is to check your privileges for anonymous users in /admin/user/permissions. Make sure that "access content" is checked for anonymous users.

If you have done this and the user still cannot see the node then you may have encountered an upgrade bug where the record is lost from the node_access table in the database. You can fix this by logging directly into the database and executing the following sql command:

INSERT INTO node_access (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (0, 0, 'all', 1, 0, 0);

Optionally you may execute:

TRUNCATE node_access; INSERT INTO node_access (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (0, 0, 'all', 1, 0, 0);

which will drop all records from the table, so be careful with this one!

If you still cannot see the records try toggling "access content" in the admin/user/permissions page.

Drupal display problems on IE, limit to CSS files

If you have problems with your site displaying incorrectly on Microsoft's Internet Explorer (as usual) one of the first things to do is to check how many style sheets you have. Why? Because IE has a limit to the number of stlye sheets it loads, it will load only the first 31 style sheets.

The way around this problem is simple in Drupal, just enable the "Optimize CSS files" option. To do that log on as administrator, go to Administer, then the Performance page in the Site Congiguration admin group. Click the "Enable" radio button under Page compression.

The down side of this is that when you are developing you will have to clear the browser cache and refresh the page each time you change your CSS.

How to create gmap coordinates programmatically in Drupal

If you have gmap and gmap content modules installed you can create a cck field of type GMaps Address and Point. To insert coordinates programmaticaly you can use the code from the function below. Deselect the "Enable bounds" check box in the edit content type - edit field. You can enable this afterwards when you finish whatever you were doing with your program, like importing nodes. The values for the address field do not have to match the values of the coordinates.

function update_geo_loc($record){

    $id = (int) $record->nid;

    $node = node_load($id);

    //print_r($node);

      $lat =  40.714269;

    $lon =  -74.005973;

    // $node->field_geo_obsr[0]['value']->address->vid = $id;

    // $node->field_geo_obsr[0]['value']->address->nid = $id;

      $node->field_geo_obsr[0]['value']->address->field_name = 'field_geo_obsr';

    // $node->field_geo_obsr[0]['value']->address->delta = 0;

       $node->field_geo_obsr[0]['value']->address->country = 'FR';

       $node->field_geo_obsr[0]['value']->address->adminarea = 'Île-de-France';

       $node->field_geo_obsr[0]['value']->address->subadminarea = 'Paris';

       $node->field_geo_obsr[0]['value']->address->locality = 'Paris';

    //     $node->field_geo_obsr[0]['value']->address->deplocality = NULL;

    //  $node->field_geo_obsr[0]['value']->address->postalcode = NULL;

    // $node->field_geo_obsr[0]['value']->address->thoroughfare = NULL;

    //  $node->field_geo_obsr[0]['value']->address->privacy = 0;

    // $node->field_geo_obsr[0]['value']->address->search = 'Toulouse, France';

    // $node->field_geo_obsr[0]['value']->address->uid = 29;

 

    $node->field_geo_obsr[0]['value']->point->latitude = $lat;

    $node->field_geo_obsr[0]['value']->point->longitude = $lon;

    $node->field_geo_obsr[0]['value']->point->elevation = 0;

    $node->field_geo_obsr[0]['value']->point->map_type = 'earth';

    //  $node->field_geo_obsr[0]['value']->point->nid = $id;

    //  $node->field_geo_obsr[0]['value']->point->vid = $id;

    //  $node->field_geo_obsr[0]['value']->search = 'Toulouse, France';

    //  $node->field_geo_obsr[0][value]->point->bounds = array();

 

    node_save($node);

 

}

Insert CCK image in to the node programmatically - Drupal API

In Drupal if you have cck image field in your node it gets harder to create node programmaticaly. Here is how you can do it using Drupal API and node_save function. You need to create image in the image field table. The image should be already uploaded on the server somwhere under the files folder. Once you insert the image information in to the image table you will get file id back. Than just pass this file id in to the node object.

Here is how you can get file id (fid):

let say your image is here

<drupal home>/files/images/myimg.jpeg

your code will look something like this:

<?php

$image_path = '/home/drupal/site/files/images/myimg.jpeg';

 

$image_info = image_get_info($image_path);

$name_arr = explode('/', $image_path );

$name = end($name_arr);

$file = array(
                  'uid' => 1,
                  'filename' => $name,
                  'filepath' => $image_path,
                  'filemime' => $image_info['mime_type'],
                  'filesize' => $image_info['file_size'],
                  'status' => FILE_STATUS_PERMANENT,
                  'timestamp' => time(),
                );
         
drupal_write_record('files', $file);
$fid = $file['fid'];

?>

 

Once you got file id you can use it in the node object and cck field using node_save() function.

Here is the complete code with the bit to get file id put in to separate function:

$nid = 123;

$node = node_load($nid);

 // add image to database
$imagepath = '/home/mynema/images/image.123.jpg';        
$output .= write_image_info($imagepath, $fid);

// now add image to node
// this is the name of your image cck field
$node->field_oa_image[0]['fid'] = $fid;

node_save($node);

function write_image_info($image_path, &$fid){
    if (file_exists($image_path)){       
        $image_info = image_get_info($image_path);
     
        $name_arr = explode('/', $image_path );
        $name = end($name_arr);
         
        $file = array(
                  'uid' => 1,
                  'filename' => $name,
                  'filepath' => $image_path,
                  'filemime' => $image_info['mime_type'],
                  'filesize' => $image_info['file_size'],
                  'status' => FILE_STATUS_PERMANENT,
                  'timestamp' => time(),
                );
         
        drupal_write_record('files', $file);
        print_dsm($file);
        $fid = $file['fid'];
    }
    else{
        $output .= '<p>image not found at this path: '.$image_path.'</p>';
    }
    return $output;
}

 

Simple video from the outer web - including external video in a content type - Part I GOOGTUBE

Creating a video content type - how to include your YOUTUBE or GOOGLE video's in your Drupal CMS - step by step.

This guide assumes you are using CCK.

So the basic idea is this - you want a video content type, but you don't want to host the video (bandwith and filesize issues).  The CMS users don't what to embed code, but asking them to include a URL seems reasonable enough.

The Googtube module is a good choice here.  It recognises Google and Youtube URLs and puts the appropriate embed code in automatically.  This means that all you need is to create a content type with a field that asks for the appropriate URL, the user submits the URL - and hey presto, as if by magic, the finished content has the video and player.

Step 1

Download and expand the googtube module.

Copy your expanded 'googtube' folder to your drupal installation sites/all/modules directory.

Enable the module in administer>modules.

Now you need to configure your input format, you'll find it in administer>configuration>input formats, go to your selected format, click configure and check the 'googtube filter' box under filters.

Check the order of your filters: click on 'rearrange', you want to see that the googtube embed won't be affected by higher up filters.  Reorder if necessary.  After the HTML filter (not the HTML corrector) and before the URL filter is recommended.

Now your module is good to go - what next?

Step 2

Creating and setting up your content type.

Create a content type (using CCK) Administer>Content>Content Templates

Go to the 'Manage Fields' tab and add a new field: field type of 'text', with a 'text field' widget.  I usually set the display at about 80 for a text field, it doesn't matter unless you limit the size of the content.  Add a bit of help text along the lines of 'enter your video URL here, it should have the format of ... example'.

If you are displaying your content by adding it to the taxonomy term do not forget to add it in the taxonomy page:

Home › Administer › Content management › Taxonomy, click on edit taxonomy and check your content type.

Create a piece of content with a you tube link.

Go to the template and select the variable that has the embed code, it will be your content field and value - change check_plain to check_markup.

da da!

Now why don't you go style crazy and add some css.

 

 

Update Drupal node programmatically, add node to taxonomy term

How to update Drupal node  programmatically, from your PHP script using Drupal API

This example function will update node and add it to the desired taxonomy term.

function add_teasers_to_term($tid, $nid){

 $node = node_load($nid);
 
 // we need to check if node is in the taxonomy already, so build array of term ids for this node
 foreach($node->taxonomy as $key=>$value){
   $nodes_taxonomy[] =  $key;
 }
 
 print_r($nodes_taxonomy);
 // first check if node is already in there
 if (in_array($tid, $nodes_taxonomy)){
    return ' node already in '.$nid;
 }
 else {
   $node->taxonomy[] = $tid;
   node_save($node);
    return ' node saved '.$nid;
 }

 

creating drupal nodes programmatically with drupal_execute function

You can create Drupal node from your PHP code using drupal_execute() or node_save() functions. I prefere drupal_execute as it gives you validation errors. Here is an example that includes adding the node to the taxonomy term.

 

To find the form id of the form you wish to simulate go to the desired form, than do view source. Search for form_id. You will find a line similar to:

<input type="hidden" name="form_id" id="edit-article-teaser-node-form" value="article_teaser_node_form"  />

in this example the form id is in the value: article_teaser_node_form , and not in the id

 

function insert_node(){
    // prepare form values to be inserted
 
     $values['values']['title'] = $this->node->title;
     $values['values']['field_intro'][0]['value'] = $this->node->intro;
     $values['values']['field_body1'][0]['value']=  $this->node->text;
    
     //  taxomony term where this node will be attached to
     $tid = 32; // lets say your term id is 32, term ids are unique across all vocabularies
     $values['values']['taxonomy'] = Array(1 => Array($tid => $tid));
    
     $values['values']['op'] = t('Save');
     $node = array('type' => 'news_article'); // this is the type of your node
    
     //insert values using Drupal forms api
     if ($tid) {
       drupal_execute('news_article_node_form', $values, (object)$node); //news_article_node_form is the id of your form
       $errors = form_get_errors();
     }else {
       $errors['Custom error'] = '<p style= "color: red;"> Node: "'. $this->node->title .'" - Not inported. Taxonomy term "'. $this->node->category .'" is invalid</p>';
     }
   
     //print_r($errors);
     if ($errors){
             foreach($errors as $key=>$value){
               $output = '<p style= "color: red;"> Error:  - '.$key.',  - '.$value.'</p>';
             }
     }
     else{
         $output = null;
     }
    
    return $output;
    }

how to export content as xml from drupal using views

Nodes can be exported from a Drupal site using the Views module and some PHP code. Views theming is used to strip all HTML tags from the view so you end up with just content. Then this output is parsed through some PHP to get formatted xml content.

1. Create View with desired nodes.

Views are useful to select desired nodes. For example you may want to create an xml feed of all articles that belong to category "Open Source". So you create a view, name it Articles_OS_xml with following settings:- Provide page view, View type = list View, Use page = deselected Nodes per page = 0 Fields: Only add one field, Node id Filters Here you select desired filter, for example, Node type "is one of" story Taxonomy term = Open Source Save your view

Now run the view, you should get a list of all node ids for the desired nodes. Next you need to turn this into XML output

Create a theme for the view. This then should strip all the html tags from the output. This leaves a list of desired node id's

Get the desired data for each node using node->