The Url class may be used in a context where you need the query string and fragment generated separately from the path or URL.

Add a helper method to support this, e.g. for \Drupal\menu_link_content\Form\MenuLinkContentForm

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

YesCT’s picture

making child of the homestretch meta

dawehner’s picture

It seems to be useful to put this into the url generator first.

dawehner’s picture

Status: Active » Needs review
FileSize
2.74 KB

Here is one.

dawehner queued 3: 2305013.patch for re-testing.

marvil07 queued 3: 2305013.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 3: 2305013.patch, failed testing.

marvil07’s picture

Status: Needs work » Needs review
FileSize
2.68 KB

Minor reroll(context around hunks changed).

Status: Needs review » Needs work

The last submitted patch, 7: add_a_helper_method_to-2305013-7.patch, failed testing.

dawehner’s picture

+++ b/core/lib/Drupal/Core/Routing/UrlGenerator.php
@@ -319,6 +319,20 @@ public function generateFromPath($path = NULL, $options = array()) {
   /**
+   * {@inheritdoc}
+   */
+  public function generateQueryAndFragment(array $query = array(), $fragment = NULL) {
+    $part = '';
+    if ($query && $query = http_build_query($query, '', '&')) {
+      $part = $query;
+    }
+    if (isset($fragment)) {
+      $part .= '#' . $fragment;
+    }
+    return $part;
+  }
+

Given that this is independent from routing, should we maybe add that rather to the UrlHelper class?

Wim Leers’s picture

Given that this is independent from routing, should we maybe add that rather to the UrlHelper class?

+1

We already have \Drupal\Component\Utility\UrlHelper::buildQuery() there.

OTOH: we do allow users to enter ?foo=bar when creating a menu item. This is stored as internal:?foo=bar, but renders to an empty URL (i.e. the empty string).

dawehner’s picture

Well yeah ideally there would maybe a helper method on both the url assembler and url generator which simply strips of the path.

Wait the menu system has bugs, I DON'T THINK SO ;)

Wim Leers’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2351015: URL generation does not bubble cache contexts

#2351015: URL generation does not bubble cache contexts introduced this.

debug(Url::fromRoute('<none>', [], ['query' => ['already' => 'fixed']])->toString());

outputs

?already=fixed
dawehner’s picture

Lovely, indeed!