You are here

function social_content_report_form_alter in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()
  2. 8.5 modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()
  3. 8.6 modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()
  4. 8.7 modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()
  5. 8.8 modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()
  6. 10.0.x modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()
  7. 10.1.x modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()
  8. 10.2.x modules/social_features/social_content_report/social_content_report.module \social_content_report_form_alter()

Implements hook_form_alter().

File

modules/social_features/social_content_report/social_content_report.module, line 17
The Social Content Report module file.

Code

function social_content_report_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // No need to do anything here, if it's not a flagging related form.
  if (strpos($form_id, 'flagging_') === FALSE) {
    return;
  }

  // Get all 'report_' flags.
  $report_types = \Drupal::service('social_content_report.content_report_service')
    ->getReportFlagTypes();

  // Add each report type form.
  $report_forms = [];
  foreach ($report_types as $report_type) {
    $report_forms[] = 'flagging_' . $report_type . '_add_form';
    $report_forms[] = 'flagging_' . $report_type . '_edit_form';
  }
  if (in_array($form_id, $report_forms, FALSE)) {
    $config = \Drupal::config('social_content_report.settings');
    $terms = $config
      ->get('reasons_with_text');

    // Only show the "Other reason" title to screenreaders.
    $form['field_other_reason']['widget'][0]['value']['#title_display'] = 'invisible';

    // Only show the reason input field if it is enabled for the reason.
    foreach ($terms as $term_id) {
      $form['field_other_reason']['#states']['visible'][] = [
        ':input[name="field_reason"]' => [
          'value' => $term_id,
        ],
      ];
    }
    $form['#attributes']['class'][] = 'form--content-reporting';
    $form['#attached']['library'][] = 'social_content_report/reporting';

    // Add some validation if the reason field is mandatory.
    if ($config
      ->get('mandatory_reason')) {
      $form['#validate'][] = 'social_content_report_mandatory_reason_validate';
    }
    $headers = \Drupal::request()->headers;
    if ($headers
      ->has('referer')) {
      $form_state
        ->set('referer', $headers
        ->get('referer'));
      $form['actions']['submit']['#submit'][] = '_social_content_report_submit';
    }
  }
}