O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Attaching managed files to entities

Now that we have our product image field in place and we can store images, let's revisit our JSON response that contains the product data and assume it looks something like this now:

{
  "products" : [
    {
      "id" : 1,
      "name": "TV",
      "number": 341,
      "image": "tv.jpg"
    },
    {
      "id" : 2,
      "name": "VCR",
      "number": 123,
      "image": "vcr.jpg"
    }
  ]
}

What's new is the addition of the image key for each product, which simply references a filename for the image that goes with the respective product. The actual location of the images is at some other path we need to include in the code.

Going back to our JsonImporter::persistProduct() method, let's delegate the handling of the image import to a helper method called handleProductImage() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required