You are here

function rules_webform_form_rules_reaction_rule_edit_form_alter in RULES WEBFORM 8

Same name and namespace in other branches
  1. 3.x rules_webform.module \rules_webform_form_rules_reaction_rule_edit_form_alter()

Implements hook_form_FORM_ID_alter().

1. Hiding our condition ('webform_name'), that user don't distracted on it. 2. If the 'Delete webform submission' action was added, then remove the 'Edit' operation link, because a user doesn't have to edit this action. 3. Add to a condition title a webform title, that a user can see which a webform has been selected.

File

./rules_webform.module, line 137
Contains rules_webform.module.

Code

function rules_webform_form_rules_reaction_rule_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $reaction_rule = $form_state
    ->getFormObject()
    ->getEntity();
  $event_name = $reaction_rule
    ->getEventNames()[0];

  // Events supported by the module.
  $webform_events = [
    'webform_submit',
    'updating_submission',
    'deleting_submission',
    'viewing_submission',
  ];
  if (!in_array($event_name, $webform_events)) {
    return;
  }
  $rule_id = $reaction_rule->id;
  $webform_id = \Drupal::state()
    ->get('rules_webform.webform_id.' . $rule_id);
  $webform = Webform::load($webform_id);

  // Hiding our condition ('webform_name'), that user don't distracted on it.
  foreach ($form['conditions-table']['conditions'] as $key => $value) {
    if (is_array($value) && isset($value['element']) && is_array($value['element']) && isset($value['element']['data']) && is_array($value['element']['data'])) {
      if (isset($value['element']['data']['#plain_text']) && get_class($value['element']['data']['#plain_text']) == 'Drupal\\Core\\StringTranslation\\TranslatableMarkup' && $value['element']['data']['#plain_text']
        ->__toString() == 'Webform name') {
        unset($form['conditions-table']['conditions'][$key]);
      }
    }
  }

  // If the 'Delete webform submission' action was added then
  // remove the 'Edit' operation link, because a user doesn't have to edit it.
  foreach ($form['actions-table']['actions'] as $key => $value) {
    if (is_array($value) && isset($value['element']) && is_array($value['element']) && isset($value['element']['data']) && is_array($value['element']['data'])) {
      if (isset($value['element']['data']['#plain_text']) && get_class($value['element']['data']['#plain_text']) == 'Drupal\\Core\\StringTranslation\\TranslatableMarkup' && $value['element']['data']['#plain_text']
        ->__toString() == 'Delete webform submission') {
        unset($form['actions-table']['actions'][$key]['operations']['data']['#links']['edit']);
      }
    }
  }

  // Add webform title to the event title that user can see which a webform
  // has been selected.
  $webform_title = $webform
    ->get('title');
  $form['event'][0]['#title'] = t('Event:');
  switch ($event_name) {
    case 'webform_submit':
      $event_title = t('Webform "@title" submit', [
        '@title' => $webform_title,
      ]);
      break;
    case 'updating_submission':
      $event_title = t('Updating a submission of the "@title" webform', [
        '@title' => $webform_title,
      ]);
      break;
    case 'deleting_submission':
      $event_title = t('Deleting a submission of the "@title" webform', [
        '@title' => $webform_title,
      ]);
      break;
    case 'viewing_submission':
      $event_title = t('Viewing a submission of the "@title" webform', [
        '@title' => $webform_title,
      ]);
  }
  $form['events']['table']['#rows'][0]['element']['data']['#plain_text'] = $event_title;

  // Removing 'Edit' button for 'Delete webform submission' action because
  // there is nothing to edit.
  if (isset($form['action_table']['table']['#rows'])) {
    foreach ($form['action_table']['table']['#rows'] as &$row) {
      if ($row['element']
        ->render() == 'Action: Delete webform submission') {
        unset($row['operations']['data']['#links']['edit']);
        break;
      }
    }
    unset($row);
  }
}