Skip to content

Commit

Permalink
Applying drupal-7-count-function-deprecation-fixes-2885610-19.patch (…
Browse files Browse the repository at this point in the history
…from https://www.drupal.org/project/drupal/issues/2885610) to fix count() calls on uncountable variables.
  • Loading branch information
mwanberg committed Dec 4, 2018
1 parent ddb5544 commit 4c3efa3
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 16 deletions.
102 changes: 102 additions & 0 deletions drupal-7-count-function-deprecation-fixes-2885610-19.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
diff --git a/includes/form.inc b/includes/form.inc
index 3b2032eb72..e4ab8c8dac 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -1440,10 +1440,12 @@ function _form_validate(&$elements, &$form_state, $form_id = NULL) {
// length if it's a string, and the item count if it's an array.
// An unchecked checkbox has a #value of integer 0, different than string
// '0', which could be a valid value.
- $is_empty_multiple = (!count($elements['#value']));
+ $is_countable = is_array($elements['#value']) || $elements['#value'] instanceof \Countable;
+ $is_empty_multiple = $is_countable && count($elements['#value']) == 0;
$is_empty_string = (is_string($elements['#value']) && drupal_strlen(trim($elements['#value'])) == 0);
$is_empty_value = ($elements['#value'] === 0);
- if ($is_empty_multiple || $is_empty_string || $is_empty_value) {
+ $is_empty_null = is_null($elements['#value']);
+ if ($is_empty_multiple || $is_empty_string || $is_empty_value || $is_empty_null) {
// Although discouraged, a #title is not mandatory for form elements. In
// case there is no #title, we cannot set a form error message.
// Instead of setting no #title, form constructors are encouraged to set
diff --git a/includes/theme.inc b/includes/theme.inc
index 9b606e9fb1..9bb31d9d75 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -1995,7 +1995,7 @@ function theme_table($variables) {
$empty = $variables['empty'];

// Add sticky headers, if applicable.
- if (count($header) && $sticky) {
+ if (!empty($header) && $sticky) {
drupal_add_js('misc/tableheader.js');
// Add 'sticky-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
@@ -2009,7 +2009,7 @@ function theme_table($variables) {
}

// Format the table columns:
- if (count($colgroups)) {
+ if (!empty($colgroups)) {
foreach ($colgroups as $number => $colgroup) {
$attributes = array();

@@ -2044,38 +2044,40 @@ function theme_table($variables) {
}

// Add the 'empty' row message if available.
- if (!count($rows) && $empty) {
+ if (empty($rows) && $empty) {
$header_count = 0;
- foreach ($header as $header_cell) {
- if (is_array($header_cell)) {
- $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
- }
- else {
- $header_count++;
+ if (!empty($header)) {
+ foreach ($header as $header_cell) {
+ if (is_array($header_cell)) {
+ $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
+ }
+ else {
+ $header_count++;
+ }
}
}
$rows[] = array(array('data' => $empty, 'colspan' => $header_count, 'class' => array('empty', 'message')));
}

// Format the table header:
- if (count($header)) {
+ if (!empty($header)) {
$ts = tablesort_init($header);
// HTML requires that the thead tag has tr tags in it followed by tbody
// tags. Using ternary operator to check and see if we have any rows.
- $output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
+ $output .= (!empty($rows) ? ' <thead><tr>' : ' <tr>');
foreach ($header as $cell) {
$cell = tablesort_header($cell, $header, $ts);
$output .= _theme_table_cell($cell, TRUE);
}
// Using ternary operator to close the tags based on whether or not there are rows
- $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
+ $output .= (!empty($rows) ? " </tr></thead>\n" : "</tr>\n");
}
else {
$ts = array();
}

// Format the table rows:
- if (count($rows)) {
+ if (!empty($rows)) {
$output .= "<tbody>\n";
$flip = array('even' => 'odd', 'odd' => 'even');
$class = 'even';
@@ -2095,7 +2097,7 @@ function theme_table($variables) {
$attributes = array();
$no_striping = FALSE;
}
- if (count($cells)) {
+ if (!empty($cells)) {
// Add odd/even class
if (!$no_striping) {
$class = $flip[$class];
6 changes: 4 additions & 2 deletions includes/form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1438,10 +1438,12 @@ function _form_validate(&$elements, &$form_state, $form_id = NULL) {
// length if it's a string, and the item count if it's an array.
// An unchecked checkbox has a #value of integer 0, different than string
// '0', which could be a valid value.
$is_empty_multiple = (!count($elements['#value']));
$is_countable = is_array($elements['#value']) || $elements['#value'] instanceof \Countable;
$is_empty_multiple = $is_countable && count($elements['#value']) == 0;
$is_empty_string = (is_string($elements['#value']) && drupal_strlen(trim($elements['#value'])) == 0);
$is_empty_value = ($elements['#value'] === 0);
if ($is_empty_multiple || $is_empty_string || $is_empty_value) {
$is_empty_null = is_null($elements['#value']);
if ($is_empty_multiple || $is_empty_string || $is_empty_value || $is_empty_null) {
// Although discouraged, a #title is not mandatory for form elements. In
// case there is no #title, we cannot set a form error message.
// Instead of setting no #title, form constructors are encouraged to set
Expand Down
30 changes: 16 additions & 14 deletions includes/theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ function theme_table($variables) {
$empty = $variables['empty'];

// Add sticky headers, if applicable.
if (count($header) && $sticky) {
if (!empty($header) && $sticky) {
drupal_add_js('misc/tableheader.js');
// Add 'sticky-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
Expand All @@ -2009,7 +2009,7 @@ function theme_table($variables) {
}

// Format the table columns:
if (count($colgroups)) {
if (!empty($colgroups)) {
foreach ($colgroups as $number => $colgroup) {
$attributes = array();

Expand Down Expand Up @@ -2044,38 +2044,40 @@ function theme_table($variables) {
}

// Add the 'empty' row message if available.
if (!count($rows) && $empty) {
if (empty($rows) && $empty) {
$header_count = 0;
foreach ($header as $header_cell) {
if (is_array($header_cell)) {
$header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
}
else {
$header_count++;
if (!empty($header)) {
foreach ($header as $header_cell) {
if (is_array($header_cell)) {
$header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
}
else {
$header_count++;
}
}
}
$rows[] = array(array('data' => $empty, 'colspan' => $header_count, 'class' => array('empty', 'message')));
}

// Format the table header:
if (count($header)) {
if (!empty($header)) {
$ts = tablesort_init($header);
// HTML requires that the thead tag has tr tags in it followed by tbody
// tags. Using ternary operator to check and see if we have any rows.
$output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
$output .= (!empty($rows) ? ' <thead><tr>' : ' <tr>');
foreach ($header as $cell) {
$cell = tablesort_header($cell, $header, $ts);
$output .= _theme_table_cell($cell, TRUE);
}
// Using ternary operator to close the tags based on whether or not there are rows
$output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
$output .= (!empty($rows) ? " </tr></thead>\n" : "</tr>\n");
}
else {
$ts = array();
}

// Format the table rows:
if (count($rows)) {
if (!empty($rows)) {
$output .= "<tbody>\n";
$flip = array('even' => 'odd', 'odd' => 'even');
$class = 'even';
Expand All @@ -2095,7 +2097,7 @@ function theme_table($variables) {
$attributes = array();
$no_striping = FALSE;
}
if (count($cells)) {
if (!empty($cells)) {
// Add odd/even class
if (!$no_striping) {
$class = $flip[$class];
Expand Down

0 comments on commit 4c3efa3

Please sign in to comment.