You are here

protected function D7Webform::buildConditionals in Webform: Migrate 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/migrate/source/d7/D7Webform.php \Drupal\webform_migrate\Plugin\migrate\source\d7\D7Webform::buildConditionals()

Build conditionals and translate them to states api in D8.

1 call to D7Webform::buildConditionals()
D7Webform::buildFormElements in src/Plugin/migrate/source/d7/D7Webform.php
Build form elements from webform component table.

File

src/Plugin/migrate/source/d7/D7Webform.php, line 576

Class

D7Webform
Drupal 7 webform source from database.

Namespace

Drupal\webform_migrate\Plugin\migrate\source\d7

Code

protected function buildConditionals($element, $elements) {
  $nid = $element['nid'];
  $cid = $element['cid'];
  $extra = unserialize($element['extra']);

  // Checkboxes : ':input[name="add_more_locations_24[yes]"]':
  $query = $this
    ->select('webform_conditional', 'wc');
  $query
    ->innerJoin('webform_conditional_actions', 'wca', 'wca.nid=wc.nid AND wca.rgid=wc.rgid');
  $query
    ->innerJoin('webform_conditional_rules', 'wcr', 'wcr.nid=wca.nid AND wcr.rgid=wca.rgid');
  $query
    ->fields('wc', [
    'nid',
    'rgid',
    'andor',
    'weight',
  ])
    ->fields('wca', [
    'aid',
    'target_type',
    'target',
    'invert',
    'action',
    'argument',
  ])
    ->fields('wcr', [
    'rid',
    'source_type',
    'source',
    'operator',
    'value',
  ]);
  $conditions = $query
    ->condition('wc.nid', $nid)
    ->condition('wca.target', $cid)
    ->execute();
  $states = [];
  if (!empty($conditions)) {
    foreach ($conditions as $condition) {
      $unsupported_condition = FALSE;

      // Element states.
      switch ($condition['action']) {
        case 'show':
          $element_state = $condition['invert'] ? 'invisible' : 'visible';
          break;
        case 'require':
          $element_state = $condition['invert'] ? 'optional' : 'required';
          break;
        case 'set':

          // Nothing found in D8 :(.
          $unsupported_condition = TRUE;
          break;
      }

      // Condition states.
      $operator_value = $condition['value'];
      $depedent = $elements[$condition['source']];
      $depedent_extra = unserialize($depedent['extra']);
      $depedent_extra['items'] = !empty($depedent_extra['items']) ? explode("\n", $depedent_extra['items']) : [];
      $depedent_extra += [
        'aslist' => NULL,
        'multiple' => NULL,
      ];

      // Element condition must be an array in Drupal 8|9 Webform.
      $element_condition = [];
      switch ($condition['operator']) {
        case 'contains':
          $element_trigger = $condition['invert'] ? '!pattern' : 'pattern';
          $element_condition = [
            'value' => [
              $element_trigger => $operator_value,
            ],
          ];

          // Specially handle the checkboxes.
          if ($depedent['type'] == 'select' && !$depedent_extra['aslist'] && $depedent_extra['multiple']) {
            $element_condition = [
              'checked' => !empty($condition['invert']),
            ];
          }
          break;
        case 'equal':
          $element_condition = [
            'value' => $operator_value,
          ];
          if ($depedent['type'] == 'select' && !$depedent_extra['aslist'] && $depedent_extra['multiple']) {
            $element_condition = [
              'checked' => TRUE,
            ];
          }
          break;
        case 'not_equal':

          // There is no handler for this in D8 so we do the reverse.
          $element_state = $condition['invert'] ? 'visible' : 'invisible';
          $element_condition = [
            'value' => $operator_value,
          ];

          // Specially handle the checkboxes.
          if ($depedent['type'] == 'select' && !$depedent_extra['aslist'] && $depedent_extra['multiple']) {
            $element_condition = [
              'checked' => TRUE,
            ];
          }
          break;
        case 'less_than':
          $element_condition = [
            'value' => [
              'less' => $operator_value,
            ],
          ];
          break;
        case 'less_than_equal':
          $element_condition = [
            'value' => [
              'less_equal' => $operator_value,
            ],
          ];
          break;
        case 'greater_than':
          $element_condition = [
            'value' => [
              'greater' => $operator_value,
            ],
          ];
          break;
        case 'greater_than_equal':
          $element_condition = [
            'value' => [
              'greater_equal' => $operator_value,
            ],
          ];
          break;
        case 'empty':
          if ($operator_value == 'checked') {
            $element_condition = [
              'unchecked' => TRUE,
            ];
          }
          else {
            $element_condition = [
              'empty' => TRUE,
            ];
          }
          break;
        case 'not_empty':
          if ($operator_value == 'checked') {
            $element_condition = [
              'checked' => TRUE,
            ];
          }
          else {
            $element_condition = [
              'filled' => FALSE,
            ];
          }
          break;
      }
      if (!$depedent_extra['aslist'] && $depedent_extra['multiple'] && is_array($depedent_extra['items']) && count($depedent_extra['items']) > 1) {
        $depedent['form_key'] = $depedent['form_key'] . "[{$operator_value}]";
      }
      elseif (!$depedent_extra['aslist'] && !$depedent_extra['multiple'] && is_array($depedent_extra['items']) && count($depedent_extra['items']) == 1) {
        $depedent['form_key'] = $depedent['form_key'] . "[{$operator_value}]";
      }
      if (!$unsupported_condition) {
        $states[$element_state][] = [
          ':input[name="' . strtolower($depedent['form_key']) . '"]' => $element_condition,
        ];
      }
    }
    if (empty($states)) {
      return FALSE;
    }
    return $states;
  }
  else {
    return FALSE;
  }
}