Problem/Motivation

When setting the Htmx value necessary we almost always need some URL to send the request to (using the get/post/put/delete methods). We also have the need to set the _wrapper_format query parameter on htmx requests.

It can be a pain to have to manage the urls, in some case we don't know what they are exactly until later in the process.

There are several approaches we can take:

Setting the request URL

Some of it is made easier by #3546105: Automatically manage _wrapper_format for HTMX requests and clean up ajax_page_state in urls. We're assuming this gets in.

  1. on the backend create a duplicate URL object and use it in get/post/…
    • This can be tricky to find what is the url of the link/form being processed
    • Extra work on the backend to manage the URLs
    $url = Url::fromRoute('some.route');
    (new Htmx())
      ->get($url)
      ->applyTo($element);
    
    // hx-get="/some/url?_wrapper_format=drupal_htmx"
    
  2. create a new syntax to use as get/post/… argument: attr(href)?_wrapper_format=drupal_htmx
    • non standards but implementation at the htmx:configRequest step is the way to go
    (new Htmx())
      ->get('attr(href)')
      ->applyTo($element);
    
    // hx-get="attr(href)?_wrapper_format=drupal_htmx"
    
  3. use hx-boost
    • Recommanded way to go about reusing an existing url, but it push url needs to be set every time, so similar situation as 1
    • Need to clean up the push-url value to remove _wrapper_format and ajax_page_state informations from the URL
    • Need to be careful about more things
    (new Htmx())
      ->boost(TRUE)
      ->applyTo($element);
    
  4. Change the behavior of an empty hx-get/post/… to behave like hx-boost for getting the url to send the request to
    • Not a standard behavior of htmx
    • Nothing extra needed on Drupal side
    (new Htmx())
      ->get() // behaves like attr(href) or hx-boost to et the URL
      ->applyTo($element);
    
    // hx-get="?_wrapper_format=drupal_htmx"
    

Adding the wrapper format when using hx-boost

When using an url from the backend it's not much of a problem. This becomes a problem when using hx-boost where we reuse the "normal" url. Without help we would need to do this for a working set of attributes:

$url = Url::fromRoute('some.route');
(new Htmx())
  ->boost(TRUE)
  // necessary to make sure we don't use the url with 
  // _wrapper_format and ajax_page_state query string
  ->pushUrl($url)
  ->vals(['_wrapper_format' => 'drupal_htmx'])
  // if this is a form with hx-params, _wrapper_format needs to be allowed
  ->params([...$params, '_wrapper_format'])
  ->applyTo($element);

Since we need to explicitly set a url, this is pointless.

  1. automatically add the _wrapper_format parameter to all htmx request on the frontend
    • Problem we can't get a full page easily, would need a custom way to bypass the behavior
  2. use hx-vals to add the _wrapper_format to all htmx requests (setting hx-vals on body for example)
    • problem when allowlisting with hx-params for forms, need to add _wrapper_format to the list otherwise it's ommited
    • Need to change MainContentSubscriber backend so that _wrapper_format in $_POST data is taken into account
  3. Create and use a new data attribute that gets picked up by drupal and adds the wrapper format and ajax page state when necessary.
    • This can be tricky to find what is the url of the link/form being processed
    • Extra work on the backend to manage the URLs
    (new Htmx())
      ->boost(full_page: TRUE)
      ->applyTo($element);
    
    // "data-hx-drupal-full-page" attribute used in htmx-assets.js 
    

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

nod_ created an issue. See original summary.

nod_’s picture

Issue summary: View changes
nod_’s picture

Issue summary: View changes
nod_’s picture

Issue summary: View changes
nod_’s picture

Issue summary: View changes

I'm leaning towards using boost with a drupal-specific attribute to control the wrapper format, ajax page state and push url.

fathershawn’s picture

I think we also have to try to forecast our use of dedicated routes that will be made possible by #3544632: Return only main content for endpoints designed to service htmx requests.

I would like to bring over this route from contrib /htmx/{entityType}/{entity}/{viewMode} and we should do something similar for blocks. Views uses dynamic routes. If we have all of that, I'm not sure how often we will need the wrapper format query parameter.

nod_’s picture

Status: Active » Fixed

After quite a bit of back and forth in the related issue, boost with a special attribute it is.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.