class HtmxFragmentController

Returns responses for HTMX Test Fixtures routes.

Attributes

#[Route(name: 'test_htmx.', requirements: [ '_permission' => 'access content', ], defaults: [ '_title' => 'Htmx Fragment', ])]

Hierarchy

Expanded class hierarchy of HtmxFragmentController

File

core/modules/system/tests/modules/test_htmx/src/Controller/HtmxFragmentController.php, line 13

Namespace

Drupal\test_htmx\Controller
View source
final class HtmxFragmentController extends ControllerBase {
  
  /**
   * Builds the partial response.
   */
  public function buildPartial(string $target, string $swap) : array {
    return [
      '#type' => 'inline_template',
      '#template' => <<<HTMX_PARTIAL
      <hx-partial hx-target="{{ target }}" hx-swap="{{ swap }}">
        {{item}}
      </hx-partial>
      HTMX_PARTIAL,
      '#context' => [
        'target' => $target,
        'swap' => $swap,
        'item' => $swap === 'delete' ? '' : [
          '#type' => 'html_tag',
          '#tag' => 'p',
          '#value' => 'Inserted by htmx!',
        ],
      ],
    ];
  }
  public function buildDeleteInsertPartial() : array {
    return [
      'missing' => $this->buildPartial('div.does-not-exist', 'outerHTML'),
      'insert' => $this->buildPartial('[data-drupal-wrapper-selector="edit-partial-replace"]', 'outerHTML'),
    ];
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.