jide's blog

I wanted to alter the meta used by a post type before it is actually retrieved. This can be useful since a lot of plugins and themes use metadata to store information about posts.

function [your theme]_post_metadata($metadata, $object_id, $meta_key, $single) {
if ($meta_key == '[your meta key]') {
$meta_type = 'post';
$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
if ( !$meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[$object_id];
}
if ( !$meta_key )
$metadata = $meta_cache;
if ( isset($meta_cache[$meta_key]) ) {
if ( $single )
$metadata = maybe_unserialize( $meta_cache[$meta_key][0] );
else
$metadata = array_map('maybe_unserialize', $meta_cache[$meta_key]);
}
// Do whatever you want with $metadata. You may need to unserialize it.
return metadata;
}
}
add_filter('get_post_metadata', '[your theme]_post_metadata', true, 4);

I stumbled upon this article from Michael Barrett http://abouthalf.com/development/poor-mans-nth-child-selector-for-ie-7-a..., explaining how to use adjacent sibling selector to fake nth-child pseudo selectors.

So I wondered if this trick could be used to fake odd / even too.

And the answer is "Yes, kind of". If you need only a limited amount of odd / even children, that will work, but you have to write as many selectors as odd children you have :

table tr {
background: red; // odd
}
table tr:first-child + tr,
table tr:first-child + tr + tr + tr,
table tr:first-child + tr + tr + tr + tr + tr,
table tr:first-child + tr + tr + tr + tr + tr + tr + tr
//...and so on {
background: blue; // even
}

If like me you are crazy enough to try to make Qt apps using Ruby, and want to use Windows API to display thumbnails of windows, here is how. The hard part is that you have to pack the dskThumbProps struct before calling DwmUpdateThumbnailProperties from Dwmapi. It's a dumb API: You call it and place it at arbitrary coordinates on the window.

$DwmUpdateThumbnailProperties = Win32API.new "Dwmapi.dll","DwmUpdateThumbnailProperties", 'LP'
DWM_TNP_VISIBLE = 0x00000008
DWM_TNP_RECTDESTINATION = 0x00000001
DWM_TNP_SOURCECLIENTAREAONLY = 0x00000010
# Here you should call DwmRegisterThumbnail and set x, y, width and height.
dskThumbProps = [
DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_SOURCECLIENTAREAONLY,
[x, y, x + width, y + height].pack('l4'),
'',
255,
1,
1
].pack("La16a16III")
$DwmUpdateThumbnailProperties.call(thumbId, dskThumbProps)

Some users of the freshy theme reported that the theme is suddenly broken, without any reason. I suspect some changes on wordpress.com to be responsible for this, but I have no clue why.

The theme was created by me, but since then, wordpress team has taken it and its now a modified version of theirs on which I have absolutely no control, so the best you can do is to tell wordpress.com about the problem and see if they can fix it.

Putting this back in the CSS restores most of it, but it's a *really* dirty quick hack I made on the go and it may not work well depending on your customizations :

#frame {
background: #fff;
width: 780px;
margin-right: auto;
margin-left: auto !important;
margin-left: 1px;
margin-top: 0px;
padding: 0px;
text-align: left;
}
#wrapper {
box-shadow: 0 0 10px 2px #5a5a5a;
-webkit-box-shadow: 0 0 10px 2px #5a5a5a;
-moz-box-shadow: 0 0 10px 2px #5a5a5a;
margin: 0 auto;
width: 780px;
}
#content {
width: 510px !important;
width: 550px;
padding: 0 20px 0 20px;
float: left;
background: #FFFFFF;
overflow: hidden;
}
#sidebar div, #sidebar ul {
padding: 0 9px 9px 9px;
}

To show the profile2 form directly on the user account edit form :

The following assumes that the profile2 type is called "profile".

First, we have to hide the tab on the user account page :

/**
* Implements hook_menu_alter().
*/
function MY_MODULE_menu_alter(&$items) {
$items['user/%user_category/edit/profile']['access callback'] = FALSE;
}

Then, merge the profile2 form with the user account form itself :

/**
* Implements hook_form_FORM_ID_alter() for the user edit form.
*/
function MY_MODULE_form_user_profile_form_alter(&$form, &$form_state) {
if (($type = profile2_get_types('profile')) && $type->userCategory) {
if (empty($form_state['profiles'])) {
$profile = profile2_load_by_user($form['#user'], 'profile');
if (empty($profile)) {
$profile = profile_create(array('type' => 'profile', 'uid' => $form['#user']->uid));
}
$form_state['profiles'][$profile->type] = $profile;
}
profile2_attach_form($form, $form_state);
}
}

