Drupal Blog

User login

Drupal 7: Hide a field depending on the value of a checkbox

I recently had the requirement to hide a horizontal tab in node display depending on the value of a checkbox i.e. Show tab X. For the horizontal tabs I was using the excellent Fieldgroup module.

In order to achieve the above, I created a little custom module and used the newly introduced in drupal 7 hook, hook_node_view_alter.

blog:

Drupal 7: Add a custom link in admin menu

Here is some code to be used in a custom module in Drupal 7 in order to create a custom link in the admin menu. This item will show up in the Configuration section and it is particularly useful with the administration menu module.

blog:

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.

Drupal Commerce - "Select a product" element: How to change the select list to radio buttons

Here is the code that changes the default select list of "Select a product" element of Drupal Commerce to radio buttons, for a standalone product list.

The following goes in a custom module (tested in Drupal 7).

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if (
strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0 && isset($form['product_id'])) {
   
$form['product_id']['#type'] = "radios";   
  }
}
?>

MySQL Order by before Group by

In a custom module, I needed to get the latest node that each user published. It is fairly easy to goup by users, but it needs a little something to actually accomplish the sorting.

The following query worked for me, we need to do the sorting in a nested select:

<?php
pager_query
("SELECT * FROM (SELECT * FROM node order by created DESC) as node WHERE nid IN ($placeholders)  GROUP BY uid",10,0,$count_query);
?>

In case the following error comes up:

Error: 1248 - SQLSTATE: 42000 ER_DERIVED_MUST_HAVE_ALIAS

Pages

Subscribe to Drupal Blog