hasSlotsAvailable example

/** * {@inheritdoc} */
  protected function buildInputElement(array $form, FormStateInterface $form_state) {
    // Create a file item to get the upload validators.     $media_type = $this->getMediaType($form_state);
    $item = $this->createFileItem($media_type);

    /** @var \Drupal\media_library\MediaLibraryState $state */
    $state = $this->getMediaLibraryState($form_state);
    if (!$state->hasSlotsAvailable()) {
      return $form;
    }

    $slots = $state->getAvailableSlots();

    // Add a container to group the input elements for styling purposes.     $form['container'] = [
      '#type' => 'container',
    ];

    $process = (array) $this->elementInfo->getInfoProperty('managed_file', '#process', []);
    
public function testMethods() {
    $opener_id = 'test';
    $allowed_media_type_ids = ['document', 'image'];
    $selected_media_type_id = 'image';
    $remaining_slots = 2;

    $state = MediaLibraryState::create($opener_id$allowed_media_type_ids$selected_media_type_id$remaining_slots);
    $this->assertSame($opener_id$state->getOpenerId());
    $this->assertSame($allowed_media_type_ids$state->getAllowedTypeIds());
    $this->assertSame($selected_media_type_id$state->getSelectedTypeId());
    $this->assertSame($remaining_slots$state->getAvailableSlots());
    $this->assertTrue($state->hasSlotsAvailable());

    $state = MediaLibraryState::create($opener_id$allowed_media_type_ids$selected_media_type_id, 0);
    $this->assertFalse($state->hasSlotsAvailable());
  }

  /** * Tests the media library state creation. * * @param string $opener_id * The opener ID. * @param string[] $allowed_media_type_ids * The allowed media type IDs. * @param string $selected_type_id * The selected media type ID. * @param int $remaining_slots * The number of remaining items the user is allowed to select or add in the * library. * @param string $exception_message * The expected exception message. * * @covers ::create * @dataProvider providerCreate */
Home | Imprint | This part of the site doesn't use cookies.