getAllowedTypeIds example

/** * Tests the media library state methods. */
  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 */

  protected function buildMediaTypeMenu(MediaLibraryState $state) {
    // Add the menu for each type if we have more than 1 media type enabled for     // the field.     $allowed_type_ids = $state->getAllowedTypeIds();
    if (count($allowed_type_ids) <= 1) {
      return [];
    }

    // @todo Add a class to the li element.     // https://www.drupal.org/project/drupal/issues/3029227     $menu = [
      '#theme' => 'links__media_library_menu',
      '#links' => [],
      '#attributes' => [
        'class' => ['js-media-library-menu'],
      ],
/** * Get the hash for the state object. * * @return string * The hashed parameters. */
  public function getHash() {
    // Create a hash from the required state parameters and the serialized     // optional opener-specific parameters. Sort the allowed types and     // opener parameters so that differences in order do not result in     // different hashes.     $allowed_media_type_ids = array_values($this->getAllowedTypeIds());
    sort($allowed_media_type_ids);
    $opener_parameters = $this->getOpenerParameters();
    ksort($opener_parameters);
    $hash = implode(':', [
      $this->getOpenerId(),
      implode(':', $allowed_media_type_ids),
      $this->getSelectedTypeId(),
      $this->getAvailableSlots(),
      serialize($opener_parameters),
    ]);

    
'type' => 'test',
      // This label will result in neutral access.       // @see \Drupal\entity_test\EntityTestAccessControlHandler::checkAccess()       'name' => $this->randomString(),
    ]);
    $neutral_entity->save();

    $parameters = $state->getOpenerParameters();
    $parameters['entity_id'] = $neutral_entity->id();
    $state = MediaLibraryState::create(
      $state->getOpenerId(),
      $state->getAllowedTypeIds(),
      $state->getSelectedTypeId(),
      $state->getAvailableSlots(),
      $parameters
    );

    $access_result = $ui_builder->checkAccess($this->createUser()$state);
    $this->assertTrue($access_result->isNeutral());
    $this->assertAccess($access_result, FALSE, NULL, []['url.query_args', 'user.permissions']);

    // Give the user permission to edit the entity and assert that access is     // granted.
Home | Imprint | This part of the site doesn't use cookies.