How to use this module with views' field

Created on 31 March 2022, almost 3 years ago
Updated 14 May 2024, 8 months ago

Problem/Motivation

My custom views's field returns JSON as a string:

        "order_line_items": "[{"sku":"GC2","quantity":"5.00","price":"50.00"},{"sku":"SGBLEND","quantity":"1.00","price":"6.00"},{"sku":"NAV1STYR","quantity":"1.00","price":"9.50"}]",

I hope to get rid of the surrounding quotation (") marks.

This is how the field is created

class OrderLineItems extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    // Do nothing -- to override the parent query.
  }


  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $order = $this->getEntity($values);
    
    $details = [];
    
    foreach ($order->getItems() as $order_item) {
      $product_sku = $order_item->getPurchasedEntity()->getSku();
      $product_quantity = $order_item->getQuantity();
      $product_price = $order_item->getUnitPrice()->getNumber();
      
      $order_item_array = [
        'sku' => $product_sku,
        'quantity' => $product_quantity,
        'price' => number_format((float)$product_price, 2, '.', '')
      ];
     $details[] = $order_item_array;
    }
    return json_encode($details);
    return $details;
  }
}

I haven't found how can I add formatter for this custom field, to make this JSON output valid., but I know that this module can make it for a real fields and I hope to get the answer here.

πŸ’¬ Support request
Status

Closed: works as designed

Version

3.0

Component

Miscellaneous

Created by

πŸ‡΅πŸ‡±Poland adpo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    I am moving support to version 3, can you please confirm if this issue still exists in version 3.
    If it does still exist please change the version dropdown to 3.0.x and set the status to Needs Work.

    I suspect this is still an issue and is currently unsupported.

    Thank you!

  • Status changed to Needs work 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    You need to create a FieldFormatter in your custom module that matches what is being done in this module I think.

    If you are going to contribute a more generic plugin for JSON fields feel free to open a merge request against 3.x

  • Status changed to Closed: works as designed 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    Re reading this is unrelated to this module.

Production build 0.71.5 2024