Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterActive::render()protectedWC 1.0

Render the block.

Method of the class: ProductFilterActive{}

Hooks from the method

Return

String. Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
$content(string) (required)
Block content.
$block(WP_Block) (required)
Block instance.

ProductFilterActive::render() code WC 8.9.0

<?php
protected function render( $attributes, $content, $block ) {
	$query_id = $block->context['queryId'] ?? 0;

	/**
	 * Filters the active filter data provided by filter blocks.
	 *
	 * $data = array(
	 *     <id> => array(
	 *         'type' => string,
	 *         'items' => array(
	 *             array(
	 *                 'title' => string,
	 *                 'attributes' => array(
	 *                     <key> => string
	 *                 )
	 *             )
	 *         )
	 *     ),
	 * );
	 *
	 * @since 11.7.0
	 *
	 * @param array $data   The active filters data
	 * @param array $params The query param parsed from the URL.
	 * @return array Active filters data.
	 */
	$active_filters = apply_filters( 'collection_active_filters_data', array(), $this->get_filter_query_params( $query_id ) );

	$context = array(
		'queryId' => $query_id,
		'params'  => array_keys( $this->get_filter_query_params( $query_id ) ),
	);

	$wrapper_attributes = get_block_wrapper_attributes(
		array(
			'data-wc-interactive' => wp_json_encode( array( 'namespace' => $this->get_full_block_name() ) ),
			'data-wc-context'     => wp_json_encode( $context ),
			'data-has-filter'     => empty( $active_filters ) ? 'no' : 'yes',
		)
	);

	$list_classes = 'filter-list';

	if ( 'chips' === $attributes['displayStyle'] ) {
		$list_classes .= ' list-chips';
	}

	ob_start();
	?>

	<div <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
		<?php if ( ! empty( $active_filters ) ) : ?>
			<ul class="<?php echo esc_attr( $list_classes ); ?>">
				<?php foreach ( $active_filters as $filter ) : ?>
				<li>
					<span class="list-item-type"><?php echo esc_html( $filter['type'] ); ?>: </span>
					<ul>
						<?php $this->render_items( $filter['items'], $attributes['displayStyle'] ); ?>
					</ul>
				</li>
				<?php endforeach; ?>
			</ul>
			<button class="clear-all" data-wc-on--click="actions.clearAll">
				<span aria-hidden="true"><?php echo esc_html__( 'Clear All', 'woocommerce' ); ?></span>
				<span class="screen-reader-text"><?php echo esc_html__( 'Clear All Filters', 'woocommerce' ); ?></span>
			</button>
		<?php endif; ?>
	</div>

	<?php
	return ob_get_clean();
}