We have run into a problem where the module renders some markup even if the field does not have any items and/or links in the suffix.
I have created a simple patch which adds a test before applying the wrapper container tag.

The goal is simple: do not display anything if there is no data. Of course it still shows the default field template markup but that can be overridden in the theme and it is out of the scope. Any suggestions, please do not hesitate to add.

CommentFileSizeAuthor
#1 wrapper-markup-1570836-1.patch1.09 KBmelon

Comments

melon’s picture

Status: Active » Needs review
StatusFileSize
new1.09 KB

Here is the patch, please review.

cwithout’s picture

The patch worked to remove the wrapper.

I'm not sure why it's out of the scope of this module for the default field template markup not to be included when the field is empty. I double checked on a fresh install that other field types do not include any markup if their values are empty, and they don't. Shouldn't that be the same for Field Collection?

cwithout’s picture

Category: feature » bug
Status: Needs review » Needs work

Looking at all the other field types, the fact that this one renders markup when the content is completely empty seems like a bug.

I realize Field Collection is slightly different because it includes an "add" link even if the fields are empty, but this definitely causes a problem when you're depending on it to render as empty in order to hide a region for users who don't have permission to add.

The reason it's rendering the the markup even if the field is empty is due to the attached CSS, prefix and suffix, so there needs to be a conditional on including those.

I've only looked at the code briefly, so somebody more familiar with this module might have some better suggestions, but here are a couple ways I can see to do that. The first has more overhead but more stand-alone and not dependent on the return of field_collection_field_formatter_links(). The second less overhead but relies on the -1 index for the element array being created when empty in field_collection_field_formatter_links().

I'm partial to the second for efficiency sake, but not being familiar with the module, I don't know if it's reliable. So I'll leave it to the maintainers to decide or determine if there is a better way.


case 'field_collection_view':

      /*CHECK WHETHER THEIR ARE ITEMS OR WHETHER THE USER HAS PERMISSION TO ADD THEM*/
      $field_collection_item = entity_create('field_collection_item', array('field_name' => $field['field_name']));
      $field_collection_item->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE);
      if (count($items) || field_collection_item_access('create', $field_collection_item)) {
        $element['#attached']['css'][] = drupal_get_path('module', 'field_collection') . '/field_collection.theme.css';
      }
      $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'full';
      foreach ($items as $delta => $item) {
        if ($field_collection = field_collection_field_get_entity($item)) {
          $element[$delta]['entity'] = $field_collection->view($view_mode);
          $element[$delta]['#theme_wrappers'] = array('field_collection_view');
          $element[$delta]['#attributes']['class'][] = 'field-collection-view';
          $element[$delta]['#attributes']['class'][] = 'clearfix';
          $element[$delta]['#attributes']['class'][] = drupal_clean_css_identifier('view-mode-' . $view_mode);

          $links = array(
            '#theme' => 'links__field_collection_view',
          );
          $links['#attributes']['class'][] = 'field-collection-view-links';
          foreach (array('edit', 'delete') as $op) {
            if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) {
              $links['#links'][$op] = array(
                'title' => entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]),
                'href' => $field_collection->path() . '/' . $op,
                'query' => drupal_get_destination(),
              );
            }
          }
          $element[$delta]['links'] = $links;
        }
      }
      field_collection_field_formatter_links($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
      break;
    case 'field_collection_view':

      $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'full';
      foreach ($items as $delta => $item) {
        if ($field_collection = field_collection_field_get_entity($item)) {
          $element[$delta]['entity'] = $field_collection->view($view_mode);
          $element[$delta]['#theme_wrappers'] = array('field_collection_view');
          $element[$delta]['#attributes']['class'][] = 'field-collection-view';
          $element[$delta]['#attributes']['class'][] = 'clearfix';
          $element[$delta]['#attributes']['class'][] = drupal_clean_css_identifier('view-mode-' . $view_mode);

          $links = array(
            '#theme' => 'links__field_collection_view',
          );
          $links['#attributes']['class'][] = 'field-collection-view-links';
          foreach (array('edit', 'delete') as $op) {
            if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) {
              $links['#links'][$op] = array(
                'title' => entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]),
                'href' => $field_collection->path() . '/' . $op,
                'query' => drupal_get_destination(),
              );
            }
          }
          $element[$delta]['links'] = $links;
        }
      }
      field_collection_field_formatter_links($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);

      /*CHECK HERE IF THE -1 INDEX WAS CREATED*/      
      if(!isset($element[-1])) {
        $element['#attached']['css'][] = drupal_get_path('module', 'field_collection') . '/field_collection.theme.css';
      }
      break;
RobW’s picture

This will also be fixed in #1157794: Move markup to template files and improve theming in new 2.x branch. Should have a working patch with all sorts of theme improvements including all markup in tpls, none in the module itself, in a couple of days.

RobW’s picture

Status: Needs work » Closed (duplicate)

Marking this as a duplicate of #1276258: Completely hide empty field collections. There's been work going on there for a while on this problem. Feel free to bring your patch over there though.