Category Archives: Drupal

Drupal: Importing commerce products with Feeds Import 3

My CMS of choice for non-bespoke projects is Drupal even though it’s written in PHP it seems a lot more secure, stable and extensible than most CMS’ out there. Recently I’ve been working on an ecommerce site using Drupal Commerce which is a bit tricky to learn but very flexible and well integrated with Drupal. Today I needed to import a product list into the new system from an existing platform. Fortunately with Drupal’s Feeds Import module this is pretty straight forward (after reading the documentation about how to process multiple taxonomies etc). However it seems like it recently had an upgrade and version 3 is incompatible with version 2 (there’s a Commerce adaptor for v2).

I couldn’t find any code about how to integrate this latest version of Feeds Import with Drupal Commerce to import the prices of the products (which are linked to standard nodes using a Product Reference field). So, I created an input filter of my own to do this, see the code below. Note the custom cover_type field which and also setting the extended data attribute of the pricing detail.

class CommerceImportFilter {
    public static function add_product( $field ) {
        $cp = commerce_product_new('product');
        $cp->title = 'softcover';
        $cp->field_cover_type = array(LANGUAGE_NONE => array( 0 => array(
            'value' => 'soft'
        )));
        $cp->commerce_price = array(LANGUAGE_NONE => array( 0 => array(
          'amount' => $field * 100,
          'currency_code' => 'TRY',
          'data' => array( 'include_tax' => 'kitap_kdv' ),
        )));
        commerce_product_save($cp);
        return $cp->product_id;
    }
}

Migrating Drupal to new server breaks login

So I just cloned a drupal site onto a new server. It all worked fine but then I couldn’t log in. I found this post which said you need to perhaps change the $cookie_domain variable but that didn’t make any difference. Finally I found the root cause of the problem – mod_rewrite wasn’t active so even though the user/login page was displaying (it was status 404 which redirects through to index.php hence into drupal) it wasn’t accepting POST requests.

a2enmod rewrite
service apache2 restart

Job done.

Add placeholder to all input fields in Drupal

Although this blog is wordpress I’d typically choose Drupal for doing any proper content-based website as it’s so easily flexible and supports multiple languages really easily out of the box. I was doing a site recently where we wanted to have mostly placeholders everywhere rather than form labels. This was unfortunately a bit tricky so I wrote the following code for the theme’s template.php to automatically put labels into the placeholders of input elements:

function THEME_form_alter(&$form, &$form_state, $form_id) {
    if( $form_id == 'user_register_form' ) {
        $form['account']['mail']['#title'] = t('Your Email Address');
    }
    if( $form_id == 'views_exposed_form' ) {
        foreach( $form as $key => &$config ) {
            if( !is_array($config) )
                continue;
            if( array_key_exists('#attributes', $config) )
                $config['#attributes']['placeholder'] = $config['#attributes']['title'];
        }

        $form['keys']['#attributes']['placeholder'] = $form['#info']['filter-keys']['label'];
        $form['#action'] = '';
    }

    //error_log( print_r( $form, TRUE ) . ' ' . $form_id );
    add_placeholder_check($form);
}

function add_placeholder_check(&$array) {
    if( !is_array($array) )
        return;

    foreach( $array as $key => &$config ) {
        if( !is_array($config) )
            continue;
        add_placeholder_check( $config );
        if( substr($key, 0, 1) != '#' && array_key_exists( '#title', $config ) ) {
            if( !array_key_exists('#attributes', $config) )
                $config['#attributes'] = array();
            $config['#attributes']['placeholder'] = $config['#title'];
        }
    }
}