Parasolx

Professional in Drupal web development, theme designing, consultation and training

Blogs

Code review sessions: ATK

14 May 2014 - 12:47 pm

Hari ini, bersama dengan pegawai Teknologi Maklumat daripada Sarawak, berkongsi pengalaman berkenaan dengan satu sistem PHP Framework iaitu ATK. Secara ringkasnya sistem ini mudah, ringan dan bekerja terus dengan struktur pangkalan data.

Perkongsian pengalaman ini bagi mencari dan menerbitkan sebuah sistem yang boleh diguna pakai untuk kegunaan kerja seharian. ATK sangat mudah dan "simple". Walau bagaimanapun, saya melihat agak banyak had atau kengkangan yang diberikan oleh ATK. Mungkin asas pembinaan framework ini adalah untuk fokus kepada memaparkan data daripada pangkalan data serta sedikit pemprosesan data sebelum diserahkan sepenuhnya kepada paparan pengguna.

Tetapi sekiranya untuk "grabbing data" atau "mining data" semata-mata, ATK ini sangat sesuai memandangkan proses pembangunannya adalah sangat mudah berbanding menggunakan Drupal. Drupal lebih kepada complete set library data system yang digunakan fokus untuk web based application atau web based portal. Sungguh pun begitu, pembangunan dengan Drupal adalah jauh lebih lama berbanding ATK.

Saya nampak ATK ini sangat berguna untuk mengendali data-data dan tatacara kerja yang tidak kompleks, segera dan tidak memerlukan paparan yang menarik.

[Snippet] Drupal 7 DB Query

10 May 2014 - 09:35 pm

Drupal 7 implemented database abstraction layer which built up on top with PDO. This make Drupal 7 in one step forward more faster for querying data from database compare to Drupal 6. Below are the snippets that can really useful for drowling custom data from database.

$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();

// Retrieve all records as stdObjects into an associative array
// keyed by the field in the result specified.
// (in this example, the title of the node)
$result->fetchAllAssoc('title');

// Retrieve a 2-column result set as an associative array of field 1 => field 2.
$result->fetchAllKeyed();
// Also good to note that you can specify which two fields to use
// by specifying the column numbers for each field
$result->fetchAllKeyed(0,2); // would be nid => created
$result->fetchAllKeyed(1,0); // would be title => nid

// Retrieve a 1-column result set as one single array.
$result->fetchCol();
// Column number can be specified otherwise defaults to first column
$result->fetchCol($db_column_number);

// Count the number of rows
$result->rowCount();

[Snippet] The right way rendering field in Drupal 7

29 Apr 2014 - 07:27 am

The best part in Drupal 7, it was totally a massive upgrade from Drupal 6 by introducing of entity concept. This is really awseome since we do not depend on "nodes" concept anymore. Supporting the entity, more and less in D7 we could expand used of Field API which provide custom field design through GUI.

Its not stop there. For themers, we able to print and render any field from any entity bundle directly into theme files without sacrifice write up SQL query code. By using node_load(), we load everything node data for nid targeting.

The best practices to render specific field is using field_get_items(). After that pass this array into field_view_value() to get the raw value and sanitize them before rendering.

$node = node_load($nid);
$field = field_get_items('node',$node,'field_name');
$output = field_view_value('node',$node,'field_name',$field[0]);
print render($output);

This method make the render value aware towards language tag. Direct calling raw value like this:

$value = $node->field_name['und'][0]['safe_value'];

is not a best practice!

[Snippet] Pass multiple filter values in Drupal Views

28 Apr 2014 - 10:31 pm

One the most powerful function in Drupal Views is contextal filter. This function able to the Views module to filter the result based on argument specific in contextual filter item. However, the problem with this function is it unable to support multiple filter by "OR" conditions. The best solution to counter this problem is by altering its query before Views rendering the results.

By creating custom module, we leveraging hook_views_query_alter().

function my_module_views_query_alter(&$view, &$query) {
  dpm($query,'query');
}

This will shows all query that available in all views. So, we specific by add condition to target specific views.

if ($view->current_display == 'my_current_display') { ... }

Then, by adding query altering for an array:

foreach ($nids as $nid){
  $query->where[1]['conditions'][] = array(
   'field' => 'node.nid',
   'value' => $nid,
   'operator' => '=',
 );

After that we add some more query altering:

$query->where[1]['type'] = 'OR';

Save and clear your cache.

[Snippet] Menus get duplicate in Admin Menu for Drupal 7

19 Mar 2014 - 07:12 pm

During developing web using Drupal 7, sometimes we caught with duplication of menus when using Admin Menu. This problem usually happen when we accidently move or modify administration menu to other menu blocks. Actually it shoudn't happen because Drupal was designed to able handle any modification make to any menu.

However when someone with inexperience with Drupal system, installing unstable modules that interfere with Drupal menu system could leads this problem happened unexpected.

For solution:

We need to log into database either through bash or using GUI like PHPMyAdmin.

Then, ran this query:

DELETE FROM `menu_links` WHERE `menu_links`.`module` = 'admin_menu'

After that ran this query:

DELETE FROM `menu_links` WHERE `menu_links`.`module` = 'system'

These two query will delete all menus under admin menu. You will face blank menu if try to refresh any page.

After that go to "admin/build/modules".

Here Drupal will rebuilt all menu system again based on default configuration.

If it still didn't work, repeat this step again but instead go to "admin/build/modules", run directly in web browser bar, "http://example.com/update.php". This step will force Drupal to refresh all default configuration and modules installed.

Lastly clear all Drupal cache manually.

Good luck.

Pages