Exemplo n.º 1
0
/**
 * Add or remove styles if button is clicked
 */
function _plain_response_settings_form_submitted($form, $form_state)
{
    $styles = _plain_response_image_styles();
    if ($form_state['values']['op'] === t('Add Custom Image Styles')) {
        foreach ($styles as $part => $settings) {
            $name = 'plain_response_' . $part;
            $style = image_style_load($name);
            if (empty($style)) {
                $style = image_style_save(array('name' => $name) + $settings);
                foreach ($settings['effects'] as $effect) {
                    $effect = image_effect_save(array('isid' => $style['isid']) + $effect);
                }
            }
        }
        drupal_set_message('Custom image styles have been added.');
    }
    if ($form_state['values']['op'] === t('Remove Custom Image Styles')) {
        foreach (array_keys($styles) as $part) {
            $name = 'plain_response_' . $part;
            $style = image_style_load($name);
            if (!empty($style)) {
                image_style_delete($style);
            }
        }
        drupal_set_message('Custom image styles have been removed.');
    }
}
 /**
  * {@inheritDoc}
  */
 public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false)
 {
     if (($style = $this->loadExistingImageStyle($node, $context)) === false) {
         $object = ['name' => $this->getStyleName($node, $context)];
         $style = image_style_save($object);
     }
     $weight = 0;
     // Handle effects as well.
     if ($node->hasChild('effects')) {
         $valid = array_keys(image_effect_definitions());
         $index = 0;
         foreach ($node->getChild('effects')->getChildren() as $effectNode) {
             $name = $effectNode->getName();
             if (is_numeric($name)) {
                 if (array_key_exists('type', $effectNode->getValue())) {
                     $name = $effectNode->getValue()['type'];
                 } else {
                     $context->logWarning(sprintf("%s: effect has no type, ignoring", $effectNode->getPath()));
                     continue;
                 }
             }
             if (!in_array($name, $valid)) {
                 $context->logWarning(sprintf("%s: effect does not exists, ignoring", $effectNode->getPath()));
                 continue;
             }
             $effect = [];
             // FIXME Handle effects more appropriately
             if (isset($style['effects']) && count($style['effects']) > $index) {
                 $chunk = array_slice($style['effects'], $index++, 1);
                 $old_effect = reset($chunk);
                 if ($old_effect['name'] == $name) {
                     $effect = $old_effect;
                 } else {
                     // Structure change, better delete all old effects.
                     foreach (array_values($style['effects']) as $effect) {
                         image_effect_delete($effect);
                     }
                 }
             }
             $effect['name'] = $name;
             $effect['data'] = $effectNode->getValue();
             if (isset($effect['data']['type'])) {
                 unset($effect['data']['type']);
             }
             $effect['isid'] = $style['isid'];
             $effect['weight'] = $weight++;
             image_effect_save($effect);
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Implements Drupal\configuration\Config\Configuration::saveToActiveStore().
  */
 public function saveToActiveStore(ConfigIteratorSettings &$settings)
 {
     $style = $this->getData();
     // Does an image style with the same name already exist?
     if ($existing_style = image_style_load($this->getIdentifier())) {
         $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) {
             image_default_style_revert($this->getIdentifier());
         } elseif ($isExistingEditable && $isNewEditable) {
             $style['isid'] = $existing_style['isid'];
             $style = 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'];
                     image_effect_save($effect);
                 }
             }
         } elseif ($isNewEditable && !$isExistingEditable) {
             if (!empty($existing_style['isid'])) {
                 $style['isid'] = $existing_style['isid'];
             }
             $style = image_style_save($style);
             if (!empty($style['effects'])) {
                 foreach ($style['effects'] as $effect) {
                     $effect['isid'] = $style['isid'];
                     image_effect_save($effect);
                 }
             }
         } else {
         }
     } else {
         $style = image_style_save($style);
         if (!empty($style['effects'])) {
             foreach ($style['effects'] as $effect) {
                 $effect['isid'] = $style['isid'];
                 image_effect_save($effect);
             }
         }
         image_style_flush($style);
     }
     $settings->addInfo('imported', $this->getUniqueId());
 }