getEffects example


  public function testImport() {
    $style = ImageStyle::create([
      'name' => 'test',
    ]);

    $style->addImageEffect(['id' => 'image_module_test_null']);
    $style->addImageEffect(['id' => 'image_module_test_null']);
    $style->save();

    $this->assertCount(2, $style->getEffects());

    $uuid = \Drupal::service('uuid')->generate();
    $style->set('effects', [
      $uuid => [
        'id' => 'image_module_test_null',
      ],
    ]);
    $style->save();

    $style = ImageStyle::load('test');
    $this->assertCount(1, $style->getEffects());
  }
    // These are added by a hook_image_style_presave() implemented in     // image_module_test module.     $this->assertEquals('bar', $style->getThirdPartySetting('image_module_test', 'foo'), 'Third party settings were added to the image style.');

    // Ensure that the image style URI matches our expected path.     $style_uri_path = $style->toUrl()->toString();
    $this->assertStringContainsString($style_path$style_uri_path, 'The image style URI is correct.');

    // Confirm that all effects on the image style have settings that match     // what was saved.     $uuids = [];
    foreach ($style->getEffects() as $uuid => $effect) {
      // Store the uuid for later use.       $uuids[$effect->getPluginId()] = $uuid;
      $effect_configuration = $effect->getConfiguration();
      foreach ($effect_edits[$effect->getPluginId()] as $field => $value) {
        $this->assertEquals($effect_configuration['data'][$field]$valuenew FormattableMarkup('The %field field in the %effect effect has the correct value of %value.', ['%field' => $field, '%effect' => $effect->getPluginId(), '%value' => $value]));
      }
    }

    // Assert that every effect was saved.     foreach (array_keys($effect_edits) as $effect_name) {
      $this->assertTrue(isset($uuids[$effect_name])new FormattableMarkup(
        
'relationship' => 'sibling',
          'group' => 'image-effect-order-weight',
        ],
      ],
      '#attributes' => [
        'id' => 'image-style-effects',
      ],
      '#empty' => $this->t('There are currently no effects in this style. Add one by selecting an option below.'),
      // Render effects below parent elements.       '#weight' => 5,
    ];
    foreach ($this->entity->getEffects() as $effect) {
      $key = $effect->getUuid();
      $form['effects'][$key]['#attributes']['class'][] = 'draggable';
      $form['effects'][$key]['#weight'] = isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL;
      $form['effects'][$key]['effect'] = [
        '#tree' => FALSE,
        'data' => [
          'label' => [
            '#plain_text' => $effect->label(),
          ],
        ],
      ];

      
    // image style preview.     $this->assertEquals(2, $this->getImageCount($style, 'public')new FormattableMarkup('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));

    // Create an image for the 'private' wrapper.     $image_path = $this->createSampleImage($style, 'private');
    $this->assertEquals(1, $this->getImageCount($style, 'private')new FormattableMarkup('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));

    // Remove the 'image_scale' effect and updates the style, which in turn     // forces an image style flush.     $style_path = 'admin/config/media/image-styles/manage/' . $style->id();
    $uuids = [];
    foreach ($style->getEffects() as $uuid => $effect) {
      $uuids[$effect->getPluginId()] = $uuid;
    }
    $this->drupalGet($style_path . '/effects/' . $uuids['image_scale'] . '/delete');
    $this->submitForm([], 'Delete');
    $this->assertSession()->statusCodeEquals(200);
    $this->drupalGet($style_path);
    $this->submitForm([], 'Save');
    $this->assertSession()->statusCodeEquals(200);

    // Post flush, expected 1 image in the 'public' wrapper (sample.png).     $this->assertEquals(1, $this->getImageCount($style, 'public')new FormattableMarkup('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'public']));

    

  protected function assertEntity(string $id, string $label, array $expected_effect_plugins, array $expected_effect_config): void {
    $style = ImageStyle::load($id);
    $this->assertInstanceOf(ImageStyleInterface::class$style);
    /** @var \Drupal\image\ImageStyleInterface $style */
    $this->assertSame($id$style->id());
    $this->assertSame($label$style->label());

    // Check the number of effects associated with the style.     $effects = $style->getEffects();
    $this->assertSameSize($expected_effect_plugins$effects);

    $index = 0;
    foreach ($effects as $effect) {
      $this->assertInstanceOf(ImageEffectBase::class$effect);
      $this->assertSame($expected_effect_plugins[$index]$effect->getPluginId());
      $config = $effect->getConfiguration();
      $this->assertSame($expected_effect_config[$index]$config['data']);
      $index++;
    }
  }

}
public function testPassingMigration() {
    $this->executeMigration('d6_imagecache_presets');

    /** @var \Drupal\image\Entity\ImageStyle $style */
    $style = ImageStyle::load('big_blue_cheese');

    // Check basic Style info.     $this->assertSame('big_blue_cheese', $style->get('name'), 'ImageStyle name set correctly');
    $this->assertSame('big_blue_cheese', $style->get('label'), 'ImageStyle label set correctly');

    // Test effects.     $effects = $style->getEffects();

    // Check crop effect.     $this->assertImageEffect($effects, 'image_crop', [
      'width' => 555,
      'height' => 5555,
      'anchor' => 'center-center',
    ]);

    // Check resize effect.     $this->assertImageEffect($effects, 'image_resize', [
      'width' => 55,
      
    $this->drupalGet($style_path);
    $this->submitForm(['new' => 'image_module_test_ajax'], 'Add');
    $this->submitForm($effect_edit, 'Add effect');
    $this->drupalGet($style_path);
    $this->submitForm(['new' => 'image_module_test_ajax'], 'Add');
    $this->submitForm($effect_edit, 'Add effect');

    // Load the saved image style.     $style = ImageStyle::load($style_name);

    // Edit back the effects.     foreach ($style->getEffects() as $uuid => $effect) {
      $effect_path = $admin_path . '/manage/' . $style_name . '/effects/' . $uuid;
      $this->drupalGet($effect_path);
      $page->findField('data[test_parameter]')->setValue(111);
      $ajax_value = $page->find('css', '#ajax-value')->getText();
      $this->assertSame('Ajax value bar', $ajax_value);
      $this->getSession()->getPage()->pressButton('Ajax refresh');
      $this->assertTrue($page->waitFor(10, function D$page) {
        $ajax_value = $page->find('css', '#ajax-value')->getText();
        return (bool) preg_match('/^Ajax value [0-9.]+ [0-9.]+$/', $ajax_value);
      }));
      $page->pressButton('Update effect');
      
$form['actions']['submit']['#value'] = $this->t('Add effect');

    return $form;
  }

  /** * {@inheritdoc} */
  protected function prepareImageEffect($image_effect) {
    $image_effect = $this->effectManager->createInstance($image_effect);
    // Set the initial weight so this effect comes last.     $image_effect->setWeight(count($this->imageStyle->getEffects()));
    return $image_effect;
  }

}


    // Get the folder for the final location of this style.     $directory = \Drupal::service('file_system')->dirname($derivative_uri);

    // Build the destination folder tree if it doesn't already exist.     if (!\Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
      \Drupal::logger('image')->error('Failed to create style directory: %directory', ['%directory' => $directory]);
      return FALSE;
    }

    foreach ($this->getEffects() as $effect) {
      $effect->applyEffect($image);
    }

    if (!$image->save($derivative_uri)) {
      if (file_exists($derivative_uri)) {
        \Drupal::logger('image')->error('Cached image file %destination already exists. There may be an issue with your rewrite configuration.', ['%destination' => $derivative_uri]);
      }
      return FALSE;
    }

    return TRUE;
  }
Home | Imprint | This part of the site doesn't use cookies.