Same filename and directory in other branches
  1. 7.x-1.x color_field.module 1 comment
  2. 7.x-2.x color_field.module 1 comment
  3. 8.x-1.x color_field.module 1 comment
  4. 8.x-2.x color_field.module 1 comment

File

./color_field.module

View source
<?php


/**
 * @file
 * A color field with a custom color picker using the Field Types API.
 */
declare (strict_types=1);
use Drupal\color_field\ColorHex;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function color_field_help(string $route_name, RouteMatchInterface $route_match) {
    switch ($route_name) {
        case 'help.page.color_field':
            $output = '';
            $output .= '<h3>' . t('About') . '</h3>';
            $output .= '<p>' . t('Color Field is simple field that use a hexadecimal notation (HEX) for the combination of Red, Green, and Blue color values (RGB). See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":link_documentation">online documentation for the Link module</a>.', [
                ':field' => Url::fromRoute('help.page', [
                    'name' => 'field',
                ])->toString(),
                ':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
                    'name' => 'field_ui',
                ])->toString() : '#',
                ':link_documentation' => 'https://drupal.org/documentation/modules/link',
            ]) . '</p>';
            $output .= '<h3>' . t('Uses') . '</h3>';
            $output .= '<dl>';
            $output .= '<dt>' . t('Managing and displaying color fields') . '</dt>';
            $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the link field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [
                ':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
                    'name' => 'field_ui',
                ])->toString() : '#',
            ]) . '</dd>';
            $output .= '<dt>' . t('Adding link text') . '</dt>';
            $output .= '<dd>' . t('In the field settings you can define additional link text to be <em>optional</em> or <em>required</em> in any link field.') . '</dd>';
            $output .= '<dt>' . t('Displaying link text') . '</dt>';
            $output .= '<dd>' . t('If link text has been submitted for a URL, then by default this link text is displayed as a link to the URL. If you want to display both the link text <em>and</em> the URL, choose the appropriate link format from the drop-down menu in the <em>Manage display</em> page. If you only want to display the URL even if link text has been submitted, choose <em>Link</em> as the format, and then change its <em>Format settings</em> to display <em>URL only</em>.') . '</dd>';
            $output .= '<dt>' . t('Adding attributes to links') . '</dt>';
            $output .= '<dd>' . t('You can add attributes to links, by changing the <em>Format settings</em> in the <em>Manage display</em> page. Adding <em>rel="nofollow"</em> notifies search engines that links should not be followed.') . '</dd>';
            $output .= '<dt>' . t('Validating URLs') . '</dt>';
            $output .= '<dd>' . t('All links are validated after a link field is filled in. They can include anchors or query strings.') . '</dd>';
            $output .= '</dl>';
            return $output;
    }
}

/**
 * Implements hook_theme().
 */
function color_field_theme() : array {
    $theme = [];
    $theme['color_field_formatter_swatch'] = [
        'variables' => [
            'shape' => NULL,
            'color' => NULL,
            'width' => NULL,
            'height' => NULL,
            'attributes' => NULL,
        ],
    ];
    $theme['color_field_formatter_swatch_option'] = [
        'variables' => [
            'id' => NULL,
            'name' => NULL,
            'input_type' => NULL,
            'value' => NULL,
            'shape' => NULL,
            'color' => NULL,
            'width' => NULL,
            'height' => NULL,
            'attributes' => NULL,
        ],
    ];
    return $theme;
}

/**
 * Implements hook_token_info().
 */
function color_field_token_info() : array {
    $tokens = [];
    $tokens['color_field']['hex'] = [
        'name' => t('HEX'),
        'description' => t("Hex style color values."),
        'type' => 'hex',
    ];
    $tokens['hex']['with_opacity'] = [
        'name' => t('With Opacity'),
        'description' => t('HEX style color with opacity'),
    ];
    $tokens['color_field']['rgb'] = [
        'name' => t('RGB'),
        'description' => t("RGB style color values."),
        'type' => 'rgb',
    ];
    $tokens['rgb']['rgba'] = [
        'name' => t('RGBA'),
        'description' => t("Color value expressed as rgba(xx,xx,xx,x.x)."),
    ];
    $tokens['rgb']['red'] = [
        'name' => t('Red'),
        'description' => t("RGB Color value for red only expressed as an int."),
    ];
    $tokens['rgb']['blue'] = [
        'name' => t('Blue'),
        'description' => t("RGB Color value for blue only expressed as an int."),
    ];
    $tokens['rgb']['green'] = [
        'name' => t('Green'),
        'description' => t("RGB Color value for green only expressed as an int."),
    ];
    $tokens['rgb']['opacity'] = [
        'name' => t('Opacity'),
        'description' => t("RGB Color value for opacity only expressed as a float."),
    ];
    return [
        'types' => [
            'color_field' => [
                'name' => t('Color Field'),
                'description' => t('Values from a specific color field item.'),
                'needs-data' => 'color_field',
            ],
            'rgb' => [
                'name' => t('RGB'),
                'description' => t('RGB Values from a specific color field item.'),
                'needs-data' => 'color_field',
            ],
            'hex' => [
                'name' => t('HEX'),
                'description' => t('HEX values from a specific color field item'),
                'needs-data' => 'color_field',
            ],
        ],
        'tokens' => $tokens,
    ];
}

/**
 * Implements hook_tokens().
 */
function color_field_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) : array {
    $replacements = [];
    if ($type === 'color_field' && !empty($data['color_field'])) {
        
        /** @var \Drupal\color_field\Plugin\Field\FieldType\ColorFieldType $color_field */
        $color_field = $data['color_field'];
        $color_hex = new ColorHex($color_field->color, $color_field->opacity);
        foreach ($tokens as $name => $original) {
            switch ($name) {
                case 'hex':
                    $replacements[$original] = $color_hex->toString(FALSE);
                    break;
                case 'hex:with_opacity':
                    $replacements[$original] = $color_hex->toString(TRUE);
                    break;
                case 'rgb':
                    $replacements[$original] = $color_hex->toRgb()
                        ->toString(FALSE);
                    break;
                case 'rgba':
                    $replacements[$original] = $color_hex->toRgb()
                        ->toString(TRUE);
                    break;
                case 'rgb:red':
                    $replacements[$original] = $color_hex->toRgb()
                        ->getRed();
                    break;
                case 'rgb:blue':
                    $replacements[$original] = $color_hex->toRgb()
                        ->getBlue();
                    break;
                case 'rgb:green':
                    $replacements[$original] = $color_hex->toRgb()
                        ->getGreen();
                    break;
                case 'rgb:opacity':
                    $replacements[$original] = $color_hex->toRgb()
                        ->getOpacity();
                    break;
            }
        }
    }
    return $replacements;
}

/**
 * Implements hook_content_export_field_value_alter().
 *
 * Include the color_field in exports by the single_content_sync module.
 */
function color_field_content_export_field_value_alter(mixed &$value, FieldItemListInterface $field) : void {
    switch ($field->getFieldDefinition()
        ->getType()) {
        case 'color_field_type':
            $value[] = [
                'color' => $field->color,
            ];
            break;
    }
}

Functions

Title Deprecated Summary
color_field_content_export_field_value_alter Implements hook_content_export_field_value_alter().
color_field_help Implements hook_help().
color_field_theme Implements hook_theme().
color_field_tokens Implements hook_tokens().
color_field_token_info Implements hook_token_info().