You are here

class ImageStyleConfigurationHandler in Configuration Management 7.3

Hierarchy

Expanded class hierarchy of ImageStyleConfigurationHandler

File

src/Handlers/ImageStyleConfigurationHandler.php, line 9

Namespace

Configuration\Handlers
View source
class ImageStyleConfigurationHandler extends ConfigurationHandler {
  public static function getSupportedTypes() {
    return array(
      'image_style',
    );
  }
  public function getIdentifiers() {
    $identifiers = array();
    foreach (image_styles() as $key => $image_style) {
      $identifiers[$key] = $image_style['name'];
    }
    return $identifiers;
  }
  public function loadFromDatabase($identifier) {
    $name = $this
      ->getInternalId($identifier);
    $configuration = new Configuration();
    $configuration
      ->setIdentifier($identifier);
    $style = $this->configuration_manager
      ->drupal()
      ->image_style_load($name);
    $this
      ->styleSanitize($style);

    // Reset the order of effects, this will help to generate always the same
    // hash for image styles that have been reverted.
    $effects_copy = array();
    if (!empty($style['effects'])) {
      foreach ($style['effects'] as $effect) {
        $configuration
          ->addModule($effect['module']);
        $effects_copy[] = $effect;
      }
    }
    $style['effects'] = $effects_copy;
    $configuration
      ->setData($style);
    $event = $this
      ->triggerEvent('load_from_database', $configuration);
    return $event->configuration;
  }
  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);
    }
  }
  public function removeFromDatabase(Configuration $configuration) {
    $name = $this
      ->getInternalId($configuration
      ->getIdentifier());
    $event = $this
      ->triggerEvent('remove_from_database', $configuration);
    $style = $event->configuration
      ->getData();
    $this->configuration_manager
      ->drupal()
      ->image_style_delete($style);
  }

  /**
   * Remove unnecessary keys for export.
   */
  protected function styleSanitize(&$style, $child = FALSE) {
    $omit = $child ? array(
      'isid',
      'ieid',
    ) : array(
      'isid',
      'ieid',
      'module',
    );
    if (is_array($style)) {
      foreach ($style as $k => $v) {
        if (in_array($k, $omit, TRUE)) {
          unset($style[$k]);
        }
        elseif (is_array($v)) {
          $this
            ->styleSanitize($style[$k], TRUE);
        }
      }
    }
  }
  public static function getSubscribedEvents() {
    return array(
      'load_from_database.field_instance' => array(
        'onFieldInstanceLoad',
        0,
      ),
    );
  }
  public function onFieldInstanceLoad($event) {

    // Check if the field is using a image style
    $field = $event->configuration
      ->getData();
    if (!empty($field['display'])) {
      foreach ($field['display'] as $display) {
        if (!empty($display['settings']['image_style'])) {
          $identifier = $display['settings']['image_style'];
          $this->configuration_manager
            ->newDependency($event->configuration, 'image_style.' . $identifier);
        }
      }
    }
  }
  protected function jsonAsArray() {
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurationHandler::$configuration_manager protected property
ConfigurationHandler::$type protected property
ConfigurationHandler::export public function Generates the JSON representation of this configuration.
ConfigurationHandler::exportToJson protected function
ConfigurationHandler::getExportPath public function
ConfigurationHandler::getInternalId protected function
ConfigurationHandler::getType public function
ConfigurationHandler::getTypeFromId protected function
ConfigurationHandler::import public function
ConfigurationHandler::importFromJson public function
ConfigurationHandler::importFromJsonAsArray protected function 1
ConfigurationHandler::registerProcessors protected function 2
ConfigurationHandler::triggerEvent protected function
ConfigurationHandler::__construct public function 1
ImageStyleConfigurationHandler::getIdentifiers public function Returns the configuration identifiers handled by this instance. Overrides ConfigurationHandler::getIdentifiers
ImageStyleConfigurationHandler::getSubscribedEvents public static function Overrides ConfigurationHandler::getSubscribedEvents
ImageStyleConfigurationHandler::getSupportedTypes public static function Returns the types of configurations that this class can handle. Overrides ConfigurationHandler::getSupportedTypes
ImageStyleConfigurationHandler::jsonAsArray protected function Overrides ConfigurationHandler::jsonAsArray
ImageStyleConfigurationHandler::loadFromDatabase public function Loads the configuration from the database. Overrides ConfigurationHandler::loadFromDatabase
ImageStyleConfigurationHandler::onFieldInstanceLoad public function
ImageStyleConfigurationHandler::removeFromDatabase public function Deletes a configuration from the database. Overrides ConfigurationHandler::removeFromDatabase
ImageStyleConfigurationHandler::styleSanitize protected function Remove unnecessary keys for export.
ImageStyleConfigurationHandler::writeToDatabase public function Saves the given configuration into the database. Overrides ConfigurationHandler::writeToDatabase