setRouteParameter example

// Make sure we do not list removed translations, i.e. translations             // that have been part of a default revision but no longer are.             if (!$latest_revision->wasDefaultRevision() || $default_revision->hasTranslation($langcode)) {
              $entity = $latest_revision;
            }
          }
          $translations = $entity->getTranslationLanguages();
        }

        $options = ['language' => $language];
        $add_url = $entity->toUrl('drupal:content-translation-add', $options)
          ->setRouteParameter('source', $original)
          ->setRouteParameter('target', $language->getId());
        $edit_url = $entity->toUrl('drupal:content-translation-edit', $options)
          ->setRouteParameter('language', $language->getId());
        $delete_url = $entity->toUrl('drupal:content-translation-delete', $options)
          ->setRouteParameter('language', $language->getId());
        $operations = [
          'data' => [
            '#type' => 'operations',
            '#links' => [],
          ],
        ];

        

  protected function registerRequest($name$include_password = TRUE, $include_email = TRUE) {
    $user_register_url = Url::fromRoute('user.register')
      ->setRouteParameter('_format', static::$format);
    $request_body = $this->createRequestBody($name$include_password$include_email);
    $request_options = $this->createRequestOptions($request_body);
    $response = $this->request('POST', $user_register_url$request_options);

    return $response;
  }

  /** * {@inheritdoc} */
  protected function setUpAuthorization($method) {
    

  protected function assertRpcLogin($username$password) {
    $request_body = [
      'name' => $username,
      'pass' => $password,
    ];
    $request_options = [
      RequestOptions::HEADERS => [],
      RequestOptions::BODY => $this->serializer->encode($request_body, 'json'),
    ];
    $response = $this->request('POST', Url::fromRoute('user.login.http')->setRouteParameter('_format', 'json')$request_options);
    $this->assertSame(200, $response->getStatusCode());
  }

  /** * Tests PATCHing security-sensitive base fields to change other users. */
  public function testPatchSecurityOtherUser() {
    // The anonymous user is never allowed to modify other users.     if (!static::$auth) {
      $this->markTestSkipped();
    }

    
/** * {@inheritdoc} */
  public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array {
    $config = $static_plugin_config;
    if ($editor->getImageUploadSettings()['status'] === TRUE) {
      $config += [
        'drupalImageUpload' => [
          'uploadUrl' => self::getUrlWithReplacedCsrfTokenPlaceholder(
            Url::fromRoute('ckeditor5.upload_image')
              ->setRouteParameter('editor', $editor->getFilterFormat()->id())
          ),
          'withCredentials' => TRUE,
          'headers' => ['Accept' => 'application/json', 'text/javascript'],
        ],
      ];
    }
    return $config;
  }

  /** * {@inheritdoc} * * @see \Drupal\editor\Form\EditorImageDialog * @see editor_image_upload_settings_form() */
$node = $this->submitNodeForm('Test 1.1 IT', 'published', TRUE, 'it');
    $this->assertNotLatestVersionPage($node);

    // Create a new draft (revision 2).     $this->drupalGet($node->toUrl('edit-form'));
    $this->submitNodeForm('Test 1.2 IT', 'draft', TRUE);
    $this->assertLatestVersionPage($node);

    // Create an English draft (revision 3) and verify that the Italian draft     // values are used as source values.     $url = $node->toUrl('drupal:content-translation-add');
    $url->setRouteParameter('source', 'it');
    $url->setRouteParameter('target', 'en');
    $this->drupalGet($url);
    $this->assertSession()->pageTextContains('Test 1.2 IT');
    $this->submitNodeForm('Test 1.3 EN', 'draft');
    $this->assertLatestVersionPage($node);

    // Create a French draft (without saving) and verify that the Italian draft     // values are used as source values.     $url->setRouteParameter('target', 'fr');
    $this->drupalGet($url);
    $this->assertSession()->pageTextContains('Test 1.2 IT');

    
      $display_plugin = $executable->displayHandlers->get($id);
      if (empty($display_plugin)) {
        continue;
      }

      $tabs[$id] = [
        '#theme' => 'menu_local_task',
        '#weight' => $display['position'],
        '#link' => [
          'title' => $this->getDisplayLabel($view$id),
          'localized_options' => [],
          'url' => $view->toUrl('edit-display-form')->setRouteParameter('display_id', $id),
        ],
      ];
      if (!empty($display['deleted'])) {
        $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-deleted-link';
      }
      if (isset($display['display_options']['enabled']) && !$display['display_options']['enabled']) {
        $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-disabled-link';
      }
    }

    // If the default display isn't supposed to be shown, don't display its tab, unless it's the only display.

  protected function getEditUrl(ContentEntityInterface $entity) {
    if ($entity->access('update', $this->loggedInUser)) {
      $url = $entity->toUrl('edit-form');
    }
    else {
      $url = $entity->toUrl('drupal:content-translation-edit');
      $url->setRouteParameter('language', $entity->language()->getId());
    }
    return $url;
  }

}
$this->assertSame(['text/plain; charset=UTF-8']$response->getHeader('Content-Type'));

    $request_options[RequestOptions::HEADERS]['Accept'] = static::$mimeType;

    // DX: 406 when requesting unsupported format but specifying Accept header:     // should result in a text/plain response.     $response = $this->request('GET', $url$request_options);
    $this->assert406Response($response);
    $this->assertSame(['text/plain; charset=UTF-8']$response->getHeader('Content-Type'));

    $url = Url::fromRoute('rest.entity.' . static::$entityTypeId . '.GET');
    $url->setRouteParameter(static::$entityTypeId, 987654321);
    $url->setOption('query', ['_format' => static::$format]);

    // DX: 404 when GETting non-existing entity.     $response = $this->request('GET', $url$request_options);
    $path = str_replace('987654321', '{' . static::$entityTypeId . '}', $url->setAbsolute()->setOptions(['base_url' => '', 'query' => []])->toString());
    $message = 'The "' . static::$entityTypeId . '" parameter was not converted for the path "' . $path . '" (route name: "rest.entity.' . static::$entityTypeId . '.GET")';
    $this->assertResourceErrorResponse(404, $message$response);
  }

  /** * Transforms a normalization: casts all non-string types to strings. * * @param array $normalization * A normalization to transform. * * @return array * The transformed normalization. */

  protected function loginRequest($name$pass$format = 'json') {
    $user_login_url = Url::fromRoute('user.login.http')
      ->setRouteParameter('_format', $format)
      ->setAbsolute();

    $request_body = [];
    if (isset($name)) {
      $request_body['name'] = $name;
    }
    if (isset($pass)) {
      $request_body['pass'] = $pass;
    }

    $result = \Drupal::httpClient()->post($user_login_url->toString()[
      
$fieldset = $assert_session->elementExists('css', $fieldset_selector);

    $this->assertSame("Media (cardinality $cardinality)", $assert_session->elementExists('css', 'legend', $fieldset)->getText());

    // Assert text that should be displayed regardless of other access.     $this->assertHelpTexts([$use_header$use_help$type_list]$fieldset_selector);

    // The entire section for creating new media should only be displayed if     // the user can create at least one media of the type.     if ($create_media_types) {
      if (count($create_media_types) === 1) {
        $url = Url::fromRoute('entity.media.add_form')->setRouteParameter('media_type', $create_media_types[0]);
      }
      else {
        $url = Url::fromRoute('entity.media.add_page');
      }
      $this->assertHelpTexts([$create_header$create_help]$fieldset_selector);
      $this->assertHelpLink(
        $fieldset,
        'media add page',
        [
          'target' => '_blank',
          'href' => $url->toString(),
        ]
$user_input = $form_state->getUserInput();
    if ($form_state->get('show_preview') || !empty($user_input['js'])) {
      $form['preview'] = [
        '#weight' => 110,
        '#theme_wrappers' => ['container'],
        '#attributes' => ['id' => 'views-live-preview', 'class' => ['views-live-preview']],
        'preview' => $view->renderPreview($this->displayID, $args),
      ];
    }
    $uri = $view->toUrl('preview-form');
    $uri->setRouteParameter('display_id', $this->displayID);
    $form['#action'] = $uri->toString();

    return $form;
  }

  /** * {@inheritdoc} */
  protected function actions(array $form, FormStateInterface $form_state) {
    $view = $this->entity;
    return [
      

  protected $logoutToken;

  /** * {@inheritdoc} */
  protected function initAuthentication() {
    $user_login_url = Url::fromRoute('user.login.http')
      ->setRouteParameter('_format', static::$format);

    $request_body = [
      'name' => $this->account->name->value,
      'pass' => $this->account->passRaw,
    ];

    $request_options[RequestOptions::BODY] = $this->serializer->encode($request_bodystatic::$format);
    $request_options[RequestOptions::HEADERS] = [
      'Content-Type' => static::$mimeType,
    ];
    $response = $this->request('POST', $user_login_url$request_options);

    
$element_style_configuration,
      $toolbar_configuration,
    ];
  }

  /** * {@inheritdoc} */
  public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array {
    $dynamic_plugin_config = $static_plugin_config;
    $dynamic_plugin_config['drupalMedia']['previewURL'] = Url::fromRoute('media.filter.preview')
      ->setRouteParameter('filter_format', $editor->getFilterFormat()->id())
      ->toString(TRUE)
      ->getGeneratedUrl();
    [$element_style_configuration$toolbar_configuration,
    ] = self::configureViewModes($editor);

    $dynamic_plugin_config['drupalElementStyles']['viewMode'] = $element_style_configuration;
    if ($this->getConfiguration()['allow_view_mode_override']) {
      $dynamic_plugin_config['drupalMedia']['toolbar'][] = $toolbar_configuration;
    }
    $dynamic_plugin_config['drupalMedia']['metadataUrl'] = self::getUrlWithReplacedCsrfTokenPlaceholder(
      Url::fromRoute('ckeditor5.media_entity_metadata')
        
'config:rest.resource.dblog', 'http_response'],
      ['user.permissions'],
      FALSE,
      'MISS'
    );
    $log = Json::decode((string) $response->getBody());
    $this->assertEquals($id$log['wid'], 'Log ID is correct.');
    $this->assertEquals('rest', $log['type'], 'Type of log message is correct.');
    $this->assertEquals('Test message', $log['message'], 'Log message text is correct.');

    // Request an unknown log entry.     $url->setRouteParameter('id', 9999);
    $response = $this->request('GET', $url$request_options);
    $this->assertResourceErrorResponse(404, "Log entry with ID '9999' was not found", $response);

    // Make a bad request (a true malformed request would never be a route     // match).     $url->setRouteParameter('id', 0);
    $response = $this->request('GET', $url$request_options);
    $this->assertResourceErrorResponse(400, 'No log entry ID was provided', $response);
  }

  /** * {@inheritdoc} */

  protected function assertRpcLogin(string $username, string $password): void {
    $request_body = [
      'name' => $username,
      'pass' => $password,
    ];
    $request_options = [
      RequestOptions::HEADERS => [],
      RequestOptions::BODY => Json::encode($request_body),
    ];
    $response = $this->request('POST', Url::fromRoute('user.login.http')->setRouteParameter('_format', 'json')$request_options);
    $this->assertSame(200, $response->getStatusCode());
  }

  /** * Tests PATCHing security-sensitive base fields to change other users. */
  public function testPatchSecurityOtherUser() {
    // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.     $url = Url::fromRoute(sprintf('jsonapi.user--user.individual')['entity' => $this->account->uuid()]);
    /* $url = $this->account->toUrl('jsonapi'); */

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