question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The block fetched without img just title and alt, tha same for taxonomy (no taxonomy name)?

See original GitHub issue

Hi

I’ve created a block in Drupal then try to fetch its content (3 image fields with title and alt + taxonomy term) I’ve got the following data structure and I’ve got just title and alt no img the same goes for taxonomy term no taxonomy name

I’ve used getResource('block_content--basic', 'uid') method to fetch the block.

{
    "type": "block_content--basic",
    "id": "a1e0df3f-88b8-4c04-859a-c5e8eb9z5b17",
    "drupal_internal__id": 2,
    "drupal_internal__revision_id": 2,
    "langcode": "en",
    "revision_created": "2022-03-31T15:51:08+00:00",
    "revision_log": null,
    "status": true,
    "info": "Bagdes",
    "changed": "2022-04-01T14:50:30+00:00",
    "reusable": true,
    "default_langcode": true,
    "revision_translation_affected": true,
    "body": null,
    "links": {
        "self": {
            "href": "http://web/jsonapi/block_content/basic/a1e0df3f-88b8-4c04-859a-c5e8eb9z5b17"
        }
    },
    "block_content_type": {
        "type": "block_content_type--block_content_type",
        "id": "48f76ec0-7c2a-4c91-a0dc-6f53434e3e92",
        "resourceIdObjMeta": {
            "drupal_internal__target_id": "basic"
        }
    },
    "revision_user": null,
    "field_acquia_drupal_7_certificat": {
        "type": "file--file",
        "id": "78148035-a610-4516-b02d-a14a3a10203d",
        "resourceIdObjMeta": {
            "alt": "Acquia Drupal 7 certification",
            "title": "Acquia Drupal 7 certification",
            "width": 147,
            "height": 147,
            "drupal_internal__target_id": 16
        }
    },
    "field_acquia_drupal_8_certificat": {
        "type": "file--file",
        "id": "12cdc36a-e4ab-4830-aa60-481296b2a173",
        "resourceIdObjMeta": {
            "alt": "Acquia Drupal 8 certification",
            "title": "Acquia Drupal 8 certification",
            "width": 295,
            "height": 295,
            "drupal_internal__target_id": 18
        }
    },
    "field_bagdes": {
        "type": "taxonomy_term--achevements",
        "id": "2cd66146-893f-4178-b092-10d88ff2b62f",
        "resourceIdObjMeta": {
            "drupal_internal__target_id": 1
        }
    },
    "field_drupal_association_members": {
        "type": "file--file",
        "id": "449195da-beca-4151-b4ce-dc9f3b23aa05",
        "resourceIdObjMeta": {
            "alt": "Drupal Association membership",
            "title": "Drupal Association membership",
            "width": 217,
            "height": 217,
            "drupal_internal__target_id": 17
        }
    },
    "relationshipNames": [
        "block_content_type",
        "revision_user",
        "field_acquia_drupal_7_certificat",
        "field_acquia_drupal_8_certificat",
        "field_bagdes",
        "field_drupal_association_members"
    ]
}

Please if someone can help because there is a native way using JS Promise to fetch the image that resides in this api route link but i think there will be another special next-drupal way to fetch the image as well.

http://127.0.0.1:8080/jsonapi/block_content/basic/a1e0df3f-88b8-4c04-859a-c5e8eb9z5b17/field_acquia_drupal_7_certificat

Thanks in advance.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
shadcncommented, Apr 1, 2022

Quick note: when you include the field_badges it will fetch all the fields for the terms. You probably care about a few fields only. You can do so as follows:

const block = await getResource<DrupalBlock>(
  "block_content--basic",
  "a1e0df3f-88b8-4c04-859a-c5e8eb9z5b17",
  {
    params: {
      include: "field_bagdes, field_acquia_drupal_7_certificat",
      "fields[taxonomy_term--achevements]": "id, name", // <---- Only give me the id and the name for the terms.
    },
  }
)
3reactions
shadcncommented, Apr 1, 2022

You can fetch additional data (relationships or entity references in Drupal) using the “include” params.

const block = await getResource<DrupalBlock>(
  "block_content--basic",
  "a1e0df3f-88b8-4c04-859a-c5e8eb9z5b17",
  {
    params: {
      include: "field_bagdes, field_acquia_drupal_7_certificat", // <----- This will include the taxonomy terms and the image from "field_acquia_drupal_7_certificat"
    },
  }
)

~What is the name of the image field you want to fetch? field_drupal_association_members ?~

Update: added include for field_acquia_drupal_7_certificat.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image Alt Text vs. Title Text vs. File Names: What's the ...
What's the difference between image alt text, title text, and file names? Learn how to optimize each one for search.
Read more >
Image SEO Options in Rank Math
How to Name Images and Optimize Images for SEO? ... Note: Rank Math adds missing ALT attributes only for images added in post...
Read more >
H67: Using null alt text and no title attribute on img ... - W3C
If no title attribute is used, and the alt text is set to null (i.e. alt="" ) it indicates to assistive technology that...
Read more >
How to Add Media Title and Alt Tags Automatically in WordPress
Optimizing your website for search engines like Google is difficult. You can simplify it with this guide to add media titles and alt...
Read more >
register_post_type() | Function
When registering a post type, always register your taxonomies using the taxonomies argument. If you do not, the taxonomies and post type will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found