setReplacementId example



  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Save a selected replacement in the image style storage. It will be used     // later, in the same request, when resolving dependencies.     if ($replacement = $form_state->getValue('replacement')) {
      /** @var \Drupal\image\ImageStyleStorageInterface $storage */
      $storage = $this->entityTypeManager->getStorage($this->entity->getEntityTypeId());
      $storage->setReplacementId($this->entity->id()$replacement);
    }
    parent::submitForm($form$form_state);
  }

  /** * Returns a list of image style replacement options. * * @return array * An option list suitable for the form select '#options'. */
  protected function getReplacementOptions() {
    
$form_display->save();

    // Check that the entity displays exists before dependency removal.     $this->assertNotNull(EntityViewDisplay::load($view_display->id()));
    $this->assertNotNull(EntityFormDisplay::load($form_display->id()));

    // Delete the 'main_style' image style. Before that, emulate the UI process     // of selecting a replacement style by setting the replacement image style     // ID in the image style storage.     /** @var \Drupal\image\ImageStyleStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')->getStorage($style->getEntityTypeId());
    $storage->setReplacementId('main_style', 'replacement_style');
    $style->delete();

    // Check that the entity displays exists after dependency removal.     $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
    $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
    // Check that the 'sticker' formatter component exists in both displays.     $this->assertNotNull($formatter = $view_display->getComponent('sticker'));
    $this->assertNotNull($widget = $form_display->getComponent('sticker'));
    // Check that both displays are using now 'replacement_style' for images.     $this->assertSame('replacement_style', $formatter['settings']['image_style']);
    $this->assertSame('replacement_style', $widget['settings']['preview_image_style']);

    
Home | Imprint | This part of the site doesn't use cookies.