You are here

function ga_push_form_rules_expression_edit_alter in GA Push 8

Implements hook_form_rules_expression_edit_alter().

File

./ga_push.module, line 908
Drupal Module: GA Push.

Code

function ga_push_form_rules_expression_edit_alter(&$form, FormStateInterface $form_state, $form_id) {
  $ga_push_rule_action_list = [
    'ga_push_event',
    'ga_push_exception',
    'ga_push_page_view',
    'ga_push_social',
  ];
  if (in_array($form_state
    ->get('action_id'), $ga_push_rule_action_list)) {

    // Use select widget.
    // @WORKAROUND: set ga push event rule action method as option list. Now rules doesn't support it.
    // @TODO: don't use this when option list is allowed!
    $mode = $form_state
      ->get('context_method');
    if ($mode == 'input') {
      $form['context']['method']['setting']['#type'] = 'select';
      $form['context']['method']['setting']['#options'] = ga_push_get_methods_option_list_event();
      if (empty($form['context']['method']['setting']['#default_value'])) {
        $form['context']['method']['setting']['#default_value'] = \Drupal::config('ga_push.settings')
          ->get('default_method');
      }
    }
  }
  if ($form_state
    ->get('action_id') == 'ga_push_event') {

    // Event only will be visible if:
    // - Datalayer method is selected.
    // - Default method is selected and is set up as datalayer.
    $default_method_key = \Drupal::config('ga_push.settings')
      ->get('default_method');
    $states = [];
    $states['visible'] = [
      '#edit-context-method-setting' => [
        [
          'value' => 'datalayer-js',
        ],
      ],
    ];
    if ($default_method_key == GA_PUSH_METHOD_DATALAYER_JS) {
      $states['visible']['#edit-context-method-setting'][] = [
        'value' => 'default',
      ];
    }
    $form['context']['event'] += [
      '#states' => $states,
    ];
  }
}