I have this code in hook_field_formatter_view() function of a module that implements an image field formatter :

function foo_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  ...
  ...
  ...
  $element['#attached']['library'][] = array('foo', 'foo');
  ...
  ...
  ...
  return $element;
}

in order to add the foo library to the nodes that contains an image field with that formatter. And it works fine but someone has found it is not working for him when using the formatter in a view. I tested with a view and works fine for me too. I have made debug on the code and this function adds the attached attribute correctly even in the reported not working site. So I wonder if some other module is overwriting that attached attribute of that image field after my module. Is the only thing I could think about.

Indeed I have changed the code a little to this:

function foo_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  ...
  ...
  ...
  foreach ($items as $delta => $item) {
    ...
    ...
    ...
    $element[$delta]['#attached']['library'][] = array('foo', 'foo');
  }
  ...
  ...
  ...
  return $element;
}

the person having the problem reports this code resolves his issue.

So, is my first code wrong? or efectively some module must be ovewriting the attached attribute while it shouldn't? Or should I use this second code instead because the first one is wrong? or are both wrong and this must be done different?

I hope this is enough to understand what the code tries to do. It adds a foo library in node only when an image field is configured to use this formatter. If you think another better way to do it please let me know.

If both of my codes are correct, I wonder if other module is overwriting, maybe calling:

['#attached']['library'] = array(....

somewhere. Instead of:

['#attached']['library'][] = array(....

to append its own library. And this is what I would like to determine too but first I need to know if my code is correct and there's other module conflicting.

Thanks in advance, regards.

Comments

Anonymous’s picture

Status: Active » Closed (won't fix)

No activity for 28 weeks, so this is probably no longer being actively worked on by the requester. It also is an issue that is very hard to debug through an issue queue.