You are here

public function EditorIconDialog::submitForm in Font Awesome Icons 8.2

Same name and namespace in other branches
  1. 8 src/Form/EditorIconDialog.php \Drupal\fontawesome\Form\EditorIconDialog::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/EditorIconDialog.php, line 404

Class

EditorIconDialog
Provides a Font Awesome icon dialog for text editors.

Namespace

Drupal\fontawesome\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  if ($form_state
    ->getErrors()) {
    unset($form['#prefix'], $form['#suffix']);
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $response
      ->addCommand(new HtmlCommand('#fontawesome-icon-dialog-form', $form));
  }
  else {
    $item = $form_state
      ->getValues();

    // Remove the prefix if the user accidentally added it.
    if (substr($item['icon_name'], 0, 3) == 'fa-') {
      $item['icon_name'] = substr($item['icon_name'], 3);
    }

    // Massage rotate and flip values to make them format properly.
    if (is_numeric($item['settings']['power_transforms']['rotate']['value'])) {
      $item['settings']['power_transforms']['rotate']['type'] = 'rotate';
    }
    else {
      unset($item['settings']['power_transforms']['rotate']);
    }
    if (!empty($item['settings']['power_transforms']['flip-h']['value'])) {
      $item['settings']['power_transforms']['flip-h']['type'] = 'flip';
    }
    else {
      unset($item['settings']['power_transforms']['flip-h']);
    }
    if (!empty($item['settings']['power_transforms']['flip-v']['value'])) {
      $item['settings']['power_transforms']['flip-v']['type'] = 'flip';
    }
    else {
      unset($item['settings']['power_transforms']['flip-v']);
    }

    // Determine the icon style - brands don't allow style.
    $metadata = $this->fontAwesomeManager
      ->getIconMetadata($item['icon_name']);
    $item['style'] = $this->fontAwesomeManager
      ->determinePrefix($metadata['styles'], $item['settings']['style']);
    unset($item['settings']['style']);

    // Remove blank data.
    $item['settings'] = array_filter($item['settings']);

    // Get power transforms.
    $item['power_transforms'] = [];
    foreach ($item['settings']['power_transforms'] as $transform) {
      if (!empty($transform['type'])) {
        $item['power_transforms'][] = $transform['type'] . '-' . $transform['value'];
      }
    }
    unset($item['settings']['power_transforms']);

    // Set the icon attributes.
    $icon_attributes = [
      'attributes' => [
        'class' => [
          trim($item['style'] . ' fa-' . $item['icon_name'] . ' ' . implode(' ', $item['settings'])),
        ],
      ],
    ];

    // If there are power transforms, add them.
    if (count($item['power_transforms']) > 0) {
      $icon_attributes['attributes']['data-fa-transform'] = [
        implode(' ', $item['power_transforms']),
      ];
    }

    // Load the configuration settings.
    $configuration_settings = $this->configFactory
      ->get('fontawesome.settings');

    // Set the user-selected tag type being used.
    $icon_attributes['tag'] = empty($configuration_settings
      ->get('tag')) ? 'i' : $configuration_settings
      ->get('tag');
    $response
      ->addCommand(new EditorDialogSave($icon_attributes));
    $response
      ->addCommand(new CloseModalDialogCommand());
  }
  return $response;
}