HtmxFragmentController.php

Namespace

Drupal\test_htmx\Controller

File

core/modules/system/tests/modules/test_htmx/src/Controller/HtmxFragmentController.php

View source
<?php

declare (strict_types=1);
namespace Drupal\test_htmx\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\Attribute\Route;

/**
 * Returns responses for HTMX Test Fixtures routes.
 */
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'),
    ];
  }

}

Classes

Title Deprecated Summary
HtmxFragmentController Returns responses for HTMX Test Fixtures routes.

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