Drupal API

Update Drupal 7 text fields max length for fields having data

Technologies: 
<?php
/*
* Utility to change the max length of a text field
*/
function change_text_field_max_length($field_name, $new_length) {
  $field_table = 'field_data_' . $field_name;
  $field_revision_table = 'field_revision_' . $field_name;
  $field_column = $field_name . '_value';

  // Alter value field length in fields table
  db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
  // Alter value field length in fields revision table

Actions in Views Bulk Operations

Views Bulk Operations come handy when doing bulk operations over a selected list. This is great because it combines the power of views to filter the selection and specific actions to do on each selected entity. For Ex: Updating some field in the record, deleting selected entity and so on.

Views also provide an easier way to define your own set of custom functions to perform on each selected row of entities.

First, we need to define an action :

Drupal l() and url() methods

Both l and url methods are handy to create urls for various entities. However, the variety of options available makes the usage exciting and complex.

l(): Formats an internal or external URL link as an HTML anchor tag.
Syntax: l($text, $path, array $options = array())

Creating an absolute URL using l(). The example below creates a node link.

Drupal File Operation

This post will illustrate various handy file operations available in Drupal

Get Absolute Path of a file from URI : drupal_realpath()
A URI in drupal looks like: private://donor_registration/somefile.csv for a private file
Absolute Path: /srv/www/example.com/public_html/sites/default/private/files/donor_registration/somefile.csv

Creating exportable field definitions in Drupal 7

There are times when we would want to create similar data structures across multiple drupal sites for common functionalities. Although this can be done using features but when the effort involves one time exercise and involves just few fields to be exported, using custom code for creating export definitions for importing in the other site can be a simpler alternative.

If you want export a field definition, you can use the handy field_info_field and field_info_instance functions. Taking our example above, here's how you would call these two functions: