Skip to content

Feature: Customization filter hooks#104

Open
s3rgiosan wants to merge 18 commits intofeature/data-api-backbonefrom
feature/picked-relationship-component
Open

Feature: Customization filter hooks#104
s3rgiosan wants to merge 18 commits intofeature/data-api-backbonefrom
feature/picked-relationship-component

Conversation

@s3rgiosan
Copy link
Copy Markdown
Member

@s3rgiosan s3rgiosan commented May 19, 2025

Description of the Change

This PR introduces WordPress JavaScript filter hooks in Content Connect's RelationshipManager component, enabling projects to customize search results, picked items, and preview components in the Block Editor UI. These filters are context-aware allowing both global and per-relationship customization.

Problem

Content Connect's Block Editor UI uses hardcoded filter functions that cannot be customized by projects. This limits flexibility when projects need to:

  • Display additional information in search results (e.g., post IDs, custom metadata)
  • Customize how picked items are displayed in the relationship list
  • Replace the default preview component with custom React components
  • Apply different customizations based on relationship type or key

Solution

Added three WordPress JavaScript filter hooks that receive full relationship context:

  1. contentConnect.searchResultFilter - Customizes search result items before display
  2. contentConnect.pickedItemFilter - Customizes picked item data before display
  3. contentConnect.pickedItemPreviewComponent - Allows replacing the preview component with custom React components

All filters receive a context object containing:

  • rel_key: The relationship key
  • rel_type: 'post-to-post' or 'post-to-user'
  • postId: Current post ID
  • mode: Content search mode ('post' | 'user' | 'term')

Filter Usage Examples

Global Customization:

import { addFilter } from '@wordpress/hooks';

addFilter(
  'contentConnect.searchResultFilter',
  'my-project/customize-search-results',
  (defaultFilter, context) => {
    return (item, result) => {
      return {
        ...item,
        url: '',
        info: `<strong>ID:</strong> ${result.id}`,
      };
    };
  }
);

Per-Relationship Customization:

addFilter(
  'contentConnect.pickedItemPreviewComponent',
  'my-project/custom-preview',
  (defaultComponent, context) => {
    if (context.rel_key === 'my-specific-relationship') {
      return ({ item }) => {
        return <CustomComponent item={item} />;
      };
    }
    return defaultComponent;
  }
);

Custom Preview Component:

import { __experimentalText as Text } from '@wordpress/components';
import { decodeEntities } from '@wordpress/html-entities';

addFilter(
  'contentConnect.pickedItemPreviewComponent',
  'my-project/custom-preview',
  (defaultComponent, context) => {
    return ({ item }) => {
      const decodedTitle = decodeEntities(item.title);
      return (
        <Text
          truncate={false}
          title={decodedTitle}
          aria-label={decodedTitle}
        >
          {decodedTitle}
        </Text>
      );
    };
  }
);

How to Test the Change

  1. Build the package: npm run build (from root directory)
  2. Start the test environment: npm run wp-env:start (or your preferred method)
  3. Test contentConnect.searchResultFilter:
    • Add a filter that customizes search result items
    • Verify customizations appear in the search dropdown
    • Test with different relationship types
  4. Test contentConnect.pickedItemFilter:
    • Add a filter that customizes picked item data
    • Verify customizations appear in the picked items list
    • Test with different relationship types
  5. Test contentConnect.pickedItemPreviewComponent:
    • Add a filter that returns a custom React component
    • Verify the custom component renders in the picked items list
    • Test with different relationship types
  6. Test per-relationship customization:
    • Add filters that check context.rel_key or context.rel_type
    • Verify different customizations apply to different relationships
  7. Verify backward compatibility:
    • Ensure default behavior works when no filters are applied
    • Verify existing functionality is not broken

Changelog Entry

Added - WordPress JavaScript filter hooks (contentConnect.searchResultFilter, contentConnect.pickedItemFilter, contentConnect.pickedItemPreviewComponent) for customizing Block Editor UI in RelationshipManager component with context awareness

Credits

Props @s3rgiosan

Checklist:

@s3rgiosan s3rgiosan changed the base branch from develop to feature/data-api-backbone May 19, 2025 12:13
@jeffpaul jeffpaul added this to the 1.7.0 milestone May 20, 2025
@jeffpaul jeffpaul moved this to Code Review in Open Source Practice May 20, 2025
@s3rgiosan s3rgiosan marked this pull request as ready for review January 12, 2026 16:29
@s3rgiosan s3rgiosan requested a review from rickalee as a code owner January 12, 2026 16:29
@s3rgiosan s3rgiosan changed the title Feature: Customized picked relationship item Feature: Customize block editor RelationshipManager UI Jan 12, 2026
@s3rgiosan s3rgiosan changed the title Feature: Customize block editor RelationshipManager UI Feature: Customization filter hooks Jan 12, 2026
@s3rgiosan s3rgiosan requested a review from jeffpaul January 12, 2026 16:36
@s3rgiosan s3rgiosan mentioned this pull request Jan 12, 2026
4 tasks
@s3rgiosan s3rgiosan requested a review from fabiankaegy January 12, 2026 17:50
…ship-component

# Conflicts:
#	dist/js/wp-content-connect.asset.php
#	dist/js/wp-content-connect.js
…ship-component

# Conflicts:
#	assets/js/components/RelationshipManager/index.tsx
#	assets/js/components/RelationshipsPanel/index.tsx
#	assets/js/index.ts
#	dist/js/wp-content-connect.asset.php
#	dist/js/wp-content-connect.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Code Review

Development

Successfully merging this pull request may close these issues.

2 participants