Comments

rhuffstedtler created an issue. See original summary.

dawehner’s picture

Component: base system » views.module

Let's move it to views where it kinda belongs to.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

cburschka’s picture

Priority: Normal » Major

This is pretty glaring.

The multiple values option only allow printing the values as a delimiter-separated string or as an HTML list; both of which are ridiculously unsuitable for JSON output.

The only alternative appears to be forcing the REST export to use the field's raw values, which completely bypasses the chosen field formatters.

cburschka’s picture

The major difficulty here appears to be where to integrate it:

1. If REST export categorically bypasses the field plugin's Multiple Field Settings to generate an array, then
a) all fields are shown as arrays, even if they only contain a single value
b) we return an array only if there are multipe values, and the resulting format will suck.

2. If we add an option to the field plugin's Multiple Field Setting that makes it return an array, then the field plugin is no longer certain to generate a string. Other non-REST displays sharing the same field settings will get output they can't process.

3. Adding another column of checkboxes next to the "Raw" option in the Row Style Options is cluttery, and also kind of redundant if "Raw" is checked.

My current proposal would be to turn the Row Style Options into a radio button table like this, allowing each field to either be rendered fully, or returned as an array of rendered items, or returned as raw data like before:

        Field | Alias | String | Array | Raw
        field | [...] |   X    |   O   |  O
Topplestack’s picture

How would one achieve this using the field's raw values? I've got a bit of a rush project and have run into this issue. I'd rather not write something custom if I don't have to.

cburschka’s picture

I ended up making a custom solution that I hope to release as a project soon, but it's not quite ready yet.

It's basically a new views field plugin that "renders" to a wrapper object instead of a string, and a normalization service that turns that wrapper into serializable data.

(The basic problem is that so much of the data in views is passed in render arrays, which makes it tricky to generate something that isn't supposed to end up as a flat string.)

Topplestack’s picture

@cburschka - do you mind sharing what you have thus far? It may save me some time.

cburschka’s picture

I set up a sandbox, but note that it's quite untested. I hope it helps you nonetheless.

mattferderer’s picture

I like the idea of passing all values as array. It's simple, easy to expect. I could see this possibly having some performance issues for larger sites & queries but in the meantime it would be an easy fix. As of now, this issue is a show stopper to implementing a headless CMS.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

johnpicozzi’s picture

@cburschka - Any update on a release for your sandbox module?

Having this issue and found that your module resolved the multiple value format perfectly.

cburschka’s picture

@johnpicozzi thanks! Yeah, I just published a -dev release for it.

johnpicozzi’s picture

@cburschka - Thanks!

motobreath’s picture

@cburschka thank you so much for this module, I see huge potential here! A question, is it possible to apply this same logic to custom field types? For example, I have a custom field that has an image with a checkbox. As of now with views this data comes out as a long string. Would it be possible to use your module to output this as an array?

Thanks!

cburschka’s picture

The module will allow you to format multiple items as an array, but each item would still be a rendered string.

To drill deeper into the structure, you would need to write a custom formatter for your field type, analogously to the ones included in the rest_views module.

motobreath’s picture

Formatting multiple items as an array is exactly what i'm trying to do, but there isn't a formatter from your module available in the dropdown for my field, even if I select the (serialized) field when adding to the view. I would guess that this is because its not a standard Drupal content type (image, etc).

I'll give creating a custom formatter for my field a try. I was able to create a formatter and it shows up in the list, but when selected the output of the REST view is blank. Here is my class:

/**
 * @file
 * Contains \Drupal\product\Plugin\Field\FieldFormatter\ProductImageFormatter.
 */

namespace Drupal\product\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'image' formatter.
 *
 * @FieldFormatter(
 *   id = "ProductImageViewFormatter",
 *   label = @Translation("View Export"),
 *   field_types = {
 *     "ProductImage"
 *   }
 * )
 */
class ProductImageViewFormatter extends ImageFormatter {
  
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = parent::viewElements($items, $langcode);       
    
    $values = $items->getValue();
    
    // Determines how the show image text and value is displayed.
    foreach ($elements as $delta => $element) {
      $showFlag = $values[$delta]['show_image'] ? 'Yes' : 'No';

      // Render output using snippets_default theme.
      $elements[$delta]=[
          "#type"=>"data",
          "#data"=>[
              "show_image"=>$showFlag
          ]          
      ];
    }

    return $elements;
  }

}

Thanks for your help!

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Vikash raj’s picture

please provide me that link of module which accept array for multiple value in rest export.

Elin Yordanov’s picture

Issue summary: View changes

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Graber’s picture

I checked "raw output" checkbox in the "show" settings for "fields". The output is an array. Anything other than the "raw output" included preprocessing that can also be done on the frontend so.. IMO we have all we need here.

immaculatexavier’s picture

Hi,

I have added multiple taxonomy term name in serialized format, but the value is displaying in array format only if i am logged in, but the value is not displaying if i am not logged in.

Can any one help me where i am wrong.

immaculatexavier’s picture

Hi,

I fixed the #23 Issue , by myself by publishing the taxonomy term. Thanks.

dan.hu’s picture

I am not sure this is perfect but I (kind of) solved the problem myself.

What I have done is using these two useful modules at the same time: Views Field View and REST Export Nested. And to achieve the goal described above, we do the following:

1. Create a View with a REST Export Nested display and a contextual argument %nid. For MULTIPLE FIELD SETTINGS, uncheck the option "Display all values in the same row", and save the view first.

2. Create the View that we wish to output multi-valued fields as arrays and add REST Export Nested for the display as well. Add a field with the type Views (this feature is provided by the Views Field View module) and set the View and Display we created in step 1 for the settings. For the Contextual filters, we set the nid of the Content which can be done by adding a hidden ID field to the display.

The output of the field is something like this:

[
  {
    field_name: field_value_1
  },
  {
    field_name: field_value_2
  },
  {
    field_name: field_value_3
  }
]

Again, not perfect, but working more or less.

Nitish k’s picture

Can anybody explain how to achieve this by using Rest Export Nested and Views Field view module?
Thanks in advance.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

joekers’s picture

@Graber this doesn't work for reference fields. I was trying this to solve the issue on an image and a file reference field and only the ID is rendered in an array.

I've been trying @cburschka's module (https://www.drupal.org/project/rest_views) and it seems to be working well for me so far. It also has a handy submodule for image URLs. Thanks for the module btw.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jedihe’s picture

hugronaphor’s picture

Worth mentioning that #22 is the actual answer ot the legacy described issue

Alex Dobson’s picture

Issue summary: View changes

This module worked great for me: https://www.drupal.org/project/rest_views. It adds in a new formatter called "Export rendered entity" that will output as proper JSON. This is the module @cburschka mentioned above so huge thanks to them for creating that!

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mchvdm’s picture

#22 indeed gives an array, but with id's not labels/titles.
#28 the module REST views gives an array with label/titles. Exaclty what I needed. Big thanks!

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.