<?php

/**
 * @file
 * The Social editor module.
 */

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_element_info_alter().
 */
function social_editor_element_info_alter(array &$types) {
  // Our process callback must run directly after TextFormat::processFormat().
  if (isset($types['text_format']) && isset($types['text_format']['#process'])) {
    $search_value = ['Drupal\filter\Element\TextFormat', 'processFormat'];
    $key = array_search($search_value, $types['text_format']['#process']);
    if ($key !== FALSE) {
      $key++;
      array_splice($types['text_format']['#process'], $key, 0, 'social_editor_filter_process_format');
    }
    else {
      $types['text_format']['#process'][] = 'social_editor_filter_process_format';
    }
  }
}

/**
 * Process callback for form elements that have a text format selector attached.
 *
 * This callback runs after filter_process_format() and performs additional
 * modifications to the form element.
 *
 * @see \Drupal\filter\Element\TextFormat::processFormat()
 */
function social_editor_filter_process_format(array &$element, FormStateInterface $form_state, array $complete_form) {
  // Init.
  $disable_fields = FALSE;

  // Forms.
  $forms = [
    "social_post_entity_form",
    "comment_post_comment_form",
    "comment_comment_form",
  ];
  // Allowed formats.
  $allowed_format = 'simple_text';

  // Check if it needs to be done here.
  if (in_array($complete_form['#form_id'], $forms)) {
    // Remove user formats except simple_text.
    foreach ($element['format']['format']['#options'] as $key => $value) {
      if ($key !== $allowed_format) {
        unset($element['format']['format']['#options'][$key]);
      }
    }
    // No changes can be made in text formats, so remove the fields.
    $disable_fields = TRUE;
  }
  else {
    // Remove simple_text.
    foreach ($element['format']['format']['#options'] as $key => $value) {
      if ($key === $allowed_format) {
        unset($element['format']['format']['#options'][$key]);
      }
    }
  }

  // No changes can be made in text formats, so deny access to the fields.
  if (($disable_fields == TRUE) || count($element['format']['format']['#options']) == 1) {
    // Deny access to format fields.
    $element['format']['format']['#access'] = FALSE;
    $element['format']['guidelines']['#access'] = FALSE;
    $element['format']['help']['#access'] = FALSE;
  }

  return $element;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Removes class, id and rel attributes introduced by Editor Advanced Link.
 *
 * @see editor_advanced_link_form_editor_link_dialog_alter()
 */
function social_editor_form_editor_link_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
  // This helps to keep the form clean and less confusing.
  if (isset($form['attributes'])) {
    // Unset un-required attribute's form elements.
    unset($form['attributes']['class']);
    unset($form['attributes']['id']);
    unset($form['attributes']['rel']);

    // Unset the group.
    unset($form['advanced']);
    unset($form['attributes']['target']['#group']);

    // Add weight to target attribute to appear after title.
    $form['attributes']['target']['#weight'] = 2;

    // Change the description text to make it more human-friendly.
    $form['attributes']['title']['#description'] = t('Give a title that will appear when hovering over this link.');
  }
}
