You are here

function field_collection_field_formatter_links in Field collection 7

Helper function to add links to a field collection field.

1 call to field_collection_field_formatter_links()
field_collection_field_formatter_view in ./field_collection.module
Implements hook_field_formatter_view().

File

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

Code

function field_collection_field_formatter_links(&$element, $entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $allow_create_item = FALSE;
  if ($settings['add'] && ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || count($items) < $field['cardinality'])) {

    // Check whether the current is allowed to create a new item.
    $field_collection_item = entity_create('field_collection_item', array(
      'field_name' => $field['field_name'],
    ));
    $field_collection_item
      ->setHostEntity($entity_type, $entity, $langcode, FALSE);
    if (entity_access('create', 'field_collection_item', $field_collection_item)) {
      $allow_create_item = TRUE;
      $path = field_collection_field_get_path($field);
      list($id) = entity_extract_ids($entity_type, $entity);
      $element['#suffix'] = '';
      if ($entity_type !== 'node' && !empty($settings['description'])) {
        $element['#suffix'] .= '<div class="description field-collection-description">' . field_filter_xss($instance['description']) . '</div>';
      }
      $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_add", $settings['add']);
      $add_path = $path . '/add/' . $entity_type . '/' . $id;
      $element['#suffix'] .= '<ul class="action-links action-links-field-collection-add"><li>' . l($title, $add_path, array(
        'query' => drupal_get_destination(),
      )) . '</li></ul>';
    }
  }

  // If there is no add link, add a special class to the last item.
  if (!empty($items) || $allow_create_item) {
    if (empty($element['#suffix'])) {
      $index = count(element_children($element)) - 1;
      $element[$index]['#attributes']['class'][] = 'field-collection-view-final';
    }
    $element += array(
      '#prefix' => '',
      '#suffix' => '',
    );
    $element['#prefix'] .= '<div class="field-collection-container clearfix">';
    $element['#suffix'] .= '</div>';
  }
  return $element;
}