Same name and namespace in other branches
  1. 7.x-1.x color_field.module \color_field_field_formatter_view() 1 comment

Implements hook_field_formatter_view().

Three formatters are implemented.

  • color_field_default_formatter just outputs markup indicating the color that was entered and uses an inline style to set the text color to that value.
  • color_field_css_declaration does the same but also changes the background color or color of a region defined by the selector.

See also

color_field_formatter_info()

File

./color_field.field.inc, line 583

Code

function color_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
    $element = array();
    $field_settings = $field['settings'];
    $display_settings = $display['settings'];
    switch ($display['type']) {
        case 'color_field_default_formatter':
            foreach ($items as $delta => $item) {
                if ($field_settings['opacity']) {
                    $value = color_field_hex2rgba(drupal_substr($item['rgb'], 1, 6), $item['opacity']);
                }
                else {
                    $value = $item['rgb'];
                }
                $element[$delta]['#markup'] = $value;
            }
            break;
        case 'color_field_css_declaration':
            foreach ($items as $delta => $item) {
                if ($field_settings['opacity'] && $display_settings['opacity_disabled'] === 0) {
                    $value = color_field_hex2rgba(drupal_substr($item['rgb'], 1, 6), $item['opacity']);
                }
                else {
                    $value = $item['rgb'];
                }
                $selector = token_replace($display_settings['selector'], array(
                    $entity_type => $entity,
                ), array(
                    'clear' => TRUE,
                ));
                $important = $display_settings['important'] ? ' !important' : '';
                $inline_css = $selector . '{ ' . $display_settings['property'] . ': ' . $value . $important . '; }';
                drupal_add_css($inline_css, 'inline');
            }
            break;
        case 'color_field_swatch':
            foreach ($items as $delta => $item) {
                if ($field_settings['opacity']) {
                    $value = color_field_hex2rgba(drupal_substr($item['rgb'], 1, 6), $item['opacity']);
                }
                else {
                    $value = $item['rgb'];
                }
                $width = $display_settings['width'];
                $height = $display_settings['height'];
                $element[$delta] = array(
                    '#theme' => 'color_swatch',
                    '#color' => $value,
                    '#width' => $width,
                    '#height' => $height,
                );
            }
            break;
    }
    return $element;
}