Community

When do we choose to use the sign '#' for array keys?

Does every array keys must be with the sign '#'?
When do we choose to use the sign '#' for array keys?

Exanple of code: (from the Pro Drupal Development book):

<?php
function color_example_field_widget_form(&$form, &$form_state, $field, $instance,
                                        
$langcode, $items, $delta, $element) {
//An array with no '#' sign
        
$value = isset($items[$delta]['rgb']) ? $items[$delta]['rgb'] : '';   
        
$element += array(
            
'#delta' => $delta,
         );
        
$element['rgb'] = array();
         switch(
$instance['widget']['type']) {
             case
'color_example_colorpicker':
//array keys with the '#' sign
                
$element['rgb'] += array(
                    
'#suffix' => '<div class="field-example-colorpicker"></div>',
                    
'#attributes' => array('class' => array('edit-field-example-colorpicker')),
                    
'#attached' => array(
                        
//Ad color picker
                        
'library' => array(
                             array(
'system','farbtastic'),
                         ),
                        
//Add javascript to trigger the colorpicker
                        
'js' => array(drupal_get_path('module', 'color_example').
                                        
'/color_example.js'),
                     ),
                 );
//array keys with the '#' sign                
            
case 'color_example_text':
                
$element['rgb'] += array(
                    
'#title' => t('Event's RGB Color'),
                     '
#type' => 'textfield',
                    
'#default_value' => $value,
                    
'#size' => 7,
                    
'#maxlength' => 7,
                 ),
             break;
             case
'color_example_3text':
                 if (isset(
$items[$delta]['rgb'])) {
                    
preg_match_all('@..@', substr($items[$delta]['rgb'],1), $match);
                 }
                 else {
                    
$match = array(array());
                 }
                
//A fieldset to hold the three text fields.
                
$element += array(
                    
'#type' => 'fieldset',
                    
'#element_validate' => array('color_example_3text_validate'),
                    
'#delta' => $delta,
                    
'#attached' => array(
                        
'css' => array(drupal_get_path('module, 'color_example').
                                         '
/color_example.css')
                     ),
                 );
                 //Create a textfield for saturation values for red, Green, and Blue.
                 foreach(array('
r' => t('Red'), 'g' => t('Green'), 'b' => t('Blue')) as
                          $key => $title) {
                     $element[$key] = array(
                         '
#type' => 'textfield',
                        
'#title' => $title,
                        
'#size' => 2,
                        
'#default_value' => array_shift($match[0]),
                        
// '#description' => t('The 2-digit hexadecimal representation of
                                                
the @color saturation, like "a1" or "ff"', array('@color => $title')),
                     );
                 }
             break;
         }
         return $element;        
     }
?>

Comments

# indicates

You might find the following link very helpful, Chris offers a very good pace for learning Drupal. BuildAModule
btw, # indicates an element of an array

"The skill of coding is to create a context in which other people can contribute."

The use of '#' to start an

The use of '#' to start an array key is a convention. It is used to different elements that the code processing the array acts on from user elements. In the case of the form API the user elements are the individual form elements and the '#' keys are how to process those elements.

nobody click here