You are here

public function ImageStyleConfigurationHandler::writeToDatabase in Configuration Management 7.3

Saves the given configuration into the database.

Parameters

\Configuration\Configuration $configuration: The configuration to be saved.

Overrides ConfigurationHandler::writeToDatabase

File

src/Handlers/ImageStyleConfigurationHandler.php, line 49

Class

ImageStyleConfigurationHandler

Namespace

Configuration\Handlers

Code

public function writeToDatabase(Configuration $configuration) {
  $name = $this
    ->getInternalId($configuration
    ->getIdentifier());
  $event = $this
    ->triggerEvent('write_to_database', $configuration);
  $style = $event->configuration
    ->getData();

  // Does an image style with the same name already exist?
  if ($existing_style = $this->configuration_manager
    ->drupal()
    ->image_style_load($name)) {
    $isExistingEditable = (bool) ($existing_style['storage'] & IMAGE_STORAGE_EDITABLE);
    $isNewEditable = (bool) ($style['storage'] & IMAGE_STORAGE_EDITABLE);

    // New style is using defaults -> revert existing.
    if (!$isNewEditable && $isExistingEditable) {
      $this->configuration_manager
        ->drupal()
        ->image_default_style_revert($name);
    }
    elseif ($isExistingEditable && $isNewEditable) {
      $style['isid'] = $existing_style['isid'];
      $style = $this->configuration_manager
        ->drupal()
        ->image_style_save($style);
      if (!empty($existing_style['effects'])) {
        foreach ($existing_style['effects'] as $effect) {
          image_effect_delete($effect);
        }
      }
      if (!empty($style['effects'])) {
        foreach ($style['effects'] as $effect) {
          $effect['isid'] = $style['isid'];
          $this->configuration_manager
            ->drupal()
            ->image_effect_save($effect);
        }
      }
    }
    elseif ($isNewEditable && !$isExistingEditable) {
      if (!empty($existing_style['isid'])) {
        $style['isid'] = $existing_style['isid'];
      }
      $style = $this->configuration_manager
        ->drupal()
        ->image_style_save($style);
      if (!empty($style['effects'])) {
        foreach ($style['effects'] as $effect) {
          $effect['isid'] = $style['isid'];
          image_effect_save($effect);
        }
      }
    }
    else {
    }
  }
  else {
    $style = $this->configuration_manager
      ->drupal()
      ->image_style_save($style);
    if (!empty($style['effects'])) {
      foreach ($style['effects'] as $effect) {
        $effect['isid'] = $style['isid'];
        $this->configuration_manager
          ->drupal()
          ->image_effect_save($effect);
      }
    }
    $this->configuration_manager
      ->drupal()
      ->image_style_flush($style);
  }
}