Drupal7 update hook dependencies


You sometimes find that update hooks run in an unwanted order that affects their behaviour or creates errors and makes your site crash. You can override this and specify the order that you want with the following hook in your .install file.

 function new_module_update_dependencies() {
  // Run new_module update after old_module update.
  $dependencies['new_module'][7001] = array(
    'old_module' => 7000,
  );
 
  return $dependencies;
 }

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");

Backup, Empty and Import databases with Drush in Drupal.


Create a backup.

drush sql-dump > /path/to/folder/backup.sql

Empty the database tables.

drush sql-drop

Import the tables.

drush sql-cli < /path/to/folder/backup.sql

How to create Features and add components with Drush in Drupal


Creation

drush fe --d --destination="sites/all/modules/custom"  my_feature views:my_view

We can find this information in the .info file

name = my_feature
core = 7.x
package = Features
dependencies[] = views
features[ctools][] = views:views_default:3.0
features[features_api][] = api:2
features[views_view][] = my_view

Enable your Feature

drush cc all
drush en my_feature

Adding Components

To see that components are available to add into your feature we’ll run Feature Compile

drush fc
If we get something like this:
Available sources
image:adaptive
image:thumbnail
image:medium
image:large

I could add into the .info file the following syntax.

features[image][] = thumbnail

Update the Feature

via Drush usingdrush fu, short for drush features-update.

drush --d fu my_feature