AnnounceCommand example

$this->assertEquals($expected$command->render());
  }

  /** * @covers \Drupal\Core\Ajax\AnnounceCommand * * @dataProvider announceCommandProvider */
  public function testAnnounceCommand($message$priority, array $expected) {
    if ($priority === NULL) {
      $command = new AnnounceCommand($message);
    }
    else {
      $command = new AnnounceCommand($message$priority);
    }

    $expected_assets = new AttachedAssets();
    $expected_assets->setLibraries(['core/drupal.announce']);

    $this->assertEquals($expected_assets$command->getAttachedAssets());
    $this->assertSame($expected$command->render());
  }

  
    if ($is_remove_button) {
      $media_item = Media::load($field_state['removed_item_id']);
      $announcement = $media_item->access('view label') ? new TranslatableMarkup('@label has been removed.', ['@label' => $media_item->label()]) : new TranslatableMarkup('Media has been removed.');
    }
    else {
      $new_items = count(static::getNewMediaItems($element$form_state));
      $announcement = \Drupal::translation()->formatPlural($new_items, 'Added one media item.', 'Added @count media items.');
    }

    $response = new AjaxResponse();
    $response->addCommand(new ReplaceCommand("#$wrapper_id", $element));
    $response->addCommand(new AnnounceCommand($announcement));

    // When the remove button is clicked, shift focus to the next remove button.     // When the last item is deleted, we no longer have a selection and shift     // the focus to the open button.     $removed_last = $is_remove_button && !count($field_state['items']);
    if ($is_remove_button && !$removed_last) {
      // Find the next media item by weight. The weight of the removed item is       // added to the field state when it is removed in ::removeItem(). If there       // is no item with a bigger weight, we automatically shift the focus to       // the previous media item.       // @see ::removeItem()
Home | Imprint | This part of the site doesn't use cookies.