Home
  • English
  • עברית
  • Home
  • Portfolio
  • Blog
  • Services
  • About us
  • Contact

hook_widget_settings_alter() and Pandora

Submitted by Amitaibu on Thu, 11/19/2009 - 16:03
— Tagged as
  • Drupal-planet

As markus_petrux wrote:

It seems that the new addition to CCK, the new hook_widget_settings_alter() opened Pandora's box.

There are quite a few modules that attach their own logic to CCK fields. Up until now those modules had to have their own DB or set their own variables.

In CCK 2.5, hook_widget_settings_alter() got in, which allows your module to hook into the field settings, and rely on CCK to save and load those settings when needed.

But that's not all - as the settings are becoming part of the CCK field defention it means that when you'll export the CCK to code - your module settings will be there. Your module now actually has import/ export functionality without you even having to bother about.

All you need is to pack your CCK code export in Features.
Here's an example that will add some data to number fields:

<?php
/**
* Implementation of hook_widget_settings_alter().
*/
function foo_widget_settings_alter(&$settings, $op, $widget) {
 
$widget_types = array('number');
  if ((!empty(
$widget['type']) && in_array($widget['type'], $widget_types)) || (!empty($widget['widget_type']) && in_array($widget['widget_type'], $widget_types))) {
    switch (
$op) {
      case
'form':
       
$settings['foo'] = array(
         
'#type' => 'checkbox',
         
'#title' => t('Some settings'),
         
'#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
         
'#default_value' => !empty($widget['foo']),
        );

        break;
      case
'save':
       
$settings[] = 'foo';
        break;
    }
  }
}

/**
* Check if 'foo' is enabled on a field.
*/
function foo_is_field_foo($field_name) {
 
$field = content_fields($field_name);
  return !empty(
$field['widget']['foo']); 
}
?>

Comments

Thanks, much needed documentation

Thanks for writing this up... Didn't know about the new hook,. I am planning on writing a new module, which will definetly use this hook.

We should realy work on the documentation in drupal.org about CCK hooks... it's getting realy outdated :X

  • reply
Submitted by manuee (not verified) on Wed, 04/28/2010 - 21:45.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • Pairs of<blockquote> tags will be styled as a block that indicates a quotation.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Gizra | Brener 5, Tel-Aviv 63826 Israel | +972-527-587-697 | info@gizra.com