You are here

function color_field_field_formatter_view in Color Field 7

Same name and namespace in other branches
  1. 7.2 color_field.field.inc \color_field_field_formatter_view()

Implements hook_field_formatter_view().

Two 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.module, line 529
An color field with a custom color picker using the Field Types API.

Code

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