Converting Form API date field to timestamp

If you are not using a date module and just want to use date field from form api, you will want to insert it into DB, probably as timestamp. So to convert that value to timestamp you need to use some precoding for that. In this example we also use a simple textfield to enter time value and add it to timestamp. Also take a note we are using timezone value.

$timezone = $user->timezone;
$dateFrom = $form_state['values']['schedule_container']['date_container']['from']['date'];

Database API, several ways to retrieve data from query

$uid = 1;
$result = db_query('SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = :uid', array(':uid' => $uid));

Fetch next row as a stdClass object.
$record = $result->fetchObject();

Fetch next row as an associative array.
$record = $result->fetchAssoc();

Fetch data from specific column from next row.Defaults to first column if not specified as argument
$data = $result->fetchColumn(1);

Grabs the title from the next row. Retrieve all records into an indexed array of stdClass objects.
$result->fetchAll();

Curl on Localhost and WAMP on x64

I was going nuts over how to install CURL on 64 bit windows. I need it for some module and just enabling it from Wamp panel didn't work. So I searched for solutions and there are quite a few combos and solutions offered online but the only thing that worked for me and what was needed is

Navigate to WAMP\bin\php(your version of php)
edit php.ini
Search for curl, uncomment extension=php_curl.dll
Navigate to WAMP\bin\Apache(your version of apache)\bin\
edit php.ini
Search for curl, uncomment extension=php_curl.dll

Sorting array of objects by value

Drupal uses many multidimensional arrays and usually you won't need to do this, or will do it in some theme layer or with views. But I did have a case where I needed to sort out array of entities by score value so I did it this way.

I used uasort or you can also use usort, both should work and properly order arrays, if you want to be sure, maybe use uasort as it is said that it "maintains index association", not sure if this would be needed here.

Getting host node and node id of field collection

I was killing myself to get host node and node.id of field_collection I am using so I can do some preprocessing before this node is saved. I was trying with field_collection_item_get_host_entity but just couldn't make it work. So I asked google and found out I can do it with regular host function like

$collection = entity_load_single('field_collection_item', $collection_id);
$node = $collection->hostEntity();
echo $node->nid;

Getting host node and node id of field collection

I was killing myself to get host node and node.id of field_collection I am using so I can do some preprocessing before this node is saved. I was trying with field_collection_item_get_host_entity but just couldn't make it work. So I asked google and found out I can do it with regular host function like

$collection = entity_load_single('field_collection_item', $collection_id);
$node = $collection->hostEntity();
echo $node->nid;

Module paths in DB

Working with multi sites and modules located in sites/all and sites/some_site your module paths could get in problems. So for a fix check you just need to replace paths in your DB like

update system set filename = replace(filename, 'sites/all', 'sites/some_site');

and this way all your contrib modules get new path, set for the sites/some_site location. You should do this when you move your modules which are already in system and you have errors and problems with paths shown.

Drupal Drush Command Cheat Sheet Copy

Everbody likes Drush, why wouldn't you. Anyway I copied cheat sheet from some PDF as this is a drupal dump site and this way I can find it quicker and make also some additions to it.

When available, there is a shorter version of the same command in parentheses.

cache clear (cc) Clear all caches.

cron Run all cron hooks.

disable (dis) Disable one or more modules.

download (dl) Download core Drupal and projects like CCK, Zen, etc.

enable (en) Enable one or more modules.

PM-UPDATE needs the following modules installed/enabled to run: update

Recently I Installed Drush to windows http://drush.ws/drush_windows_installer, scared that it won't work like it should I tried to update some site so I go to local site directory and try pm-update. I get this message

pm-update needs the following modules installed/enabled to run: update. [error]
The drush command 'pm-update' could not be executed.