Logical operator

Created on 7 October 2020, over 4 years ago
Updated 4 April 2025, 17 days ago

Hi everybody,
After a lot of migrate test, I discover a little missing feature.

Problem/Motivation

By default, with multiple condition on a field, it's the logical "AND" which is applied. But on my D7 Webform I have logical "or", which is not supported yet.

Steps to reproduce

Have a field with multiple conditions but, only one of these conditions could be TRUE to display the field (OR). It's just an example.

Proposed resolution

My solution is maybe a little ugly, I'm open to suggestion to improve this code.
They'r 2 steps, first in build the condition and second, write states in yaml file:

        if (!$unsupported_condition) {
          // Add indicator that we are a logical 'or' condition
          if($condition['andor'] == 'or'){
            $states[$element_state][] = [
              ':input[name="' . $depedent['form_key'] . '"]' => [
                'element' => $element_condition,
                'logical' => 'or'
              ]
            ];
          }else{
            // Actual building condition (AND is the default, so we don't need to specify)
            $states[$element_state][] = [':input[name="' . $depedent['form_key'] . '"]' => $element_condition];

          }

In second time :

             if($item['logical'] === 'or'){

                $markup .= "$indent      " . str_replace("\n","",Yaml::dump([$name], 4, 2)). ":\n";
                foreach (explode("\n", Yaml::dump($item['element'], 2, 2)) as $line) {
                  $markup .= "$indent          " . $line. "\n";
                }
                $markup .= "$indent      " . Yaml::dump(['or'], 4, 2);// . "\n";
              }
              else{
                // Actual yaml write condition
                $markup .= "$indent      " . Yaml::dump($name, 2, 2) . ":\n";
                foreach (explode("\n", Yaml::dump($item, 2, 2)) as $line) {
                  $markup .= "$indent        " . $line. "\n";
                }
              }

Yes, it's not very beautiful but it's the only way that I am found to write yaml like this

      - ':input[name="23_1_nouvelle_raison_sociale"]':
          filled: true
      - or
      - ':input[name="23_1_nouveau_numeros_de_siret"]':
          filled: true
      - or
      - ':input[name="23_1_nouveau_code_ape"]':
          filled: true
      - or
      - ':input[name="nouvelle_ccn"]':
          filled: true

instead of this

      ':input[name="23_1_nouvelle_raison_sociale"]':
        filled: true
      or
      ':input[name="23_1_nouveau_numeros_de_siret"]':
        filled: true
      or
      :input[name="23_1_nouveau_code_ape"]':
        filled: true
      or
      ':input[name="nouvelle_ccn"]':
        filled: true

Which is not working and through a yaml parser error by the way.

Again, if it can be improve it will be nice. Furthermore I haven't add the XOR logical, but I believe it wasn't possible in D7 webform, but Im'n not sure.

Have a nice day :-)

Feature request
Status

Needs work

Version

2.0

Component

Code

Live updates comments and jobs are added and updated live.
  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024