drupal

Drupal: Send file programmatically

Here is a handy, general function for Drupal 6 that takes as an argument a file id and returns the file for downloading.

function MYMODULE_send_file($fid) {

  $file = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $fid));
  if ($file) {    
    $my_path = $_SERVER['DOCUMENT_ROOT'] .'/'.$file->filepath;
       
    header("Pragma: public");
    header("Expires: 0");
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: pre-check=0, post-check=0, max-age=0', false);
    header('Last-Modified: '.gmdate('D, d M Y H  

Create an ubercart order programmatically and update address and products.

in

One of my latests tasks was to allow a link that would take a user id and a node id as an argument, and automatically create a new order that would have some fields pre populated and ready for further editing. Scope is to use the user id as the client that does the order and to automatically select the product depending on the taxonomy term of the provided node.

In my case I offer a series of links through a custom view template, that uses data from each row results to built the link.

Here is the code for my module, hopefully it will be useful for someone:


<?php

Remove duplicate entries from views results... the hard way.

In some cases it is seems impossible to get rid of the duplicate entries that views return in the output.
Not an elegant way, but this small module would do the trick...

So let's create a module the usual way, and put the following code in the .module file.
This one is called remove_duplicates.

 
<?php
function remove_duplicates_views_pre_render(&$view)
{
  $used_nids = array();

          foreach ($view->result as $row)
      {
        if (!in_array($row->nid, $used_nids))
        {
          $new_view_result[] = $row;
          $used_nids[] = $row->nid;

Drupal - Load user's Content Profile, given the UID

in

There are so many reasons to use Content Profile instead of the user's core profile. For once, profiles are created as separate nodes and therefore the full flexibility of CCK and VIews come in the game. This way it is possible to build some really sophisticated profiles, which is usually what is needed for any website.

I had to look around for an easy solution on how to load the user's content profile programmatically, so the following might come in helpful.

How to use datepicker pop up in a form field in a custom module?

Ever needed to use datepicker pop up in a custom module? That would be the most usable way to allow users to choose a date.

Here is the code to be used in the form function:


$format = 'd-m-y';
  $form['date_from'] = array(
    '#title' => t('Date From'),
    '#type' => 'date_popup',
    '#date_format' => $format,
    '#description' => t('Please select a date'),
  );

Drupal: How to add another recipient in personal user contact form

Drupal's core contact module comes with the feature that allows users to have their own personal contact forms.
We were asked to provide a way so that User 1 would receive a copy of any message exchanged between users via the forms.

Our first thought was to use Rules, but it seemed that creating an event for contact form submission was the long way to go.

The solution was quite simple: Using hook_mail_alter.

Reset drupal statistics - The Read counter

Here is a quick and easy way to reset the drupal statistics stored in the database with a couple of SQL queries.

You can reset the Total counters with:


UPDATE `node_counter` set `totalcount` = 0;

And the day counters with:


UPDATE `node_counter` set `daycount` = 0;

Important: You should keep a backup of the database before performing any actions.

Another suggestion is to use the Statistics Advanced Settings module that comes with this setting.

Drupal: Accordions in custom module - How to

If you'd like some fancy accordion on your module's output, here is a short recipe:

1.Download jquery_update.
Make sure that your jquery version is minimum 1.3.+ after the installation. This is required for the jquery_ui 1.7.x in step 2. An easy way to check the jquery version is to visit the drupal status report page under /admin/reports/status

2. Download jquery_ui.
Install it according to the instructions in the documentation.

Translate <Any> in Views Exposed Filter

While trying to translate the <Any> string in Views, locale returned the error "unsupported HTML".

A quick workaround is to switch from <Any> to -Any-; There is an option available under the Views Tools tab just for that.

The string "-Any-" is translatable, so from this point on it is easy to search for the string "-Any-" in locale search. If it is not found, you would probably need to check that you typed Any in the search form (case sensitive!).

Once found, it is easy to enter the required translation.

Custom submitted by in tpl files

 

Here is a line of code to override Drupal's submitted by elements in node.tpl.php file.

<span class="submitted">

<?php print format_date($node->created, 'custom', "F d, Y");  print ' | ' . theme('username', $node); print ' | ' . $node->comment_count . ' Comments'; ?>

</span>

 

Syndicate content