Skip to content

Commit

Permalink
Issue #3: Tablefields should not print empty tables when there is no …
Browse files Browse the repository at this point in the history
…data.
  • Loading branch information
Jen Lampton committed Feb 8, 2018
1 parent 64a41a6 commit 50c70f3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 44 deletions.
78 changes: 34 additions & 44 deletions tablefield.module
Expand Up @@ -367,26 +367,46 @@ function tablefield_field_formatter_view($entity_type, $entity, $field, $instanc
$raw = unserialize($table['value']);
$caption = isset($raw['caption']) ? check_plain($raw['caption']) : '';

$not_empty = array();
$data_not_empty = FALSE;
foreach ($table['tabledata'] as $key => $row) {
if (!empty(array_filter($row))) {
$not_empty[] = TRUE;
}
}
if (in_array(TRUE, $not_empty)) {
$data_not_empty = TRUE;
}

$not_empty = array();
$value_not_empty = FALSE;
foreach ($table['value'] as $key => $row) {
if (!empty(array_filter($row))) {
$not_empty[] = TRUE;
}
}
if (in_array(TRUE, $not_empty)) {
$value_not_empty = TRUE;
}

// Rationalize the stored data.
if (!empty($table['tabledata'])) {
if (!empty($table['tabledata']) && $data_not_empty) {
$tabledata = $table['tabledata'];
}
elseif (!empty($table['value'])) {
elseif (!empty($table['value']) && $value_not_empty) {
$tabledata = tablefield_rationalize_table(unserialize($table['value']));
}

// Run the table through input filters.
if (isset($tabledata)) {
if (!empty($tabledata)) {

if (!empty($settings['trim_trailing_rows'])) {
$tabledata = tablefield_rtrim_rows($tabledata);
}

if (!empty($settings['trim_trailing_cols'])) {
$tabledata = tablefield_rtrim_cols($tabledata);
}
if (!empty($settings['trim_trailing_rows'])) {
$tabledata = tablefield_rtrim_rows($tabledata);
}
if (!empty($settings['trim_trailing_cols'])) {
$tabledata = tablefield_rtrim_cols($tabledata);
}

if (!empty($tabledata)) {
foreach ($tabledata as $row_key => $row) {
foreach ($row as $col_key => $cell) {
if (!empty($table['format'])) {
Expand Down Expand Up @@ -427,7 +447,7 @@ function tablefield_field_formatter_view($entity_type, $entity, $field, $instanc
'#theme' => 'tablefield_view',
'#attributes' => array(
'id' => 'tablefield-' . $delta,
'class' => array('tablefield', 'tablefield-' . $delta),
'class' => array('tablefield'),
),
'#caption' => $caption,
'#header' => $header,
Expand Down Expand Up @@ -985,7 +1005,7 @@ function tablefield_rebuild_form_ajax($form, $form_state) {

// We don't want to re-send the format/_weight options.
unset($rebuild['format']);
unset($rebuild['_weight']);
//unset($rebuild['_weight']);

// We need to avoid sending headers or the multipart form
// will make it fail. So, we need to explicitly define the
Expand Down Expand Up @@ -1061,41 +1081,11 @@ function tablefield_theme() {
'langcode' => NULL,
'formatter' => NULL,
),
'file' => 'tablefield.theme.inc',
),
);
}

/**
* Theme function for table view.
*/
function theme_tablefield_view($variables) {
// Apply scope property to headers for accessibility.
if (is_array($variables['header'])) {
foreach ($variables['header'] as &$header) {
$header['scope'] = 'col';
}
}

// If the user has access to the csv export option, display it now.
$export = '';
if ($variables['export'] && user_access('export tablefield')) {
$url = sprintf('tablefield/export/%s/%s/%s/%s/%s', $variables['entity_type'], $variables['entity_id'], $variables['field_name'], $variables['langcode'], $variables['delta']);
$export = '<div id="tablefield-export-link-' . $variables['delta'] . '" class="tablefield-export-link">' . l(t('Export Table Data'), $url) . '</div>';
}

return '<div id="tablefield-wrapper-' . $variables['delta'] . '" class="tablefield-wrapper">'
. theme('table__tablefield',
array(
'header' => $variables['header'],
'rows' => $variables['rows'],
'attributes' => $variables['attributes'],
'caption' => $variables['caption'],
)
)
. $export
. '</div>';
}

/**
* Helper function to fill in locked values from instance defaults.
*
Expand Down
40 changes: 40 additions & 0 deletions tablefield.theme.inc
@@ -0,0 +1,40 @@
<?php
/**
* @file
* Theme fuunctions for table field.
*/

/**
* Theme function for table view.
*/
function theme_tablefield_view($variables) {
// Apply scope property to headers for accessibility.
if (is_array($variables['header'])) {
foreach ($variables['header'] as &$header) {
$header['scope'] = 'col';
}
}

// If the user has access to the csv export option, display it now.
$export = '';
if ($variables['export'] && user_access('export tablefield')) {
$url = sprintf('tablefield/export/%s/%s/%s/%s/%s', $variables['entity_type'], $variables['entity_id'], $variables['field_name'], $variables['langcode'], $variables['delta']);
$export = '<div id="tablefield-export-link-' . $variables['delta'] . '" class="tablefield-export-link">' . l(t('Export Table Data'), $url) . '</div>';
}

$table = array(
'header' => $variables['header'],
'rows' => $variables['rows'],
'attributes' => $variables['attributes'],
);
if(isset($variables['caption']) && !empty($variables['caption'])) {
$table['caption'] = $variables['caption'];
}

$output = '<div id="tablefield-wrapper-' . $variables['delta'] . '" class="tablefield-wrapper">';
$output .= theme('table__tablefield', $table);
$output .= $export;
$output .= '</div>';

return $output;
}

0 comments on commit 50c70f3

Please sign in to comment.