You are here

public function AddBannerForm::validateForm in Dynamic Banner 8

Form validation 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 FormBase::validateForm

File

src/forms/AddBannerForm.php, line 225

Class

AddBannerForm

Namespace

Drupal\dynamic_banner\forms

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  //No need to validate

  // For a banner to exist it needs a path that it is assigned to and an image, thats it.
  if ($form_state
    ->getValue('path') != '' && ($form_state
    ->getValue('image') != '' || $form_state
    ->getValue('imgurl') != '')) {
    $path = $form_state
      ->getValue('path');
    $dbid = $form_state
      ->getValue('dbid');
    if ($path != 'DEFAULT' && $dbid == '') {

      // check db before altering the path variable
      // check for more than one of the same path banners
      $query = \Drupal::database()
        ->select('dynamic_banner', 'frm')
        ->fields('frm', array(
        'path',
      ))
        ->condition('path', $path)
        ->execute();
      $result = $query
        ->fetchAll();
      if (isset($result[0]) && $result[0]->path) {
        $form_state
          ->setErrorByName('path', t('The path %path is already in use.', array(
          '%path' => $path,
        )));
        return;
      }

      // path is not clean at this point because of wildcard and random must chop those characters off
      // find the * or wildcard
      $wild_position = strrpos($path, "*");
      if ($wild_position !== FALSE) {
        $path = drupal_substr($path, 0, $wild_position);
      }

      // find the ! or random
      $rand_position = strrpos($path, "!");
      if ($rand_position !== FALSE) {
        $path = drupal_substr($path, 0, $rand_position);
      }

      // D8
      $alias = \Drupal::service('path_alias.manager')
        ->getAliasByPath('/' . $path);
      if (strcmp($alias, $path) == 0) {
        $alias = '';

        // No alias was found.
        $form_state
          ->setErrorByName('path', t('The path %path is not known by drupal.', array(
          '%path' => $path,
        )));
        return;
      }
      else {
        if ($dbid != 0) {
          return;
        }
      }
    }

    /*
     * Image validation & upload
     */
    $image = $form_state
      ->getValue('image');
    if (isset($image[0]) && $image[0] != '' && $image[0] != 0) {

      //$file = file_load($form_state->getValue('image'));
      $file = File::load((int) $image[0]);
      if ($file) {
        $image = $form_state
          ->getValue('image');
        $file
          ->setPermanent();
        $file_usage = \Drupal::service('file.usage');
        $file_usage
          ->add($file, 'dynamic_banner', 'banner', 1);

        // Save the file again for permanent status
        $file
          ->save();
      }
      else {
        $form_state
          ->setErrorByName('image', t('Failed to write the uploaded file to the folder.'));
        return;
      }
    }
    elseif ($form_state
      ->getValue('image') == 0) {
      echo "inside image is 0";
      $this
        ->dynamic_banner_image_delete($form_state
        ->getValue('oldimagefid'));
    }

    /*
     * Image validation & upload ends here
     */
  }
  else {
    $form_state
      ->setErrorByName('path', t('There was a problem with the required fields please check the form and try again.'));
    return;
  }
}