You are here

public function BackgroundImageForm::validate in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::validate()
  2. 2.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::validate()

Validation handler.

File

src/Form/BackgroundImageForm.php, line 881

Class

BackgroundImageForm

Namespace

Drupal\background_image\Form

Code

public function validate() {

  // Handle type selection.
  if ($this->step == 1) {
    if (!isset($this->type) || !in_array($this->type, array_keys(BackgroundImage::getTypes()))) {
      return $this->subformState
        ->setError($this->subform['type'], $this
        ->t('You must select a valid type.'));
    }
    else {
      if ($this->type === BackgroundImageInterface::TYPE_GLOBAL) {
        if (empty($this->backgroundImage
          ->label())) {
          return $this->subformState
            ->setError($this->subform['label'], $this
            ->t('You must enter a valid label.'));
        }
      }
      else {
        if (empty($this->target)) {
          if ($this->type === BackgroundImageInterface::TYPE_ENTITY) {
            $entity_type_id = $this
              ->getSubformValue('entity_type');
            if (!isset($entity_type_id)) {
              return $this->subformState
                ->setError($this->subform['entity_type'], $this
                ->t('You select a valid entity type.'));
            }
            else {
              $entity_id = $this
                ->getSubformValue("entity_type_{$entity_type_id}");
              if (!isset($entity_id)) {
                return $this->subformState
                  ->setError($this->subform["entity_type_{$entity_type_id}"], $this
                  ->t('You enter a specific entity to target.'));
              }
              else {
                if (!$this->backgroundImage
                  ->getTargetEntity($this->type, "{$entity_type_id}:{$entity_id}")) {
                  return $this->subformState
                    ->setError($this->subform["entity_type_{$entity_type_id}"], $this
                    ->t('You enter a valid entity to target.'));
                }
              }
            }
          }
          else {
            if ($this->type === BackgroundImageInterface::TYPE_ENTITY_BUNDLE && ($target = $this
              ->getSubformValue('entity_bundle'))) {
              return $this->subformState
                ->setError($this->subform['view'], $this
                ->t('You select a valid entity bundle.'));
            }
            else {
              if ($this->type === BackgroundImageInterface::TYPE_PATH && ($target = $this
                ->getSubformValue('path'))) {
                return $this->subformState
                  ->setError($this->subform['path'], $this
                  ->t('You enter a valid path.'));
              }
              else {
                if ($this->type === BackgroundImageInterface::TYPE_ROUTE && ($target = $this
                  ->getSubformValue('route'))) {
                  return $this->subformState
                    ->setError($this->subform['route'], $this
                    ->t('You enter a valid route.'));
                }
                else {
                  if ($this->type === BackgroundImageInterface::TYPE_VIEW && ($target = $this
                    ->getSubformValue('view'))) {
                    return $this->subformState
                      ->setError($this->subform['view'], $this
                      ->t('You select a valid view.'));
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}