Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
with the default implementation of hook_field_formatter_view, we unfortunately got duplicates id attributes. To prevent it, I suggest to use a class attribute instead of id. And to limit inline css duplication, I suggest to use drupal_static, as it follow :
function jquery_colorpicker_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)
{
$css_attachment_cache = &drupal_static(__FUNCTION__ . '_css');
if (!isset($css_attachment_cache)) {
$css_attachment_cache = array();
}
$element = array();
foreach($items as $delta => $item)
{
if($display['type'] == 'jquery_colorpicker_color_display')
{
$element[$delta]['#markup'] = '<div class="jquery_colorpicker_color_display_' . $instance['id'] . '_' . $delta . '_' . $item['jquery_colorpicker'] . '"> </div>';
$css_attachment = array
(
'type' => 'inline',
'data' => '.jquery_colorpicker_color_display_' . $instance['id'] . '_' . $delta . '_' . $item['jquery_colorpicker'] . '{background-color:#' . $item['jquery_colorpicker'] . ';}',
);
if(!in_array($css_attachment, $css_attachment_cache))
{
$css_attachment_cache[] = $css_attachment;
$element[$delta]['#attached']['css'] = $css_attachment_cache;
}
}
elseif($display['type'] == 'jquery_colorpicker_text_display')
{
$element[$delta]['#markup'] = '<div class="jquery_colorpicker_text_display"><span class="jquery_colorpicker_hash_mark">#</span>' . $item['jquery_colorpicker'] . '</div>';
}
}
return $element;
}
It's working well in my installation. Can you give me your opinions ?
Thanks!
Comments
Comment #1
plopescHi
Thanks for your interest in the module
I have modified and committed your patch git attibution. See commit.
Your patch will be rolled out in the next JQuery Colorpicker release.
Regards