Skip to content

Refactor JsonapiView for custom fields and images#47309

Closed
sadiapeerzada wants to merge 1 commit intojoomla:6.0-devfrom
sadiapeerzada:patch-2
Closed

Refactor JsonapiView for custom fields and images#47309
sadiapeerzada wants to merge 1 commit intojoomla:6.0-devfrom
sadiapeerzada:patch-2

Conversation

@sadiapeerzada
Copy link

PR: Enhanced JsonapiView with Safe Custom Fields & Core Fixes

Summary

This PR improves the JsonapiView of com_content in the Joomla API by:

  1. Introducing a safe container for custom fields (custom_fields) in both single item and list views.
  2. Fixing dynamic field injection — removed runtime injection and standardized fields to render.
  3. Enhanced preparation of items:
    • Properly concatenates introtext and fulltext for API output.
    • Triggers onContentPrepare for all content plugins.
    • Safely processes custom fields with fallback to rawvalue.
    • Resolves tags consistently using TagsHelper.
    • Handles core images (image_intro and image_fulltext) with proper URL resolution.
    • Supports multilingual associations when Multilanguage is enabled.

Key Changes

  • Added $fieldsToRenderItem and $fieldsToRenderList with custom_fields.
  • Added $relationship array including category, created_by, modified_by, tags, and multilingual associations.
  • Updated prepareItem() method for:
    • Safe processing of custom fields.
    • Safe conversion of images to array and URL resolution.
    • Proper tags fetching.
    • Multilingual association objects.

Benefits

  • Ensures API consumers receive a consistent, safe, and complete dataset for articles.
  • Fixes prior issues with dynamic field injection.
  • Supports multilingual and tag-rich content seamlessly.
  • Compatible with all current and future content plugins.

Notes

  • No breaking changes for existing API consumers.
  • Fully compatible with Joomla 4+ API component standards.
  • custom_fields are safely exposed via API without relying on internal raw properties.

Removed copyright notice and updated comments. Added custom fields handling and improved image processing.
@alikon
Copy link
Contributor

alikon commented Mar 6, 2026

@sadiapeerzada please fix the code style

i've made a quick test and your pr seems promising

{
    "links": {
        "self": "http:\/\/localhost:7060\/api\/index.php\/v1\/content\/articles"
    },
    "data": [
        {
            "type": "articles",
            "id": "1",
            "attributes": {
                "id": 1,
                "asset_id": 101,
                "title": "test_imagesin_fileds",
                "alias": "test-imagesin-fileds",
                "state": 1,
                "access": 1,
                "created": "2026-03-05 10:44:12",
                "created_by": 657,
                "created_by_alias": "",
                "modified": "2026-03-06 08:16:29",
                "featured": 0,
                "language": "*",
                "hits": 0,
                "publish_up": "2026-03-05 10:44:12",
                "publish_down": null,
                "note": "",
                "images": {
                    "image_intro": "http:\/\/localhost:7060\/images\/sampledata\/fruitshop\/apple.jpg#joomlaImage:\/\/local-images\/sampledata\/fruitshop\/apple.jpg?width=375&height=281",
                    "image_intro_alt": "",
                    "float_intro": "",
                    "image_intro_caption": "",
                    "image_fulltext": "http:\/\/localhost:7060\/images\/sampledata\/fruitshop\/bananas_2.jpg#joomlaImage:\/\/local-images\/sampledata\/fruitshop\/bananas_2.jpg?width=300&height=352",
                    "image_fulltext_alt": "",
                    "float_fulltext": "",
                    "image_fulltext_caption": ""
                },
                "metakey": "",
                "metadesc": "",
                "metadata": {
                    "robots": "",
                    "author": "",
                    "rights": ""
                },
                "version": 4,
                "featured_up": null,
                "featured_down": null,
                "typeAlias": "com_content.article",
                "text": " ",
                "custom_fields": {
                    "images": "provaprova",
                    "asset-id": "{\"imagefile\":\"images\\\/sampledata\\\/parks\\\/animals\\\/200px_phyllopteryx_taeniolatus1.jpg#joomlaImage:\\\/\\\/local-images\\\/sampledata\\\/parks\\\/animals\\\/200px_phyllopteryx_taeniolatus1.jpg?width=180&height=135\",\"alt_text\":\"\"}"
                },
                "tags": []
            },
            "relationships": {
                "category": {
                    "data": {
                        "type": "categories",
                        "id": "2"
                    }
                },
                "created_by": {
                    "data": {
                        "type": "users",
                        "id": "657"
                    }
                },
                "tags": {
                    "data": []
                }
            }
        }
    ],
    "meta": {
        "total-pages": 1
    }
}

@bembelimen bembelimen added the b/c break This item changes the behavior in an incompatible why. HEADS UP label Mar 6, 2026
@bembelimen
Copy link
Contributor

Please follow our AI policy for communication and contribution.

@bembelimen bembelimen closed this Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

b/c break This item changes the behavior in an incompatible why. HEADS UP PR-6.0-dev

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants