You are here

function field_collection_field_formatter_view in Field collection 7

Implements hook_field_formatter_view().

File

./field_collection.module, line 1047
Module implementing field collection field type.

Code

function field_collection_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  switch ($display['type']) {
    case 'field_collection_list':
      foreach ($items as $delta => $item) {
        if ($field_collection = field_collection_field_get_entity($item)) {
          $output = l($field_collection
            ->label(), $field_collection
            ->path());
          $links = array();
          foreach (field_collection_get_operations($settings) as $op => $label) {
            if ($settings[$op] && entity_access($op === 'edit' ? 'update' : $op, 'field_collection_item', $field_collection)) {
              $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_{$op}", $settings[$op]);
              $links[] = l($title, $field_collection
                ->path() . '/' . $op, array(
                'query' => drupal_get_destination(),
              ));
            }
          }
          if ($links) {
            $output .= ' (' . implode('|', $links) . ')';
          }
          $element[$delta] = array(
            '#markup' => $output,
          );
        }
      }
      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) {

        // Existing field collections should be loaded without cache so that
        // node Preview will work for both existing nodes and new ones.
        if (isset($item['entity']->item_id)) {

          // Load without cache.
          $field_collection = field_collection_item_load($item['entity']->item_id, TRUE);
        }
        else {
          $field_collection = field_collection_field_get_entity($item);
        }
        if ($field_collection) {
          $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 (field_collection_get_operations($settings) as $op => $label) {
            if ($settings[$op] && entity_access($op === 'edit' ? 'update' : $op, 'field_collection_item', $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);
      if (!empty($items) || !empty($element['#suffix'])) {
        $element['#attached']['css'][] = drupal_get_path('module', 'field_collection') . '/field_collection.theme.css';
      }
      break;
    case 'field_collection_fields':
      $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);
        }
      }
      break;
  }
  return $element;
}