Same name and namespace in other branches
  1. 7.x-2.x color_field.field.inc \color_field_field_widget_form()

Implements hook_field_widget_form().

File

./color_field.module, line 572

Code

function color_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
    $widget = $element;
    $widget['#delta'] = $delta;
    $default_value = isset($instance['default_value'][0]['rgb']) ? $instance['default_value'][0]['rgb'] : '';
    $value = isset($items[$delta]['rgb']) ? $items[$delta]['rgb'] : $default_value;
    switch ($instance['widget']['type']) {
        case 'color_field_simple_color':
        case 'color_field_simple_widget':
            // Add library.
            drupal_add_library('color_field', 'jquery-simple-color');
            $field_id = $field['field_name'] . '-' . $langcode . '-' . $delta;
            // Support field collection module.
            isset($element['#field_parents']) && !empty($element['#field_parents']) ? $bundle_id = '-' . implode('-', $element['#field_parents']) : ($bundle_id = '');
            $id = str_replace("_", "-", 'edit' . $bundle_id . '-' . $field_id . '-rgb');
            $settings['cell_width'] = isset($field['settings']['cell_width']) && $field['settings']['cell_width'] != '' ? (int) $field['settings']['cell_width'] : 10;
            $settings['cell_height'] = isset($field['settings']['cell_height']) && $field['settings']['cell_height'] != '' ? (int) $field['settings']['cell_height'] : 10;
            $settings['cell_margin'] = isset($field['settings']['cell_margin']) && $field['settings']['cell_margin'] != '' ? (int) $field['settings']['cell_margin'] : 1;
            $settings['box_width'] = isset($field['settings']['box_width']) && $field['settings']['box_width'] != '' ? $field['settings']['box_width'] . 'px' : '115px';
            $settings['box_height'] = isset($field['settings']['box_height']) && $field['settings']['box_height'] != '' ? $field['settings']['box_height'] . 'px' : '20px';
            $settings['columns'] = isset($field['settings']['columns']) && $field['settings']['columns'] != '' ? $field['settings']['columns'] : '16';
            $widget += array(
                '#attached' => array(
                    'js' => array(
                        drupal_get_path('module', 'color_field') . '/color_field_jquery_simple_color/color_field_jquery_simple_color.jquery.js',
                        array(
                            'data' => array(
                                'color_field_jquery_simple_color' => array(
                                    '#' . $id => $settings,
                                ),
                            ),
                            'type' => 'setting',
                        ),
                    ),
                    'css' => array(
                        drupal_get_path('module', 'color_field') . '/color_field_jquery_simple_color/color_field_jquery_simple_color.css',
                    ),
                ),
            );
            $widget += array(
                '#type' => 'textfield',
                '#default_value' => $value,
                '#size' => 7,
                '#maxlength' => 7,
            );
            break;
        case 'color_field_farbtastic_widget':
            $base = drupal_get_path('module', 'color');
            $id = 'edit-' . str_replace("_", "-", $field['field_name']) . '-' . $langcode . '-' . $delta . '-rgb';
            $settings['id'] = $id;
            $settings['picker_id'] = $id . '_picker';
            $widget += array(
                '#type' => 'textfield',
                '#description' => t('Format #FFFFFF'),
                '#default_value' => $value,
                '#size' => 7,
                '#maxlength' => 7,
                '#prefix' => '<div id=' . $settings['picker_id'] . '></div>',
                '#attached' => array(
                    // Add Farbtastic color picker.
'library' => array(
                        array(
                            'system',
                            'farbtastic',
                        ),
                    ),
                    'js' => array(
                        drupal_get_path('module', 'color_field') . '/color_field_farbtastic/color_field_farbtastic.jquery.js',
                        array(
                            'data' => array(
                                'color_field_farbtastic' => array(
                                    $id => $settings,
                                ),
                            ),
                            'type' => 'setting',
                        ),
                    ),
                    'css' => array(
                        $base . '/color.css' => array(),
                    ),
                ),
            );
            break;
        case 'color_field_plain_text':
            $widget += array(
                '#type' => 'textfield',
                '#description' => t('Format #FFFFFF'),
                '#default_value' => $value,
                '#size' => 7,
                '#maxlength' => 7,
            );
            break;
        case 'color_field_spectrum_widget':
            // Add library.
            drupal_add_library('color_field', 'bgrins-spectrum');
            $field_id = $field['field_name'] . '-' . $langcode . '-' . $delta;
            // Support field collection module.
            isset($element['#field_parents']) && !empty($element['#field_parents']) ? $bundle_id = '-' . implode('-', $element['#field_parents']) : ($bundle_id = '');
            $id = str_replace("_", "-", 'edit' . $bundle_id . '-' . $field_id . '-rgb');
            $settings['id'] = $id;
            $settings['show_input'] = isset($field['settings']['show_input']) && $field['settings']['show_input'] == 1 ? TRUE : FALSE;
            $settings['show_palette'] = isset($field['settings']['show_palette']) && $field['settings']['show_palette'] == 1 ? TRUE : FALSE;
            $settings['show_palette_only'] = isset($field['settings']['show_palette_only']) && $field['settings']['show_palette_only'] == 1 ? TRUE : FALSE;
            $settings['show_buttons'] = isset($field['settings']['show_buttons']) && $field['settings']['show_buttons'] == 1 ? TRUE : FALSE;
            $settings['allow_empty'] = isset($field['settings']['allow_empty']) && $field['settings']['allow_empty'] == 1 ? TRUE : FALSE;
            $default_colors = isset($field['settings']['palette']) && $field['settings']['palette'] != '' ? $field['settings']['palette'] : '';
            preg_match_all("/#[0-9a-fA-F]{6}/", $default_colors, $default_colors, PREG_SET_ORDER);
            foreach ($default_colors as $color) {
                $settings['palette'][] = $color[0];
            }
            $widget += array(
                '#type' => 'textfield',
                '#description' => t('Format #FFFFFF'),
                '#default_value' => $value,
                '#size' => 7,
                '#maxlength' => 7,
                '#attached' => array(
                    // Add Spectrum color picker.
'library' => array(
                        array(
                            'color_field',
                            'spectrum',
                        ),
                    ),
                    'js' => array(
                        drupal_get_path('module', 'color_field') . '/color_field_spectrum_color_picker/color_field_spectrum_color_picker.jquery.js',
                        array(
                            'data' => array(
                                'color_field_spectrum' => array(
                                    '#' . $id => $settings,
                                ),
                            ),
                            'type' => 'setting',
                        ),
                    ),
                ),
            );
            break;
        case 'color_field_default_widget':
            $field_id = str_replace("_", "-", $field['field_name']) . '-' . $langcode . '-' . $delta;
            // Support field collection module.
            isset($element['#field_parents']) ? $bundle_id = implode('-', $element['#field_parents']) : ($bundle_id = '');
            $divid = 'div-' . $field_id . '-' . $bundle_id . '-rgb';
            $id = 'edit-' . $field_id . '-' . $bundle_id . '-rgb';
            $settings['value'] = $value;
            $settings['id'] = $id;
            $default_colors = isset($field['settings']['default_colors']) ? $field['settings']['default_colors'] : '';
            preg_match_all("/#[0-9a-fA-F]{6}/", $default_colors, $default_colors, PREG_SET_ORDER);
            foreach ($default_colors as $color) {
                $settings['colors'][] = $color[0];
            }
            $widget += array(
                '#suffix' => '<label>' . $instance['label'] . '</label><div id=' . $divid . '></div><div class="description">' . $element['#description'] . '</div>',
                '#attached' => array(
                    'js' => array(
                        drupal_get_path('module', 'color_field') . '/color_field_default/color_field.js',
                        drupal_get_path('module', 'color_field') . '/color_field_default/color_field.jquery.js',
                        array(
                            'data' => array(
                                'color_field' => array(
                                    '#' . $divid => $settings,
                                ),
                            ),
                            'type' => 'setting',
                        ),
                    ),
                    'css' => array(
                        drupal_get_path('module', 'color_field') . '/color_field_default/color_field.css',
                    ),
                ),
            );
            $widget += array(
                '#attributes' => array(
                    'class' => array(
                        'element-invisible',
                    ),
                    'id' => $id,
                ),
                '#type' => 'token',
                '#default_value' => $value,
                '#size' => 7,
                '#maxlength' => 7,
            );
            break;
    }
    $element['rgb'] = $widget;
    return $element;
}