drupal

Drupal Bootstrap 3 theme media queries javascript event (like Omega 3)

Having used the Omega 3 Drupal theme for a few years now I got quite use to writing javascript functions that would react to the 'reponsivelayout' event that was supplied by the theme. This event would fire after the responsive classes were applied to the body tag when the page loaded or when the media queries changed the site layout.
I often used this event to create things like mobile menus and such.
Now on the latest site I am building I am trying out the Bootstrap theme which does not fire such an event, so I thought I would add one that basically uses javascript similar to the Omega 3 theme.
If you would like the same, just add a js file to your Bootstrap sub theme with the js below:

 (function($) { 

var setCurrentLayout = function (oldLayout, newLayout) {
$('body').removeClass('responsive-layout-'+oldLayout).addClass('responsive-layout-'+newLayout);
$.event.trigger('responsivelayout', {from: oldLayout, to: newLayout});
};

var checkCurrentLayout = function () {
var current = 'xs';
var dummy = $('<div id="bootstrap-media-query-dummy" class="container"></div>').prependTo('body');
var width = dummy.outerWidth();

if (width > 750 - 1) {
current = 'sm';
}
if (width > 970 - 1) {
current = 'md';
}
if (width > 1170 - 1) {
current = 'lg';
}
dummy.remove();
if (typeof Drupal.settings.currentLayout == 'undefined') {
Drupal.settings.currentLayout = current;
setCurrentLayout('', current);
} else if (Drupal.settings.currentLayout != current) {
var old = Drupal.settings.currentLayout;
Drupal.settings.currentLayout = current;
setCurrentLayout(old, current);
}
...

Drupal Module: Watchdog Event Extras

When checking the logs on the Drupal sites I run I often find myself copying the IP and searching it on google. Most of the time it is when I'm looking at a page not found or login attempt failure and I would like to know if these came from some sort of spam or a legitimate user. This process became a little tedious, so I created the Watchdog Event Extras module.

Basically what the module does is overrides the standard drupal log event page, outputting the same and adding a drupal_alter()
call. At the moment the module includes two sub modules, the first of which adds geolocation data derived from the hostname and the freegeoip.net web service with the second adding data from the stopforumspam.com web service.

Any other module can also add data to the report page by implementing MYMODULE_watchdog_event_extras_alter(&amp;$rows, $dblog)

Cheick it out https://www.drupal.org/project/watchdog_event_extras

Also, don't forget to checkout the Link Favicon Formatter module if you ever needed to add a site favicon to a link field: https://www.drupal.org/project/link_favicon_formatter

Changing the Inline Entity Form autocomplete field to a select list

The Inline Entity Form module for Drupal is an awesome module. Most people probably have experienced using it with Drupal Commerce, but it can be used when ever an Entity Reference field is used. I have personally used it on many projects, most of which were not commerce related.

Sometimes though, the autocomplete field to select an existing entity is not appropriate, say if you only have a few entities that can be referenced.

Luckily though we can make use of the hook that the Inline Entity Form module provides which is "HOOK_inline_entity_form_reference_form_alter(&$reference_form, &$form_state)".

Below is an example of how to change all the autocomplete fields to select lists when the reference field references any type of node.

 function MYMODULE_inline_entity_form_reference_form_alter(&$reference_form, &$form_state) { 
// change the entity reference 'add more' autocomplete into a select list
if($reference_form['#entity_type'] == 'node') { // only change the forms that reference a node
$field = field_info_field($reference_form['#parents'][0]);
$nodes = node_load_multiple(array(), array('type' => reset($field['settings']['handler_settings']['target_bundles'])));
$options = array();
foreach($nodes as $node){
$options[$node->title.' ('.$node->nid.')'] = $node->title;
}
$reference_form['entity_id']['#type'] = 'select';
$reference_form['entity_id']['#options'] = $options;
}
}

Drupal Migrate V2 and Addressfield

While trying to migrate data from a legacy CakePHP system to Drupal 7 recently I had a problem getting an Addressfield field to play nicely.
What I had origianly was this (which did not work).

  $this->addFieldMapping('field_customer_address:thoroughfare', 'address'); 
$this->addFieldMapping('field_customer_address:locality', 'suburb');
$this->addFieldMapping('field_customer_address:administrative_area', 'state');
$this->addFieldMapping('field_customer_address:postal_code', 'postcode');

Looking all over the web I found many examples saying to use the arguments function. eg.
  $address_arguments= array( 
'thoroughfare' => array('source_field' => 'address'),
'locality' => array('source_field' => 'suburb'),
'administrative_area' => array('source_field'=>'state'),
'postal_code' => array('source_field' => 'postcode'),
);
$this->addFieldMapping('field_customer_address')
->arguments($address_arguments);

But when trying this I got a message from the module saying that this was not correct. I think the above code was/is for version 1 of the migrate module.
It turns out that all I needed to do was to set a default country. ie.
  $this->addFieldMapping('field_customer_address')->defaultValue('AU'); 
$this->addFieldMapping('field_customer_address:thoroughfare', 'address');
$this->addFieldMapping('field_customer_address:locality', 'suburb');
$this->addFieldMapping('field_customer_address:administrative_area', 'state');
$this->addFieldMapping('field_customer_address:postal_code', 'postcode');

Useful Drupal Links

I often seem to forget all the good places on the web to find info and things about drupal, So I thought I would start a list, mainly for my own reference.

  • Drupal API Reference Drupal API reference. Every Drupal dev. should be taking advantage of this site.
  • TopDrops Listing of most popular Drupal powered websites.
  • Drupal Jobs A group on Drupal Groups dedicated to job advertisements in the Drupal space.
  • Drupal paid services Another page dedicated to job advertisements.
  • Drupal Project applications A list of Drupal projects that are waiting review and approval to become full Drupal projects.
  • pareview.sh A site to review your Drupal module code. This should be used on all of your Drupal projects.
  • simpletest.me Easily test a drupal theme or module.
  • Drupal 7 Template (Theme Hook) Suggestions
  • Drupal X-Ray Tool to view the modules a Drupal website uses.
  • Create forms in a safe way to avoid cross-site request forgeries (CSRF)
Modules to remember
  • Fields
    • Conditional fields
  • Taxonomy
    • Hierarchical Select
  • Input formats
    • Pathologic
  • Image carousels / sliders
    • Slick Carousel
    • Owl Carousel
  • Development
    • Object Log
    • Security Review
  • Views
    • Foo table - responsive tables
  • Paths
    • Query parameters to url
  • Performance
    • CSS Embedded Images
    • Views Litepager
    • Views content cache
  • Emails
    • Mail logger
    • Reroute email
Useful modules when inheriting a Drupal...