Drupal uses a default jQuery UI theme. Here is how to use your own theme created using the jQuery ThemeRoller.

Put the generated jQuery UI theme in your theme directory, in a subfolder called "jquery-ui". It should have an "images" folder and a "jquery-ui.css" file.

Then, in your theme's template.php :

/**
* Implements hook_library_alter().
*/
function YOUR_THEME_library_alter(&$libraries, $module) {
$libraries['ui']['css'] = array();
$libraries['ui.accordion']['css'] = array();
$libraries['ui.autocomplete']['css'] = array();
$libraries['ui.button']['css'] = array();
$libraries['ui.datepicker']['css'] = array();
$libraries['ui.dialog']['css'] = array();
$libraries['ui.draggable']['css'] = array();
$libraries['ui.droppable']['css'] = array();
$libraries['ui.mouse']['css'] = array();
$libraries['ui.position']['css'] = array();
$libraries['ui.progressbar']['css'] = array();
$libraries['ui.resizable']['css'] = array();
$libraries['ui.selectable']['css'] = array();
$libraries['ui.slider']['css'] = array();
$libraries['ui.sortable']['css'] = array();
$libraries['ui.tabs']['css'] = array();
$libraries['ui']['css'][drupal_get_path('theme', 'YOUR_THEME') . '/jquery-ui/jquery-ui.css'] = array();
}

For a recent project, I had to migrate from Drupal 5 to Drupal 7. Here is how I did it without too much pain. The key steps are :

  • Export data to CSV from the original instance using Views module and a CSV output plugin.
  • Import them in the 7.x instance using Feeds.

A friend of mine just pointed out that my name is written in big characters in a slide of the latest Keynote of Dries Buytaert showing Drupal contributors :) It is in slide #10, and also in the video, around 1:00. Yay ! I'm famous !

Random magic hehe, I'm not such a big contributor ;) Try to find my name in this picture where contributors size is proportionate, that's a little harder :) :

/**
* Implements hook_cron_queue_info().
*/
function MYMODULE_cron_queue_info() {
$queues = array();
$queues['MYMODULE_queue_notify'] = array(
'worker callback' => 'MYMODULE_queue_notify_worker',
'time' => 15,
);
return $queues;
}
 
/**
* Queue worker for sending notification mails.
*/
function MYMODULE_queue_notify_worker($params) {
drupal_mail('MYMODULE', 'notify', $params['to'], language_default(), $params, $params['from']);
}
 
/**
* Implements hook_mail().
*/
function MYMODULE_mail($key, &$message, $params) {
$language = $message['language'];
$message['subject'] = $params['subject'];
$message['body'][] = $params['message'];
}
 
/**
* Implements hook_rules_action_info().
*/
function MYMODULE_rules_action_info() {
return array(
'MYMODULE_rules_action_mail' => array(
'label' => t('Send mail'),
'group' => 'MYMODULE',
'parameter' => array(
'to' => array(
'type' => 'text',
'label' => t('To'),
'description' => t('The e-mail address or addresses where the message will be sent to. The formatting of this string must comply with RFC 2822.'),
),
'subject' => array(
'type' => 'text',
'label' => t('Subject'),
'description' => t("The mail's subject."),
),
'message' => array(
'type' => 'text',
'label' => t('Message'),
'description' => t("The mail's message body."),
),
'from' => array(
'type' => 'text',
'label' => t('From'),
'description' => t("The mail's from address. Leave it empty to use the site-wide configured address."),
'optional' => TRUE,
),
),
),
);
}
 
/**
* Action: Add notification mails to the queue.
*/
function MYMODULE_rules_action_mail($to, $subject, $message, $from = NULL, $settings, RulesState $state, RulesPlugin $element) {
// Gather params.
$params = array(
'to' => str_replace(array("\r", "\n"), '', $to),
'from' => !empty($from) ? str_replace(array("\r", "\n"), '', $from) : NULL,
'subject' => $subject,
'message' => $message,
);
// Add to the queue.
$queue = DrupalQueue::get('MYMODULE_queue_notify');
$queue->createItem($params);
}

When using a view with exposed filters, if the exposed filter is empty, by default views renders the view with all results displayed. To make an empty view instead :

  • Add an argument "Global: Null"
  • Action to take if argument is not present: Provide default argument
  • Default argument type: Fixed entry
  • Default argument: let it blank
  • Validator: PHP code
  • PHP validate code:
    if (!empty($view->exposed_input) && count($view->exposed_input) == 1 && isset($view->exposed_input['destination'])) {
    return FALSE;
    }
    return (!empty($view->exposed_input));
  • Action to take if argument does not validate: Display empty text (if you want the view to be rendered)

And voilà !

Syndicate content