Drupal best open source PHP CMS 2009

12 novembre 2009

Drupal riVince l’Open Source CMS Award 2009 nella categoria Best Open Source PHP CMS rilasciato sotto licenza GNU/GPL o equivalente.
Secondi a parimerito Joomla e Worpress.
Tra i milgiori CMS non in PHP invece vince Plone.

La casa Bianca usa Drupal

26 ottobre 2009

La Casa Bianca modifica il proprio sito appoggiandolo su Drupal. Esteriormente il sito non è cambiato, ma alle spalle ora c’è il mio CMS preferito.
Complimenti a loro.

via Ossblog

Rilasciato plugin ilmeteo.it per wordpress

21 settembre 2009

E’ stato rilasciato da ilmeteo.it un plugin per wordpress per le previsioni meteo.
A questo indirizzo http://imeteo.wordpress.com/ è possibile scaricarlo e vedere le istruzioni per installarlo.

Modulo Drupal per nuovo tipo di contenuto

25 giugno 2009

In Drupal è molto semplice sviluppare un modulo che permeta di avere a disposizione un nuovo tipo di contenuto che estenda il classico nodo.

Vediamo come sviluppare un modulo che aggiunga il tipo di contenuto libro.

Creiamo la cartella node_book che andremo a mettere nella cartella modules del nostro sito.
Dentro questa cartella mettiamo il file node_book.info con le specifiche del nodo:

; $Id$
name = node book
description = "nodo libro"
core = 6.x

e il file node_book.module con il codice vero e proprio.

Ecco i vari hook che si devono implementare:
Hook menu

function node_book_menu()
{
$items[] = array( 'path' => 'node_book', 'callback' => 'node_book_page', 'access' => true, 'type' => MENU_CALLBACK );
return $items;
}

hook perm

function node_book_perm()
{
return array('create book node', 'edit own book nodes');
}

hook access
function node_book_access($op, $node, $account) {
if ($op == 'create') {
// Only users with permission to do so may create this node type.
return user_access('create nameofnodetype', $account);
}
// Users who create a node may edit or delete it later, assuming they have the
// necessary permissions.
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own nameofnodetype', $account) && ($account->uid == $node->uid)) {
return TRUE;
}
}
}

hook help
function node_book_help($path, $arg) {
switch ($path) {
case 'admin/help#block':
return '

'. t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') .'

';

case 'admin/build/block':
return t('

Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually.

If you want certain blocks to disable themselves temporarily during high server loads, check the "Throttle" box. You can configure the auto-throttle on the throttle configuration page after having enabled the throttle module.

You can configure the behaviour of each block (for example, specifying on which pages and for what users it will appear) by clicking the "configure" link for each block.

', array('@throttle' => url('admin/settings/throttle')));
}
}

Ora passiamo agli hook che implementano veramente il nostro modulo:
hook form

function node_book_form(&$node) {
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
}
if ($type->has_body) {
// In Drupal 6, we can use node_body_field() to get the body and filter
// elements. This replaces the old textarea + filter_form() method of
// setting this up. It will also ensure the teaser splitter gets set up
// properly.
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
}
// NOTE in node_example there is some addition code here not needed for this simple node-type
// Now we define the form elements specific to our node type.
$form['editor'] = array(
'#type' => 'textfield',
'#title' => t('Editore'),
'#default_value' => isset($node->editor) ? $node->editor : '',
);
$form['book_author'] = array(
'#type' => 'textfield',
'#title' => t('Autore'),
'#default_value' => isset($node->book_author) ? $node->book_author : '',
);
$form['place'] = array(
'#type' => 'textfield',
'#title' => t('Luogo'),
'#default_value' => isset($node->place) ? $node->place : '',
);
$form['year'] = array(
'#type' => 'textfield',
'#title' => t('Anno'),
'#default_value' => isset($node->year) ? $node->year : '',
);
$form['book_type'] = array(
'#type' => 'textfield',
'#title' => t('Tipo'),
'#default_value' => isset($node->book_type) ? $node->book_type : '',
);
$form['magazine'] = array(
'#type' => 'textfield',
'#title' => t('Rivista'),
'#default_value' => isset($node->magazine) ? $node->magazine : '',
);
return $form;
}

hook insert
/**
* Implementation of hook_insert().
*
* As a new node is being inserted into the database, we need to do our own
* database inserts.
*/
function node_book_insert($node) {
db_query("INSERT INTO {node_book} (vid, nid, editor, book_author, place, year, book_type, magazine) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s')", $node->vid, $node->nid,$node->editor,$node->book_author,$node->place,$node->year,$node->book_type,$node->magazine);
}

