getOpenerId example


class OpenerResolver implements OpenerResolverInterface {

  use ContainerAwareTrait;

  /** * {@inheritdoc} */
  public function get(MediaLibraryState $state) {
    $service_id = $state->getOpenerId();

    $service = $this->container->get($service_id);
    if ($service instanceof MediaLibraryOpenerInterface) {
      return $service;
    }
    throw new \RuntimeException("$service_id must be an instance of " . MediaLibraryOpenerInterface::class);
  }

}
/** * 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 */

  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),
    ]);

    return Crypt::hmacBase64($hash, \Drupal::service('private_key')->get() . Settings::getHashSalt());
  }

  /** * Validate a hash for the state object. * * @param string $hash * The hash to validate. * * @return string * The hashed parameters. */
'#theme' => 'links__media_library_menu',
      '#links' => [],
      '#attributes' => [
        'class' => ['js-media-library-menu'],
      ],
    ];

    $allowed_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple($allowed_type_ids);

    $selected_type_id = $state->getSelectedTypeId();
    foreach ($allowed_types as $allowed_type_id => $allowed_type) {
      $link_state = MediaLibraryState::create($state->getOpenerId()$state->getAllowedTypeIds()$allowed_type_id$state->getAvailableSlots()$state->getOpenerParameters());
      // Add the 'media_library_content' parameter so the response will contain       // only the updated content for the tab.       // @see self::buildUi()       $link_state->set('media_library_content', 1);

      $title = $allowed_type->label();
      $display_title = [
        '#markup' => $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span>', ['@title' => $title]),
      ];
      if ($allowed_type_id === $selected_type_id) {
        $display_title = [
          
$neutral_entity = EntityTest::create([
      '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
Home | Imprint | This part of the site doesn't use cookies.