Drupal

Group Add More: Simulate clicking on group of multi-value fields

A custom content type that we built recently had multiple fields which accepted unlimited values. Some of these fields were grouped together and expected to accept values together.

The fix to this problem is rather simple. Simulate clicking on individual "Add More" buttons. We generalized this idea and create a jQuery plugin to facilitate this on any group of multi-value fields inside a container. This is supported for Drupal 6 & 7. Use the plugin from the approriate github branch from: https://github.com/nkmani/group-add-more (1.0 is for D6 and 2.0 is for D7).

To use this plugin:
Include the jquery.group-add-more.js file (both development as well as minified versions are available) in your theme.
Include the javascript snippet to attach the plugin to the container that has the add-more buttons.

The rendered group field would look like as shown below.

Filefield widget says "Access Denied" on legacy nodes

One of the media heavy sites managed by us, there is a media content type which has been around for a while (starting from Drupal 5). The images associated with the content has been managed through multiple APIs before finally being ported to make use of FileField module. As it happens, the older legacy files were distributed all over the place but the newer files where all structured in a single location.

FileField module can be given a path where all file uploads can reside. Now this path happens to be different from all the paths used in the legacy nodes. Most of the use cases this does not cause a problem. But when there is a need to edit nodes that refer to the legacy files, the FileField widget would say "Access Denied" next to the image in the node edit form.

The reason for this behavior is obvious. The file field widget wants to validate the file path to make sure it is accessible before it can display it (this happens only in the node edit mode; works fine in node view mode). Since the legacy files are in a place which the current configuration of the FileField widget does not know about, it fails the validation and hence says "Access Denied".

The validation, as it happens, is done through hook_file_download API, which is passed the filepath. So a simple work around for this problem is to add a hook_file_download() API in one of our custom modules to validate the path and return appropriate headers as shown in the following code:

/**
 * Implementation of hook_file_download().
 */
function legacyfiles_file_download($filepath) {
  // validate legacy paths
  if (file_exists($filepath)) {
    $filesize = filesize($filepath);
    return array(
      'X-Content-Length: ' . $filesize,
    );
  } else {
    return array();
  }
}
Issues with filter cache and embedding views slideshow

Scenario:
Drupal 7
Views and Views Slideshow module
Custom blocks with PHP_CODE input format to embed the views

This normally works only once after caches are cleared. After the first page load, the slideshow images would appear but the cycle would not work.

Reason:
The block system uses cached values of the content from filter module (even through blocks may not be cached), the filter content is always cached and this can't be disabled. The php input_filter evaluates the code and caches the result. Now when the code is evaluated, as a side effect, the slideshow module adds the appropriate required js files.

Subsequent page loads would not emit the required js files, but only the static html from the embed_view. If the view did not rely on additional js files, this would have worked. But in the case of slideshow plugin (or a similar plugin that adds its own extra js/css files) would break.

Work around:
Instead of building a custom block, configure the view as a block and then use display output theme override in view to add any customizations and use the views block. The views block is not cached the way filters are cached. So, this would always work.

How to add auto-complete support to textfield

In Drupal 7, it is pretty straightforward to add auto-complete support to any text field (much like the taxonomy fields).

hook_form_alter()

Place the following form_alter hook in your custom "mymodule".

function mymodule_form_alter((&$form, &$form_state, $form_id) {
  if ($form_id == "<id of form where autocompletion required>") {
     .
     .
     .

    // define the auto-completion values here
    // these will typically come from a data source
    $acValues = array("a", "b", "c", ... , 'z');

    $autocomplete['js'][] = array (
      'type' => 'setting',
      'data' => array('mymoduleACvalues' => $acValues)
    );
 
    $form["field_mytextfield"]["#attached"] = $autocomplete;
  }
}

jQuery behavior

Place the following jQuery behavior in some javascript file; either in the custom module or theme.

(function($) {
  Drupal.behaviors.mycustomAutoComplete = {
    attach : function(context, settings) {
      $(".field-name-field-mytextfield input").autocomplete({
        source : Drupal.settings.mymoduleACvalues
      });
    }
  }
})(jQuery);