Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 17 additions & 65 deletions src/Renderer/TangibleFieldsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class TangibleFieldsRenderer implements Renderer {
*/
protected array $field_configs = [];

/**
* Whether assets have been enqueued.
*/
protected bool $enqueued = false;

/**
* Type mapping: DataView type => Tangible Fields type.
*/
Expand Down Expand Up @@ -95,9 +90,6 @@ public function render_editor( Layout $layout, array $data ): string {

$html .= '</div>';

// Schedule asset enqueueing.
$this->schedule_enqueue();

return $html;
}

Expand Down Expand Up @@ -143,12 +135,11 @@ protected function render_section( array $section ): string {
$section_id = sanitize_key( $section['label'] ) . '_' . uniqid();

return $fields->render_field( $section_id, [
'type' => 'accordion',
'label' => $section['label'],
'value' => true,
'isOpen' => true, // Expanded by default.
'fields' => $section_fields,
...$this->get_memory_store_callbacks(),
'type' => 'accordion',
'label' => $section['label'],
'isOpen' => true, // Expanded by default.
'uncontrolled' => true,
'fields' => $section_fields,
] );
}

Expand Down Expand Up @@ -176,11 +167,11 @@ protected function extract_fields_from_item( array $item ): array {

// Wrap in an accordion to preserve the section label.
$fields[] = [
'type' => 'accordion',
'label' => $item['label'],
'title' => $item['label'],
'value' => true, // Expanded by default.
'fields' => $section_fields,
'type' => 'accordion',
'label' => $item['label'],
'title' => $item['label'],
'uncontrolled' => true,
'fields' => $section_fields,
];
} elseif ( $item['type'] === 'tabs' ) {
foreach ( $item['tabs'] ?? [] as $tab ) {
Expand Down Expand Up @@ -234,9 +225,9 @@ protected function render_tabs( array $tabs_structure ): string {
$tabs_id = 'tabs_' . uniqid();

return $fields->render_field( $tabs_id, [
'type' => 'tab',
'tabs' => $tabs,
...$this->get_memory_store_callbacks(),
'type' => 'tab',
'tabs' => $tabs,
'uncontrolled' => true,
] );
}

Expand Down Expand Up @@ -369,7 +360,6 @@ protected function render_simple_field( array $field, string $type, mixed $value
'value' => $this->format_value_for_field( $value, $type ),
'description' => $field['help'] ?? $config['description'] ?? '',
'placeholder' => $field['placeholder'] ?? $config['placeholder'] ?? '',
...$this->get_memory_store_callbacks(),
];

// Add type-specific options.
Expand Down Expand Up @@ -413,7 +403,6 @@ protected function render_repeater_field( array $field, mixed $value, array $con
'value' => $value,
'sub_fields' => $sub_fields,
'layout' => $config['layout'] ?? 'table',
...$this->get_memory_store_callbacks(),
];

// Optional repeater settings.
Expand Down Expand Up @@ -645,25 +634,6 @@ protected function get_default_value( string $slug ): mixed {
return $config['default'] ?? null;
}

/**
* Get memory store callbacks for Tangible Fields.
*
* We use memory store because DataView handles persistence.
*
* @return array Store and permission callbacks.
*/
protected function get_memory_store_callbacks(): array {
$fields = tangible_fields();

return [
...$fields->_store_callbacks['memory'](),
...$fields->_permission_callbacks( [
'store' => [ 'always_allow' ],
'fetch' => [ 'always_allow' ],
] ),
];
}

/**
* Render a list of entities.
*
Expand Down Expand Up @@ -738,31 +708,13 @@ protected function format_value_for_display( mixed $value, string $type, string
}

/**
* Enqueue Tangible Fields assets.
* Required by the interface, but enqueue logic will be handled by
* the fields module
*
* @see https://github.com/TangibleInc/fields/blob/main/enqueue.php
*/
public function enqueue_assets(): void {
if ( $this->enqueued ) {
return;
}

$this->ensure_tangible_fields_loaded();

$fields = tangible_fields();
$fields->enqueue();

$this->enqueued = true;
}

/**
* Schedule asset enqueueing for the footer.
*/
protected function schedule_enqueue(): void {
if ( $this->enqueued ) {
return;
}

// Enqueue in footer to ensure all fields are registered.
add_action( 'admin_footer', [ $this, 'enqueue_assets' ], 5 );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/data-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ public function test_renderer_extract_fields_preserves_section_structure(): void
$this->assertArrayHasKey( 'title', $result[0] ); // Required by Fields library.
$this->assertEquals( 'Contact Info', $result[0]['title'] );
$this->assertEquals( 'Contact Info', $result[0]['label'] );
$this->assertTrue( $result[0]['value'] ); // Expanded by default.
$this->assertTrue( $result[0]['uncontrolled'] );
$this->assertArrayHasKey( 'fields', $result[0] );
$this->assertCount( 2, $result[0]['fields'] );
}
Expand Down