function extended_file_field_field_formatter_view in Extended File Field 7
Implements hook_field_formatter_view().
Generates the extended_file_field render array for the field's value. May also generate a second table for any 'hidden' files, optionally contained within a fieldset, depending on the value of $settings['showhidden'].
Parameters
$entity_type: The type of $entity.
$entity: The entity being displayed.
$field: The field structure.
$instance: The field instance.
$langcode: The language associated with $items.
$items: Array of file values for this field
$display: The display settings to use, as found in the 'display' entry of instance definitions. Contains keys of 'type' (name of the formatter to use) and 'settings' (the array of formatter settings).
Return value
array A renderable array for the $items, as an array of child elements.
Related topics
File
- ./
extended_file_field.module, line 503 - Extends the core File field widget and provides a new formatter.
Code
function extended_file_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$metadata = extended_file_field_metadata();
$settings = $display['settings'];
$elements = array();
// Remove any NULL values from the $items array.
$items = array_filter($items);
if (!empty($items)) {
// Re-index array, keying by fid
$files = array();
foreach ($items as $item) {
$files[$item['fid']] = $item;
}
$items = $files;
unset($files);
// Populate the 'allowed extension types' array
$extension_filter = extended_file_field_extension_filter($settings);
// Add file extensions
extended_file_field_add_extensions($items);
// Apply "file extensions" treatment according to the formatter settings
if (!empty($extension_filter)) {
extended_file_field_filter_by_extension($items, $extension_filter, $settings['extensionbehavior']);
}
// Apply "display = 0" treatment according to the formatter settings
if (in_array($settings['showhidden'], array(
'exclude',
'hide',
'table',
))) {
extended_file_field_filter_by_display($items, $settings['showhidden']);
}
}
// Allow other modules to alter the $items array.
$context = array(
'field' => $field,
'instance' => $instance,
'entity' => $entity,
'entity_type' => $entity_type,
'langcode' => $langcode,
'display' => $display,
);
drupal_alter('extended_file_field_items', $items, $context);
if (!empty($items)) {
// Re-order the items array according to the desired sort order.
if ($settings['sortby'] == 'reverse') {
$items = array_reverse($items, TRUE);
}
elseif ($settings['sortby'] != 'default') {
extended_file_field_sort($items, $settings['sortby'], $settings['sortorder']);
}
// Apply "Number of items" treatment according to the formatter settings
if ($settings['restrictdisplay'] != '-1' && count($items) > $settings['restrictdisplay']) {
$counter1 = $counter2 = 0;
foreach ($items as $key => $item) {
$counter = empty($item['separate-table']) ? $counter1 : $counter2;
if ($counter < $settings['restrictdisplay'] && $item['display'] && empty($item['hide-extension']) && empty($item['hide-display'])) {
if (empty($item['separate-table'])) {
$counter1++;
}
else {
$counter2++;
}
}
elseif ($item['display'] == 1) {
if (!empty($settings['restrictbehavior']) && $settings['restrictbehavior'] == 'exclude') {
unset($items[$key]);
}
else {
$items[$key]['hide-count'] = TRUE;
}
}
}
}
}
$upload_link = array();
// Even though we'll want this to be the last thing in our $elements, since
// we'll bail early if there are no files, start by rendering the link to
// upload more files if we're configured to display it. We'll stuff this
// into $elements at the appropriate moment later in this function.
// We don't render the link in preview mode, due to the hook_entity_access()
// call causing PHP notices within entity_metadata_no_hook_node_access() as
// identified in http://drupal.org/node/2125091.
if (!empty($settings['showuploadlink']) && empty($entity->in_preview) && extended_file_field_entity_access('update', $entity_type, $entity) && field_access('update', $field, $entity_type, $entity) && extended_file_field_entity_type_supports_edit_link($entity_type)) {
// @todo: This is evil. Entities should have to tell us their edit URI.
// This hack here only works on certain entity_types.
// @see https://drupal.org/node/1970360
$uri = entity_uri($entity_type, $entity);
$uri['path'] .= '/edit';
// @todo: Files aren't translatable, so I think it's safe to hard-code
// '-und' in this fragment, but it'd be nice to confirm that. Also, is
// there a cleaner way to get an appropriate ID for a field?
$uri['options'] = array(
'fragment' => 'edit-' . str_replace('_', '-', $field['field_name']) . '-und',
);
$upload_link = array(
'#theme' => 'extended_file_field_upload_link',
'#uri' => $uri,
'#text' => t('Upload new files'),
// @todo: Maybe the theme function doesn't need any of this.
'#entity_type' => $entity_type,
'#entity' => $entity,
'#field' => $field,
);
}
// If the $items array is empty, bail now.
if (empty($items)) {
return $upload_link;
}
// Assemble the table header.
$header = extended_file_field_header($settings['columns']);
// Assemble the table rows. Returns rows sorted into an array with two keys,
// 'visible' and 'hidden'.
$rows = extended_file_field_generate_rows($items, $context);
// Generate the file formatter table
if (!empty($rows['visible'])) {
$table_id = check_plain("extended-file-field-table-" . str_replace('_', '-', $field['field_name']));
$table = extended_file_field_generate_table($header, $rows['visible'], $table_id);
// Add the javascript toggle if necessary
if (!empty($settings['restrictbehavior']) && $settings['restrictbehavior'] == 'toggle') {
$table['#attached']['js'][] = drupal_get_path('module', 'extended_file_field') . '/extended_file_field.formatter.js';
}
$elements[] = $table;
}
// If a second table for 'hidden' files is required, generate it
if (!empty($rows['hidden'])) {
$table_id = check_plain("extended-file-field-table-" . str_replace('_', '-', $field['field_name']) . "-hidden");
$table = extended_file_field_generate_table($header, $rows['hidden'], $table_id);
if ($settings['usefieldset'] == TRUE) {
// Wrap the table in a collapsed fieldset if desired
$num_hidden_files = count($rows['hidden']);
$elements[] = array(
'fieldset' => array(
'#type' => 'fieldset',
'#title' => format_plural($num_hidden_files, '1 more file', '@count more files'),
'#attributes' => array(
'class' => array(
'collapsible',
'collapsed',
),
),
'content' => array(
$table,
),
),
'#attached' => array(
'js' => array(
'misc/collapse.js',
'misc/form.js',
),
),
);
}
else {
// Create a field header and add the 'hidden' file table to $elements.
$elements[] = array(
'#markup' => t('Filtered files'),
);
$elements[] = $table;
}
}
// Add the upload link if it exists.
if (!empty($upload_link)) {
$elements[] = $upload_link;
}
// Add our $items array to the context variable
$context = array(
'items' => $items,
) + $context;
// Allow other modules to alter the table render arrays.
drupal_alter('extended_file_field_output', $elements, $context);
return $elements;
}