Skip to content
Snippets Groups Projects
Commit 8abf14b9 authored by Shelane French's avatar Shelane French
Browse files

Issue #3071642 by naresh_bavaskar: Condition less than or equal and greater than or equal

Adds new options and converts from numeric based to a text based key. Refactors the output
to a single function for repeated tasks.
parent 3044ab64
No related branches found
No related tags found
No related merge requests found
......@@ -63,18 +63,20 @@ class ViewsConditionalField extends FieldPluginBase implements ContainerFactoryP
* @var array
*/
public $conditions = [
1 => 'Equal to',
2 => 'NOT equal to',
3 => 'Greater than',
4 => 'Less than',
5 => 'Empty',
6 => 'NOT empty',
7 => 'Contains',
8 => 'Does NOT contain',
9 => 'Length Equal to',
10 => 'Length NOT equal to',
11 => 'Length Greater than',
12 => 'Length Less than',
'eq' => 'Equal to',
'neq' => 'NOT equal to',
'gt' => 'Greater than',
'gte' => 'Greater than or equals',
'lt' => 'Less than',
'lte' => 'Less than or equals',
'em' => 'Empty',
'nem' => 'NOT empty',
'cn' => 'Contains',
'ncn' => 'Does NOT contain',
'leq' => 'Length Equal to',
'lneq' => 'Length NOT equal to',
'lgt' => 'Length Greater than',
'llt' => 'Length Less than',
];
/**
......@@ -194,8 +196,8 @@ class ViewsConditionalField extends FieldPluginBase implements ContainerFactoryP
}
// We using there is_numeric because values '0', '0.0' counts as empty.
if (empty($values['options']['equalto']) && !in_array($values['options']['condition'], [
5,
6,
'em',
'nem',
]) && !is_numeric($values['options']['equalto'])
) {
$form_state->setErrorByName('condition', $this->t("Please specify something to compare with."));
......@@ -211,6 +213,22 @@ class ViewsConditionalField extends FieldPluginBase implements ContainerFactoryP
return $this->options['strip_tags'] ? trim(strip_tags($unparsed)) : trim($unparsed);
}
/**
* Create renderable markup for field values.
*
* @param $value
* The value to be displayed.
*
* @return
* The rendered value.
*/
private function markup($value) {
$value = [
'#markup' => $value,
];
return \Drupal::service('renderer')->render($value);
}
/**
* {@inheritDoc}
*/
......@@ -271,194 +289,142 @@ class ViewsConditionalField extends FieldPluginBase implements ContainerFactoryP
// Run conditions.
switch ($condition) {
// Equal to.
case 1:
case 'eq':
if ($r == $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Not equal to.
case 2:
case 'neq':
if ($r !== $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Greater than.
case 3:
case 'gt':
if ($r > $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Greater than or equals.
case 'gte':
if ($r >= $equalto) {
return $then;
}
else {
return $this->markup($or);
}
break;
// Less than.
case 4:
case 'lt':
if ($r < $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
return $this->markup($or);
}
break;
// Less than or equals.
case 'lte':
if ($r <= $equalto) {
return $then;
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Empty.
case 5:
case 'em':
if (empty($r)) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Not empty.
case 6:
case 'nem':
if (!empty($r)) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Contains.
case 7:
case 'cn':
if (mb_stripos($r, $equalto) !== FALSE) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Does NOT contain.
case 8:
case 'ncn':
if (mb_stripos($r, $equalto) === FALSE) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Length Equal to.
case 9:
case 'leq':
if (mb_strlen($r) == $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Length Not equal to.
case 10:
case 'lneq':
if (mb_strlen($r) !== $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Length Greater than.
case 11:
case 'lgt':
if (mb_strlen($r) > $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
// Length Less than.
case 12:
case 'llt':
if (mb_strlen($r) < $equalto) {
$then = [
'#markup' => $then,
];
return \Drupal::service('renderer')->render($then);
return $this->markup($then);
}
else {
$or = [
'#markup' => $or,
];
return \Drupal::service('renderer')->render($or);
return $this->markup($or);
}
break;
}
......
<?php
/**
* @file
* Contains update functions for Views Conditional.
*/
/**
* Update Views Conditional condition value configuration.
*/
function views_conditional_update_8001(&$sandbox) {
$config_factory = \Drupal::configFactory();
$mappings = [
1 => 'eq',
2 => 'neq',
3 => 'gt',
4 => 'lt',
5 => 'em',
6 => 'nem',
7 => 'cn',
8 => 'ncn',
9 => 'leq',
10 => 'lneq',
11 => 'lgt',
12 => 'llt',
];
foreach ($config_factory->listAll('views.view.') as $view_config_name) {
$view = $config_factory->getEditable($view_config_name);
$save = FALSE;
$displays = $view->get('display');
foreach ($displays as $display_name => &$display) {
if (!empty($display['display_options']['fields'])) {
$fields = $display['display_options']['fields'];
foreach ($fields as $field_name => $field) {
if ($field['plugin_id'] == 'views_conditional_field') {
$condition = $field['condition'];
$display['display_options']['fields'][$field_name]['condition'] = $mappings[$condition];
\Drupal::logger('views_conditional')->notice("Updating condition setting in $field_name for $view_config_name");
$save = TRUE;
}
}
}
}
if ($save) {
$view->set('display', $displays);
$view->save(TRUE);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment