Skip to content
Snippets Groups Projects
Commit c6cdbecc authored by Mike Feranda's avatar Mike Feranda Committed by Mike Feranda
Browse files

Issue #3161356 by mferanda: Suppression - Add ability to automatically suppress other cells

parent d1900d2a
No related branches found
No related tags found
No related merge requests found
field.formatter.settings.html_formatter_type:
type: mapping
label: 'HTML Formatter Suppression override'
mapping:
suppression_override:
type: boolean
label: 'Suppression override'
...@@ -11,7 +11,7 @@ use Drupal\Core\Field\FormatterBase; ...@@ -11,7 +11,7 @@ use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
/** /**
* Plugin implementation of the 'aggrid_formatter_type' formatter. * Plugin implementation of the 'html_formatter_type' formatter.
* *
* @FieldFormatter( * @FieldFormatter(
* id = "html_formatter_type", * id = "html_formatter_type",
...@@ -28,6 +28,7 @@ class HtmlFormatterType extends FormatterBase { ...@@ -28,6 +28,7 @@ class HtmlFormatterType extends FormatterBase {
*/ */
public static function defaultSettings() { public static function defaultSettings() {
return [ return [
'suppression_override' => false,
// Implement default settings. // Implement default settings.
] + parent::defaultSettings(); ] + parent::defaultSettings();
} }
...@@ -36,9 +37,18 @@ class HtmlFormatterType extends FormatterBase { ...@@ -36,9 +37,18 @@ class HtmlFormatterType extends FormatterBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function settingsForm(array $form, FormStateInterface $form_state) { public function settingsForm(array $form, FormStateInterface $form_state) {
return [
// Implement settings form. $form = parent::settingsForm($form, $form_state);
] + parent::settingsForm($form, $form_state);
$form['suppression_override'] = [
'#type' => 'checkbox',
'#title' => $this->t('Suppression override'),
'#description' => $this->t('If enabled, suppression will be ignored'),
'#default_value' => $this->getSetting('suppression_override'),
];
return $form;
} }
/** /**
...@@ -47,6 +57,9 @@ class HtmlFormatterType extends FormatterBase { ...@@ -47,6 +57,9 @@ class HtmlFormatterType extends FormatterBase {
public function settingsSummary() { public function settingsSummary() {
$summary = []; $summary = [];
// Implement settings summary. // Implement settings summary.
if ($override = $this->getSetting('suppression_override') ? 'Yes' : 'No') {
$summary[] = $this->t('Suppression override: @suppover', ['@suppover' => $override]);
}
return $summary; return $summary;
} }
...@@ -74,16 +87,107 @@ class HtmlFormatterType extends FormatterBase { ...@@ -74,16 +87,107 @@ class HtmlFormatterType extends FormatterBase {
return $rowSettings; return $rowSettings;
} }
/**
* @param $rowSettings
* @param $headers
* @param $rowData
* @return mixed
*/
public function processSuppression($rowSettings, $headers, $rowData) {
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();
$settings = $this->getSettings();
// Get suppression override setting
if (!empty($settings['suppression_override'])) {
$suppOverride = $settings['suppression_override'];
}
/*
* Basic level 1 suppression
*
* This will review the items and check settings for anything marked as
* suppressed. Those cells will be immediately suppressed.
*
* Level 1 includes items that are marked for other suppression.
*
*/
// Suppression variables
$suppCell = [];
$suppCount = [];
if (is_array($rowData)) {
for ($i = 0; $i < count($rowData); $i++) {
// Each row... then each cell in each row.
foreach ($headers as $field) {
// Check if suppression is enabled for cell or if the cell is already marked for suppression
$suppCellProcess = (isset($rowData[$i]->$field)
&& is_numeric($rowData[$i]->$field)
&& ((isset($rowSettings[$i][$field]['valueSuppression']['min']) && $rowData[$i]->$field >= $rowSettings[$i][$field]['valueSuppression']['min']
&& isset($rowSettings[$i][$field]['valueSuppression']['max']) && $rowData[$i]->$field <= $rowSettings[$i][$field]['valueSuppression']['max']
|| isset($rowSettings[$i][$field]['valueSuppression']['any']) && $rowSettings[$i][$field]['valueSuppression']['any']))
&& (isset($rowSettings[$i][$field]['valueSuppression']['role']) && !empty(array_intersect($rowSettings[$i][$field]['valueSuppression']['role'], $roles))
|| !isset($rowSettings[$i][$field]['valueSuppression']['role']))
|| in_array($suppCell[$i], $field));
// Check if suppression and for role. If suppressed, add it to suppression list.
if ($suppCellProcess) {
// Add the current cell to the suppression
if (!in_array($suppCell[$i], $field)) {
array_push($suppCell, [$i => $field]);
}
// Additional Suppression
if (isset($rowSettings[$i][$field]['valueSuppression']['otherRowCol'])) {
// Set the other Row/Col from setting
$otherRowCol = $rowSettings[$i][$field]['valueSuppression']['otherRowCol'];
// Loop through other Row/Column and set
foreach($otherRowCol as $otherRow => $value) {
foreach($value as $key => $otherField) {
if (!in_array($suppCell[$otherRow], $otherField)) {
array_push($suppCell, [$otherRow => $otherField]);
}
}
}
}
}
}
}
}
// Process actual suppressions
if (!$suppOverride) {
foreach($suppCell as $key => $value) {
foreach($value as $row => $field) {
$rowData[$row]->$field = '+';
}
}
}
return $rowData;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createAggridRowData($rowSettings, $headers, $rowData) { public function createAggridRowData($rowSettings, $headers, $rowData) {
// Get user. // Get user.
$current_user = \Drupal::currentUser(); $current_user = \Drupal::currentUser();
$roles = $current_user->getRoles(); $roles = $current_user->getRoles();
// Cycle through suppression multiple times
if (is_array($rowData)) {
for ($i = 0; $i < 4; $i++) {
$rowData = $this->processSuppression($rowSettings, $headers, $rowData);
}
}
// HTML rendering
$table_render = ''; $table_render = '';
$spanSkip[][] = 0; $spanSkip[][] = 0;
if (is_array($rowData)) { if (is_array($rowData)) {
...@@ -148,25 +252,9 @@ class HtmlFormatterType extends FormatterBase { ...@@ -148,25 +252,9 @@ class HtmlFormatterType extends FormatterBase {
} else { } else {
$cellScope = ''; $cellScope = '';
} }
// Check if suppression and for role. If suppressed, send a '+' as value.
if (isset($rowData[$i]->$field)
&& is_numeric($rowData[$i]->$field)
&& ((isset($rowSettings[$i][$field]['valueSuppression']['min']) && $rowData[$i]->$field >= $rowSettings[$i][$field]['valueSuppression']['min']
&& isset($rowSettings[$i][$field]['valueSuppression']['max']) && $rowData[$i]->$field <= $rowSettings[$i][$field]['valueSuppression']['max']
|| isset($rowSettings[$i][$field]['valueSuppression']['any']) && $rowSettings[$i][$field]['valueSuppression']['any'])
&& isset($rowSettings[$i][$field]['valueSuppression']['role']) && !empty(array_intersect($rowSettings[$i][$field]['valueSuppression']['role'], $roles)))
) {
$fieldValue = '+';
}
elseif (isset($rowData[$i]->$field)) {// Check if there is data.
$fieldValue = $rowData[$i]->$field;
} else {// If not, send blank variable.
$fieldValue = '';
}
// Finally, display the cell. // Finally, display the cell.
$table_render .= '<td ' . $cellScope . ' rowspan="' . $rowSpan . '" colspan="' . $colSpan . '" class="' . $cellClass . '">' . $fieldValue . '</td>'; $table_render .= '<td ' . $cellScope . ' rowspan="' . $rowSpan . '" colspan="' . $colSpan . '" class="' . $cellClass . '">' . $rowData[$i]->$field . '</td>';
} elseif ($spanSkip[$i][$colCount] > 0) { } elseif ($spanSkip[$i][$colCount] > 0) {
// No need to render the cell. // No need to render the cell.
} else { } else {
...@@ -188,7 +276,7 @@ class HtmlFormatterType extends FormatterBase { ...@@ -188,7 +276,7 @@ class HtmlFormatterType extends FormatterBase {
$elements = []; $elements = [];
$aggridDefault = []; $aggridDefault = [];
$gsRowSettingsDefault = []; $gsRowSettingsDefault = [];
// Get config for aggrid. // Get config for aggrid.
$config = \Drupal::config('aggrid.general'); $config = \Drupal::config('aggrid.general');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment