Wednesday, March 12, 2008

Programmatically insert cck field

Ok, after couple hrs of strive on the net. This is an example on how to insert a cck field pro grammatically.
More reference on: http://drupal.org/node/159186

function dpci_admin_update_2(){
$cck_path = drupal_get_path("module","Content");
include_once($cck_path."/content_admin.inc");
include_once($cck_path."/content_crud.inc");
include_once($cck_path."/content.module");

$field_widget_type = 'text-options_onoff';
$label = 'My Check Box';
$content_type = 'page';
$field_name = 'my_check_box';
$properties['field_widget_type']='text-options_onoff';

// First step of creating the field - create the basic field (corresponds
// to page 1 of the Add field UI).
$default_field_add = array(
'label' => $label,
'type_name' => $content_type,
'field_widget_type' => $field_widget_type,
'op' => 'Create field',
'submit' => 'Create field',
'form_id' => '_content_admin_field_add_new',
);
// Create our new field.
_content_admin_field_add_new_submit('_content_admin_field_add_new', $default_field_add);

// Second step, update the field with our custom properties.
$default_field_edit = array(
'label' => $label,
'type_name' => $content_type,
'weight' => -10,
'description' => 'Some description',
'default_value_php' => '',
'group' => 0,
'required' => 1,
'multiple' => FALSE,
'text_processing' => 0,
'max_length' => '',
'allowed_values' => '0|dont check
1|check',
'allowed_values_php' => '',
'op' => 'Save field settings',
'submit' => 'Save field settings',
'type_name' => $content_type,
'field_name' => 'field_'.$field_name,
'form_id' => '_content_admin_field',
'default_value' => array(), // This might be cruft
'referenceable_types' => array(), // Used in nodereference fields and userreference
);

foreach($default_field_edit AS $key => $value) {
if (isset($properties[$key])) {
$default_field_edit[$key] = $properties[$key];
}
}

// Derive the widget and type from the input type.
$widget_parts = explode('-', $properties['field_widget_type']);
$default_field_edit['widget_type'] = $widget_parts[1];
$default_field_edit['module'] = $widget_parts[0] .', optionwidgets';

_content_admin_field_submit('_content_admin_field_add_new', $default_field_edit);

return array();
}

Monday, March 10, 2008

Quick way to remove an element from array in php

  1. // our initial array
  2. $arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");
  3. print_r($arr);
  4. // remove the elements who's values are yellow or red
  5. $arr = array_diff($arr, array("yellow", "red"));
  6. print_r($arr);

Wednesday, March 05, 2008

MYSQL Backup and Restore from Command Line

To backup a database:
mysqldump dbname >dbname.sql

To backup a single table:
mysqldump dbname tbname >tbname.sql

To Restore a database:
Create a db named "mydb"
mysql -u username -p mydb < dbname.sql
Reference: http://www.abbeyworkshop.com/howto/lamp/MySQL_Export_Backup/index.html