public function BackgroundImageForm::submit in Background Image 8
Same name and namespace in other branches
- 2.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::submit()
- 2.0.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::submit()
Submit handler.
File
- src/
Form/ BackgroundImageForm.php, line 781
Class
Namespace
Drupal\background_image\FormCode
public function submit() {
// If user was selecting a type, then rebuild the form.
if ($this->step < 2) {
$this->step++;
$this->subformState
->setRebuild();
return;
}
// If there is no image and no overridden settings, then this shouldn't
// create a new background image entity and should immediately return.
if (!$this->imageFile && !$this->overriddenSettings) {
// Immediately return if this is an embedded form.
if ($this->inlineEntity) {
return;
}
// Otherwise, show an error message explaining why this cannot be saved.
if ($this->parent) {
drupal_set_message($this
->t('To create a new background image, you must override the inherited background image. Either by setting a new image or changing some of its settings.'), 'error');
}
else {
drupal_set_message($this
->t('To create a new background image, you must first set an image.'), 'error');
}
$this->subformState
->setRebuild();
return;
}
// Set the image.
if ($this->imageFile) {
$this->backgroundImage
->set('image', $this->imageFile);
}
// If this background image should inherit its parent image, go ahead and
// delete any images currently associated.
$image_type = (int) $this
->getSubformValue([
'image_group',
'image_type',
], BackgroundImageInterface::NORMAL);
if ($image_type === BackgroundImageInterface::INHERIT) {
if ($this->parent && $this->imageFile) {
try {
$this->imageFile
->delete();
} catch (\Exception $e) {
// Intentionally left empty.
}
$this
->setSubformValue([
'image',
'0',
], []);
}
$this->imageFile = NULL;
}
// Merge in any overridden settings.
if ($this->overriddenSettings) {
$this->backgroundImage
->getSettings()
->merge($this->overriddenSettings);
}
// Set whether image is dark.
$dark = $this
->getSubformValue([
'image',
'0',
'background_image',
'dark',
], $this->backgroundImage
->getSettings()
->getOverridden('dark'));
if ($this->imageFile && isset($dark)) {
$this->backgroundImage
->getSettings()
->set('dark', $dark);
}
else {
$this->backgroundImage
->getSettings()
->set('dark', $this->backgroundImage
->getSetting('dark'));
}
// Set the preload background color.
if ($this->imageFile && ($preload_background_color = $this
->getSubformValue([
'image',
'0',
'background_image',
'preload_background_color',
'value',
], $this->backgroundImage
->getSettings()
->getOverridden('preload.background_color')))) {
$this->backgroundImage
->getSettings()
->set('preload.background_color', $preload_background_color);
}
else {
$this->backgroundImage
->getSettings()
->set('preload.background_color', $this->backgroundImage
->getSetting('preload.background_color'));
}
try {
$status = $this->backgroundImage
->save();
// Only show a message and redirect if this is not embedded on another form.
if (!$this->inlineEntity) {
if ($status == SAVED_UPDATED) {
drupal_set_message($this
->t('The background image @link has been updated.', [
'@link' => $this->backgroundImage
->toLink()
->toString(),
]));
}
else {
drupal_set_message($this
->t('The background image @link has been added.', [
'@link' => $this->backgroundImage
->toLink()
->toString(),
]));
}
$this->subformState
->setRedirectUrl($this->backgroundImage
->toUrl('collection'));
}
} catch (\Exception $e) {
// Intentionally left empty.
}
}