hook update
/**
* Implementation of hook_update().
*
* As an existing node is being updated in the database, we need to do our own
* database updates.
*/
function node_book_update($node) {
// if this is a new node or we're adding a new revision,
if ($node->revision) {
node_book_insert($node);
}
else {
db_query("UPDATE {node_book} SET editor='%s', book_author='%s', place='%s', year='%s', book_type='%s', magazine='%s' WHERE vid = %d", $node->editor, $node->book_author, $node->place, $node->year, $node->book_type, $node->magazine, $node->vid);
}
}

hook nodeapi
/**
* Implementation of hook_nodeapi().
*
* When a node revision is deleted, we need to remove the corresponding record
* from our table. The only way to handle revision deletion is by implementing
* hook_nodeapi().
*/
function node_book_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'delete revision':
// Notice that we're matching a single revision based on the node's vid.
db_query('DELETE FROM {node_book} WHERE vid = %d', $node->vid);
break;
}
}

hook_delete
/**
* Implementation of hook_delete().
*
* When a node is deleted, we need to remove all related records from out table.
*/
function node_book_delete($node) {
// Notice that we’re matching all revision, by using the node’s nid.
db_query(’DELETE FROM {node_book} WHERE nid = %d’, $node->nid);
}

hook load
/**
* Implementation of hook_load().
*
* Now that we've defined how to manage the node data in the database, we
* need to tell Drupal how to get the node back out. This hook is called
* every time a node is loaded, and allows us to do some loading of our own.
*/
function node_book_load($node) {
$additions = db_fetch_object(db_query('SELECT editor,book_author,place,year,book_type,magazine FROM {node_book} WHERE vid = %d', $node->vid));
return $additions;
}

hook view
/**
* Implementation of hook_view().
*
* This is a typical implementation that simply runs the node text through
* the output filters.
*/
function node_book_view($node, $teaser = FALSE, $page = FALSE) {
$node = node_prepare($node, $page);
$node->content['myfield'] = array(
'#value' => 'Editore: '.$node->editor.'<br />'.'Autore: '.$node->book_author.'<br >'.'Luogo: '.$node->place.'<br >'.'Anno: '.$node->year.'<br >'.'Tipologia: '.$node->book_type.'<br >',
'#weight' => 1,
);
return $node;
}

Ora manca solo il DB, ecco la tabella per i campi in più del nostro nodo
CREATE TABLE node_book (
vid int(10) unsigned NOT NULL default ‘0′,
nid int(10) unsigned NOT NULL default ‘0′,
editor varchar(255) NOT NULL default ”,
book_author varchar(255) NOT NULL default ”,
place varchar(255),
year varchar(255),
book_type varchar(255),
magazine varchar(255),
PRIMARY KEY (vid, nid),
KEY `node_type_book_nid` (nid)
)

Dopo aver creato la tabella e attivato il modulo avrete un nuovo tipo di contenuto disponibile.

Scrivere Modulo Drupal

1 giugno 2009

Ultimamente mi sono concentrato molto su Drupal, soprattutto per la sua flessibilità e modularità.
La possibilità di scrivere un modulo, che aggiunge funzioni, senza andare a toccare il core del resto del cms è veramente molto utile e produttivo.

Ecco un esempio di primo modulo che chiameremo nodelist:
Creiamo un file nodelist.info che contiene le informazioni del modulo.

; $Id: nodelist.info Exp $
name = Nodelist
description = visualizza list nodi
package = node
version = VERSION
core = 6.x

; Information added by drupal.org packaging script on 2008-08-14
version = "6.4"
project = "drupal"

Creare il file nodelist.module in cui scriveremo il modulo vero e proprio.

Per fare ciò utilizziamo i famosi Hook di Drupal.
Le varie function per convenzione si chiameranno nodelist_[hook].


/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function nodelist_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#nodelist":
$output = '<p>'.t("Displays links to nodes ") .'</p>';
break;
}
return $output;
}
/**
* Valid permissions for this module
* @return array An array of valid permissions for the mailing module
*/
function nodelist_perm() {
return array('access nodelist content');
} // function nodelist_perm()
/**
*
* @return
*/
function nodelist_block($op='list', $delta=0) {
// listing of blocks, such as on the admin/block page
if ($op == "list") {
$block[0]["info"] = t("node List");
return $block;
} else if ($op == 'view') {
// our block content
// content variable that will be returned for display
$block_content = '';
$result = db_query("SELECT nid, title, created FROM {node} ");
while ($links = db_fetch_object($result)) {
$block_content .= l($links->title, 'node/'.$links->nid) . '
';
}
// check to see if there was any content before setting up the block
if ($block_content == '') {
// no content from a week ago, return nothing.
return;
}
// set up the block
$block['subject'] = 'node';
$block['content'] = $block_content;
return $block;
}
}

Con questi file dentro la cartella nodelist e caricata nella cartella modules vi troverete la possibilità di abilitare il nuovo modulo.
Abilitandolo avrete il nuovo blocco a disposizione.