You are here

public function BackgroundImageForm::setSubformState in Background Image 8

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

File

src/Form/BackgroundImageForm.php, line 657

Class

BackgroundImageForm

Namespace

Drupal\background_image\Form

Code

public function setSubformState() {
  $this
    ->traitSetSubformState();

  // Make sure we have a type and target set.
  if (!isset($this->type) || !isset($this->target)) {

    // If there is an inline entity, then the type is already determined.
    if ($this->inlineEntity) {
      $this->backgroundImage
        ->associateEntity($this->inlineEntity, FALSE);
      $this->type = $this->backgroundImage
        ->getType();
      $this->target = $this->backgroundImage
        ->getTarget();
      $this->step = 2;
    }
    else {
      if (!$this->backgroundImage
        ->isNew()) {
        $this->type = $this->backgroundImage
          ->getType();
        $this->target = $this->backgroundImage
          ->getTarget();
        $this->step = 2;
      }
      else {
        $type = $this
          ->getSubformValue('type');
        if (isset($type)) {
          $this->type = (int) $type;
          if ($this->type === BackgroundImageInterface::TYPE_GLOBAL) {
            $this->target = '';
            $this->backgroundImage
              ->set('label', $this
              ->getSubformValue('label', ''));
          }
          else {
            if ($this->type === BackgroundImageInterface::TYPE_ENTITY && ($entity_type_id = $this
              ->getSubformValue('entity_type'))) {
              $entity_id = $this
                ->getSubformValue("entity_type_{$entity_type_id}");
              if (isset($entity_id) && ($uuid = \Drupal::entityTypeManager()
                ->getStorage($entity_type_id)
                ->load($entity_id)
                ->uuid())) {
                $this->target = "{$entity_type_id}:{$uuid}";
              }
            }
            else {
              if ($this->type === BackgroundImageInterface::TYPE_ENTITY_BUNDLE && ($target = $this
                ->getSubformValue('entity_bundle'))) {
                $this->target = $target;
              }
              else {
                if ($this->type === BackgroundImageInterface::TYPE_PATH && ($target = $this
                  ->getSubformValue('path'))) {
                  $this->target = $target;
                }
                else {
                  if ($this->type === BackgroundImageInterface::TYPE_ROUTE && ($target = $this
                    ->getSubformValue('route'))) {
                    $this->target = $target;
                  }
                  else {
                    if ($this->type === BackgroundImageInterface::TYPE_VIEW && ($target = $this
                      ->getSubformValue('view'))) {
                      $this->target = $target;
                    }
                  }
                }
              }
            }
          }
          $this->backgroundImage
            ->set('type', $this->type)
            ->set('target', $this->target);
        }
      }
    }

    // If there is still no type or target, then do not continue. The type
    // selection form must be shown.
    if ($this->type === BackgroundImageInterface::TYPE_GLOBAL) {

      // Ensure there is a label.
      $label = $this
        ->getSubformValue('label', $this->backgroundImage
        ->label());
      if (!isset($label)) {
        return $this;
      }
    }
    else {
      if (!isset($this->type) || !isset($this->target)) {
        return $this;
      }
    }
  }
  $this->parent = $this->backgroundImage
    ->getParent();
  $this->overriddenSettings = $this->backgroundImage
    ->getSettings()
    ->merge($this
    ->getSettingValues())
    ->getOverridden();
  $this->targetEntity = $this->backgroundImage
    ->getTargetEntity();

  // Determine if an image is required.
  $this->required = $this->type === BackgroundImageInterface::TYPE_GLOBAL || ($this->targetEntity ? self::getBackgroundImageManager()
    ->getEntityConfig($this->targetEntity, 'require') : FALSE);

  // Determine the image file.
  if (($fids = $this
    ->getSubformValue([
    'image',
    '0',
    'fids',
  ])) && ($fid = reset($fids))) {
    $this->imageFile = File::load($fid);
  }
  else {
    $this->imageFile = $this->backgroundImage
      ->getImageFile(FALSE);
  }

  // Determine whether to override the image file.
  if ($this->required || $this->imageFile) {
    $this->imageType = BackgroundImageInterface::NORMAL;
  }
  else {
    $this->imageType = $this->parent ? BackgroundImageInterface::INHERIT : BackgroundImageInterface::NORMAL;
  }
  return $this;
}