Install Node.js & Grunt in Ubuntu 14.04


First you need to add this for add-apt-repository command to work:

$ apt-get install software-properties-common

If not you’ll get this error:

$ sudo: add-apt-repository: command not found

Install Node

$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs

Install Grunt

$ sudo npm install -g grunt-cli
 
Advertisements

Move up metatag in Drupal 7


/**  
* Implements hook_html_head_alter().  
* moving og:title up in head element list  
*/ 
function mymodule_html_head_alter(&$head_elements) {  
  foreach ($head_elements as $key => $element) { 
    if($key == 'metatag_og:title') { 
      $head_elements[$key]['#weight'] = -999; 
    } 
  } 
}

Drupal Update Hook Dependencies


Sometimes, when developing an update hook, you might want it to run after or before a different module’s update hook. this is the way to do it:

Place the following code into your module’s mymodule.install file.

Where 7001 and 7140 are the hook update number.

/**
 * Implementation of hook_update_dependencies().
*/
function mymodule_update_dependencies() {
  // Make sure that mymodule runs after other_module
  $dependencies['mymodule'][7001] = 
 'other_module' => 7140,
);
  return $dependencies;
}

Drupal 7 override image styles programmatically


/**
 * Implements hook_image_styles_alter().
 */
function mymodule_image_styles_alter(&$styles) {
  // Check that we only affect a default style.
  if (isset($styles['thumbnail_large']) && isset($styles['thumbnail_large']['storage'])) {

    if ($styles['thumbnail_large']['storage'] == IMAGE_STORAGE_DEFAULT) {

      $styles['thumbnail_large'] = array(
        'name' => 'thumbnail_large',
        'effects' => array(
          1 => array(
            'label' => 'Javascript crop',
            'help' => 'Create a crop with a javascript toolbox.',
            'effect callback' => 'imagecrop_effect',
            'form callback' => 'imagecrop_effect_form',
            'summary theme' => 'imagecrop_effect_summary',
            'module' => 'imagecrop',
            'name' => 'imagecrop_javascript',
            'data' => array(
              'width' => '620',
              'height' => '350',
              'xoffset' => 'center',
              'yoffset' => 'center',
              'resizable' => 1,
              'downscaling' => 1,
              'aspect_ratio' => 'CROP',
              'disable_if_no_data' => 1,
            ),
            'weight' => '1',
          ),
          2 => array(
            'label' => 'Scale and crop',
            'help' => 'Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image.',
            'effect callback' => 'image_scale_and_crop_effect',
            'dimensions callback' => 'image_resize_dimensions',
            'form callback' => 'image_resize_form',
            'summary theme' => 'image_resize_summary',
            'module' => 'image',
            'name' => 'image_scale_and_crop',
            'data' => array(
              'width' => '620',
              'height' => '350',
            ),
            'weight' => '2',
          ),
        ),
        'label' => 'thumbnail_large',
      );
    }
  }
}

Drupal 7 View unset Read More link (attachment_after) programmatically


function mymodule_views_post_build(&$view) {
  switch ($view->name) {
    case 'my_view_name': 
      $view->attachment_after = ''; 
      unset($view->attachment_after); 
    break;
  }
}

Add different scripts to the header in Drupal (meta, link..)


Now let’s imagine that you want to use a generic function to add different types of scripts to your header: <meta> <link>? <script>. This is the solution I’ve found.

Important: it took me few hours until I realized.

second parameter of drupal_add_html_head must be a unique string, so I’ve used the path to the script because they are in different files inside a folder structure. but you can use a random generator, or maybe a better solution, but keep in mind that must be unique.

$script = "<meta name="google-site-verification" content="xxxxx" />";
drupal_add_html_head(array('#type' => 'markup', '#markup' => $script), "$script");
$script = "<link rel="apple-touch-icon-precomposed" href="www.example.com" />";
drupal_add_html_head(array('#type' => 'markup', '#markup' => $script), "$script");